PHP Classes

Extended MySQLi: MySQL database access wrapper using MySQLi

Recommend this page to a friend!

  Author Author  
Name: Camilo Sperberg <contact>
Classes: 8 packages by
Country: The Netherlands The Netherlands
Age: 36
All time rank: 75822 in The Netherlands The Netherlands
Week rank: 323 Up13 in The Netherlands The Netherlands Up

  Detailed description   Download Download .zip .tar.gz   Install with Composer Install with Composer  
This package implements a MySQL database access wrapper using the MySQLi extension.

It is based on the Reduced MySQLi class originally written by Mertol Kasanan.

This version can account the memory usage, log the executed queries in the current page.




This class is made by unreal4u (Camilo Sperberg). However, the initial idea isn't mine, so I would like to thank Mertol Kasanan, this class is based on his work. See for details.

About this class

  • It receives parametrized and simple SQL queries.
  • It creates 3 arrays: one containing all data, and another one that contains some statistics. Optionally, it logs all errors into another array and also into an valid XML file.
  • The DB connection made is singleton, that means only one connection is made for all your queries, even if you have more than 1 instance. The connection is established on demand, not when you initialize the class.

Detailed description

This package implements a MySQL database access wrapper using the MySQLi extension.

There is class that manages MySQL database access connections so only one connection is established during the same PHP script execution.

Another class implements other database access functions like executing queries with prepared queries, measuring the time the queries take to execute and the memory usage, retrieving query results into arrays, the number of result rows, last inserted record identifier and log executed queries to a valid XML log file or directly into your page.

If the query takes just too long, you can cache the query result into an XML file, and you can also handle errors.

This package has been extensivily tested with xDebug, APC and Suhosin so that no errors are present.

Basic usage

<pre>include('src/unreal4u/config.php'); // Please see below for explanation include('src/unreal4u/dbmysqli.php'); $dbLink = new unreal4u\dbmysqli(); $id_user = 23; $username = 'unreal4u'; $aResult = $dbLink->query('SELECT id,username FROM users WHERE id = ? AND username = ?',$id_user,$username);</pre>

  • Congratulations! $aResult haves the result of your query!
  • Now you can do anything you want with the array, one of the easiest methods to go trough it is a foreach: <pre>foreach($aResult AS $a) { echo 'The id of the user named '.$a['username'].' is: '.$a['id']."\n"; }</pre>
  • In case of large queries, don't forget to unset the results in order to save PHP's memory for later: unset($aResult);
  • Please see index.php for more options and advanced usage

Including with composer

Add this to your composer.json: <pre> { "require": {

   "unreal4u/dbmysqli": "@stable"

} } </pre>

Now you can instantiate a new dbmysqli class by executing:

<pre> require('vendor/autoload.php');

$rutverifier = new unreal4u\dbmysqli(); </pre>

Pending --------- * Multiquery support. * Better naming convention to include other RDSMs later on * Convert to PDO (to support :tag type associations)

Version History

  • 2.0.0 : * Original file with some changes, now using constants instead of variables to make the connection * Fixed some minor bugs in case of no results of query, time calculation and variable initialization
  • 2.0.1 : * Added live statistics, it displays: * Memory usage * Time * Queries executed
  • 2.1.0b: Some mayor changes: (that never became final release) * Now supporting XML log with SimpleXML. * Added data array size: displays total data size in bytes. * XML now logs also: * Number of results * Data array size * Number of queries * Live statistics now displays data array size and the query with its binding.
  • 2.1.1 : * Fixed some bugs with live statistics * Fixed some minor bugs related with the array data size
  • 2.1.2 : * Error handling is now little better, however it will be improved further. * Data array size is now optional: it is just TOO slow. I'll see if I can improve it later. * Fixed minor bug with XML logging related to number of results
  • 2.1.5 - RC: * Now the class supports caching. * Proper error handling is now available. * Improved logging system a little more. It is a lot clearer now, it has it's own function.
  • 2.1.5: * Fixed all sort of bugs. * Improved cache: it was faster to read the XML file with SimpleXML and structure it with PHP. * Added some more error exceptions. (Couldn't connect to DB, couldn't create cache file, etc)
  • 2.1.6: * Improved error handling. * Added some more error exceptions. (Mainly possible file problem issues) * Added a new constant: DBPORT, in order to connect to a specific port different than the default. * Fixed a little bug in DB_Connect with variable name. (Was private static, should have been only private) * Special functions __call, __get, etc are now private. * DB_SHOW_ERRORS now working properly. * Class is now valid for PHP &gt;= 5.1.6.
  • 2.1.7: * Fixed little inconsistency when webserver's charset was different from mysql's charset.
  • 2.2.0: * Better error management: The class will no longer fail when a certain table or field doesn't exist. * <code>$dbLiveStats</code> now shows you the error. * Compatible (in strict mode) with PHP 5.3. All magic methods are now public. * Changed file name from <code>db.class.php</code> to <code>mysql.class.php</code>. * Changed basic class name from <code>DB()</code> to <code>DB_mysql()</code>. * Better XML validation. (!) * Fixed some minor errors on this README xD
  • 3.0.0: * Runs on PHP5.3 on Windows and Linux without problems. * The class is now compatible with PHPDocumentor, so technical reference should be easier. * <code>$dbLiveStats</code> and <code>$dbErrors</code> are now public variables, so no more global variables
  • 3.1.0: * Support for transactions! * Connection is now made on demand, that means that if the class is instanciated but not used, no connection to MySQL will be made. * Better documentation * Full revision of methods, added some new ones, optimized some old ones. * This file is under SVN control now :)
  • 4.0.0: * No longer is XML-based cache used, cacheManager class (see my other classes) is now in charge of doing all that job! * Better exception handling
  • 4.0.1: * Support for multi-connections * Better documentation * Code cleanup and some minor improvements
  • 4.1.0: * Made class compatible with composer.phar and PSR-0 autoloader standards * Some minor fixes in documentation and code
  • 4.1.1: * Fixes * Better documentation
  • 4.1.2: * Totally forgot about PSR-0 underscore standard. The class is now refactored to wipe out the usage of underscore in the class's name
  • 4.1.3: * Better documentation * Year change
  • 5.0.0: * [BC] Result array is now an SplFixedArray, uses less memory and should be a bit faster as well * [BC] More types supported: float and DateTime objects are now returned for those type of data's

Contact the author

  Classes of Camilo Sperberg  >  Extended MySQLi  >  Download Download .zip .tar.gz  >  Support forum Support forum (9)  >  Blog Blog  >  RSS 1.0 feed RSS 2.0 feed Latest changes  
Name: Extended MySQLi
Base name: mysqli-extended
Description: MySQL database access wrapper using MySQLi
Version: 4.0.1
PHP version: 5.3.0
License: BSD License
All time users: 2182 users
All time rank: 1770
Week users: 0 users
Week rank: 391 Equal
  Groups   Screenshots Screenshots   Rate classes User ratings  
  Trackback   Applications   Related pages   Files Files  

Group folder image PHP 5 Classes using PHP 5 specific features View top rated classes
Group folder image Databases Database management, accessing and searching View top rated classes
Group folder image Cache Classes for managing cached pages View top rated classes

  Files folder image Screenshots  
File Role Description
Accessible without login Image file 2009-11-24.u4u-0012.png Screen print_r of result array
Accessible without login Image file 2009-11-24.u4u-0013.png Screen print_r of error array
Accessible without login Image file 2009-11-24.u4u-0015.png Screen print_r of livestats array
Accessible without login Image file 2009-11-24.u4u-0016.png Screen view of generated xml log


What is the best PHP mysqli class?
recommend a mysqli class

Best Package to Address SQL Injection Vulnerabilities
Upgrading security of existing MySQL code

  User ratings  
RatingsUtility Consistency Documentation Examples Tests Videos Overall Rank
All time: Perfect (100%) Good (93%) - Perfect (100%) - - Sufficient (69%) 444
Month: Not yet rated by the users

  Pages that reference this package  
Ayuda con "proyecto cms"
Lo primero y más importante: tu código es MUY vulnerable a inyecciones SQL, te recomiendo que trabajes con mi class que soluciona eso:...
Duda son sesiones
Duda son sesiones lo más fácil y rápido es crear un campo que se llame id_grupo en la tabla de menú y seleccionas sólo aquellos que le son pertinentes...
yahoo!! publicaron mi class :D
Hoy me levanté y me encontré con la sorpresa de que aprobaron y publicaron mi class en
¿Con escapar los valores basta?
...Recién me di cuenta que cuando hiciste este tema todavía no había publicado mi class xDDDD...

Latest pages that reference packages Latest pages that reference packages

  Applications that use this package  
Personal home page
My (unfinished) personal homepage uses this class
A client's personal website

Add link image If you know an application of this package, send a message to the author to add a link here.

  Related pages  
Live Demo
This is a live demo on my test server
Generated XML Log
This is the XML log file auto-generated by the class
Cache & no-cache Benchmark results
A special care was taken with speed. Is caching good or bad? Here the results
Class on GitHub
Class on GitHub

  Files folder image Files  
File Role Description
Files folder imagedocumentation (6 files)
Files folder imagesrc (1 directory)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file Doc. Auxiliary data

  Files folder image Files  /  documentation  
File Role Description
  Accessible without login Plain text file capturing-exceptions.php Example Example script
  Accessible without login Plain text file complex-example.php Example Example script
  Accessible without login Plain text file multiple-databases.php Example Example script
  Accessible without login HTML file README.html Doc. Documentation
  Accessible without login Plain text file simple-example.php Example Example script
  Accessible without login Plain text file transaction-example.php Example Example script

  Files folder image Files  /  src  
File Role Description
Files folder imageunreal4u (3 files)

  Files folder image Files  /  src  /  unreal4u  
File Role Description
  Accessible without login Plain text file auxiliar_classes.php Aux. Auxiliary script
  Accessible without login Plain text file config.php Conf. Configuration script
  Accessible without login Plain text file dbmysqli.php Class Class source

Install with Composer Install with Composer - Download Download all files: mysqli-extended.tar.gz
NOTICE: if you are using a download manager program like 'GetRight', please Login before trying to download this archive.