PHP Classes

Extended MySQLi: MySQL database access wrapper using MySQLi

Recommend this page to a friend!
  Info   View files Example   Screenshots Screenshots   View files View files (11)   DownloadInstall with Composer Download .zip   Reputation   Support forum (9)   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStarStar 69%Total: 2,208 All time: 1,767 This week: 284Up
Version License PHP version Categories
mysqli-extended 4.0.1BSD License5.3.0PHP 5, Databases, Cache


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.

Picture of Camilo Sperberg
  Performance   Level  
Name: Camilo Sperberg <contact>
Classes: 8 packages by
Country: The Netherlands The Netherlands
Age: 41
All time rank: 75520 in The Netherlands The Netherlands
Week rank: 118 Up1 in The Netherlands The Netherlands Up


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

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




$db = new unreal4u\dbmysqli();
$db->supressErrors = true;
$db->keepLiveLog = true;

$db->query('SELECT * FROM a');





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

  • 2009-11-24.u4u-0012.png
  • 2009-11-24.u4u-0013.png
  • 2009-11-24.u4u-0015.png
  • 2009-11-24.u4u-0016.png
  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

 Version Control Unique User Downloads Download Rankings  
This week:0
All time:1,767
This week:284Up
 User Ratings  
 All time