PHP MySQL Wrapper
I’m releasing a very old PHP class that I created circa 2006 to act as a wrapper for PHP’s MySQL functions. I recently updated it to use PHP’s MySQLi extension. It has a number of simple functions that accept parameters via arrays and builds queries with them.
The class is used statically, not actually instantiated, so it actually works more like a namespace. For instance:
// Connect to database
DB::connect('localhost', 'db', 'root', 'pass');
// Get an array of all employees with the first name 'fred' in descending order by their start date
$employees = DB::selectArray(
'employees',
'*',
array('first_name' => 'fred'),
'date_start',
true
);
// Insert new record into the employees table and set start date
$employee = array(
'first_name' => 'bob',
'mast_name' => 'smith'
);
$recordId = DB::insert('employees', $employee, 'date_start');
// Disconnect from database
DB::disconnect();
This makes the wrapper fast and easily accessible in functions, but also quite limited. For instance, it’s not meant for situations in which you will need to connect to multiple databases concurrently.
There are certainly more robust database solutions out there, but for simple projects and scripts, this class may prove useful. I still use it often times for quick, one-time import situations.
As of version 2.0 (August 18th 2013), this class uses PHP’s MySQLi extension.
Grab a copy of the wrapper from the GitHub repo here: PHP MySQL Wrapper