I always felt performing MySQL queries took way to much effort and
LOC. So over the past few years, I've been building a class that
simplifies the existing MySQLi class. I called it MySQL Advanced
(MySQLa).
Here are some of the features:
- No object instantiations needed.
- Single line access to any query.
- Built in parameter binding.
- Results are Object, Numeric, and Array accessible.
- Accessible from anywhere in the code (statically) (no passing
parameters or using globals).
- Store multiple connections but only connect to those you use.
- Automagically opens and closes connections when needed.
- Throws exceptions instead of requiring additional function calls
for error codes.
Here is a recreation of the example from the PHP Manual using my new
class: http://www.php.net/manual/en/mysqli-stmt.fetch.php
try {
MySQLa::addServer("localhost", "my_user", "my_password",
"world");
// Return all the rows
$rows = MySQLa::query("SELECT Name, CountryCode FROM City ".
"ORDER by ID DESC LIMIT 150,5",
MySQLa::RETURN_ALL)->fetch();
// Iterate through the rows
foreach ($rows as $row) {
printf ("%s (%s)\n", $row->Name, $row->CountryCode);
}
} catch (MySQLa_Exception $ex) {
// Check for any errors in Connection, Query, or Result.
echo "MySQLa Exception [".$ex->getCode()."]: ".$ex-
>getMessage();
}
As you can see, the query itself was only one line. This is also the
case even when you are using parameter binding.
Source code, tutorial, and instructions are available at my site:
http://www.stjohnjohnson.com/projects/mysql-advanced
Take a look at it, fool around with it, and let me know what you
think. Thanks!