I have created a MYSQL abstraction layer that I am pretty proud of.
Here are some of the features:
- Maintain only one persistent connection to database
- Can create multiple stored connections at runtime
- Error Logging
- Select between 3 return type with each query, including all new iObject
- iObject (improved Object) has helper methods, and is extensible via Plugin
- Escape function for variables/array before using them in query
- Get results in xml or html format, great for reports/test
What I am most happy about is the iObject return style, here is a demo.
// get instance
$DB = DB::getInstance('users');
// escape string
$rating = $DB->prepare($_POST['rating']);
// do query
$result = $DB->query("SELECT * FROM sites WHERE rating='$rating'");
// echo html output
echo $result->asHTML();
//echo xml output
echo $result->asXML();
//echo num rows
echo $result->num_rows;
// echo affected rows
echo $result->affected_rows;
// Fetch rows where domain = flicker
$row = fetchRow('domain','flicker',1);
// cycle through rows returned
foreach($result->rows as $row){
// echo htmlspecialchars escaped value from column
echo $row->escape('url');
}
To imaging the power of reporting, this code here
$DB = DB::getInstance();
$r = $DB->query('SELECT * FROM members',"OBJ");
echo $r->asHTML();
Will output this here -
To see more, and grab the sourcecode, go here.



May 19th, 2008 at 8:59 am
very cool…