php[architect] logo

Want to check out an issue? Sign up to receive a special offer.

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.


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.
 

Responses and Pingbacks

Take a look at NotORM, the query would be even simpler (with good readability):

$count = $db->widgets()->where(“quanity > ?”, $quantity)->count(“*”);

This is just the basic stuff, NotORM is even more powerful with relations which can be easily retrieved with exceptional performance.

Static configuration (DB::$host etc) setting makes it impossible to use multiple DBs, using static classes is a bad idea in general anyway.
Might be good enough for small projects, but as soon as you need more features like handling multiple transactions, it seems to be too simple.
Simple is good, but you should be able to extend it, and not be locked in.

Hi there,

I have just released a BETA version of Inform8 which supports ORM and a OO Query Language called IQL. It also uses code generation to build your entire integration layer for you. It’s very simple and powerful.

Examples:

Returns an array of Product Objects
$products = ProductIQL::select()->get();

Returns an array of Product Objects where price > 5
$products = ProductIQL::select()->where( ProductIQL::$_TABLE, ProductIQL::$PRICE, ‘>’, 5)->get();

Count all records
$count = ProductIQL::count()->get();

There are some demonstration screencasts at http://inform8.com

Regards
Ryan

This is really something I wouldn’t expect from php|arch – featuring a not so good solution.

Such “simpler” solutions should be completely disregarded in respect of us to raise the quality of the code produced. Instead, you are featuring this ugly mysql client candidate in php architect? Really?

@Nikola Petkanski
Stop complaining, not everyone are developing banking systems only. Sometimes you only need 2-3 tables and MeekroDB is just fine for that.

@Spiechu // Like++

I honestly don’t see how it makes it simpler.

MeekroDB has a few nice points:
– SQL injections protection built-in (wonderful!),
– easy to use,
– clean and readable code.

But the downsides are:
– single DB connection only,
– always fetches results in an array (read: memory problems!),
– MySQL only,
– can’t use prepared statements

All of these things could be built without sacrificing the good points, but judging from posts on author’s site they won’t be. Too bad, it would make a great library otherwise.

@Andrew // What’s wrong with fetching results in an array? Could you explain me a little bit more?

I will give it a try, looks simple enough to learn for a beginner 🙂

I had a look at its source code and have to say I stopped reading after a few minutes, the code is very unreadable and not testable. It seems to be good for mini or weekend project, but as long as the project grows to certain size, this library will become a bottleneck and will require a lot of efforts to make the switch.

[…] the php|architect site today John Mertic has a new post looking at a simpler MySQL library – MeekroDB. I remember fondly back in the day, when PHP 4.0 was all the rage, that if you wanted to […]

William Edstrom on
December 18th, 2014 at 5:10 pm

Meekro DB also has an instance option allowing you to have multiple database connections at once. The static singleton-esque method is optional.

Leave a comment

Use the form below to leave a comment: