Looking for a simpler MySQL library? Try MeekroDB

Posted by on March 24, 2011
 

I remember fondly back in the day, when PHP 4.0 was all the rage, that if you wanted to interface with MySQL there was one choice: the PHP mysql extension. Then came database abstraction layers ( such as ADODB and PEAR_DB ) and newer extensions ( namely PDO and mysqli ), all with the goal of simplifying the code needing written to be able to get data successfully out of a database. But even then, it still could get a bit complex. Fortunately, someone saw this and wrote a library to make it even simpler. This library is MeekroDB.

MeekroDB dubs itself as “The Simple PHP MySQL library”, and has a philosophy of making what is simple simple. How simple is it? Well let’s look at this snippet where we are looking for the total count of widgets that the quantity is greater than the specified variable $quantity, where we use the PHP mysqli extension

$mysqli = new Mysqli('my_database_server','my_database_user',
'my_database_password','my_database_name');

$result = $mysqli->prepare("SELECT COUNT(*) FROM widgets WHERE quanity >?");
$mysqli->bind_param("i", $quantity);
$mysqli->execute();
$mysqli->bind_result($count);
$mysqli->fetch();
$row = $result->fetch_assoc();

echo $count . " widgets found whose quantity is greater than " . $quantity . ".";

This of course takes the path of using prepared statements, where we make sure that no evil SQL injection attacks can be performed. As you can see, it gets a bit lengthy and it’s very elegant. Now, let’s look at the code you’d use with MeekroDB to do the same thing.

require_once 'meekrodb.class.php';
DB::$host = 'my_database_server';
DB::$user = 'my_database_user';
DB::$password = 'my_database_password';
DB::$dbName = 'my_database_name';

$count = DB::queryFirstField("SELECT COUNT(*) FROM widgets WHERE quanity > %i", $quantity);

echo $count . " widgets found whose quantity is greater than " . $quantity . ".";

Using MeekoDB, we’ve squashed those 6 ugly lines handling the prepared statement and fetching the result into one line, with all the same protections from SQL injection that we have with the mysqli version above. This is the true power of this library, where it makes the tediousness of interacting with PHP’s DB layer simple, yet still powerful.

For more examples of working with the library, check out the quickstart guide they have put together, and the documentation for a more indepth look at how to use the library.


About the author—John Mertic is a senior software engineer and serves as the partner and community developer liaison at SugarCRM, having several years of experience with PHP web applications. At SugarCRM, he has specialized in data integration, mobile and user interface architecture. An avid writer, he has been published in php|architect, IBM Developerworks, and in the Apple Developer Connection, and is the author of the book 'The Definitive Guide to SugarCRM: Better Business Applications'. He has also contributed to many open source projects, most notably the PHP project where is the creator and maintainer of the PHP Windows Installer.