ezSQL Database Library (Improved!)
Note: I’ve made a dedicated page for ezSQL-related items, you can view it here
I’ve been using ezSQL for a long time, as my “database driver”. It’s an awesome little class, that you can easily use to get database results, and return in different formats. I’ve made a bunch of changes and updates to it, so I thought I’d put them out there and share them.
Along the way, I’ve made some updates and changes to it, to suite my requirements, including format PHP5 support. I’ve updated the error handling/logging, so now you can call:
[php]
$sql->error(); // Get the error string
$sql->errno(); // Get the error number
[/php]
Respectively after queries to return the “real” status of a query. the debug() function has been added to, so at call time, you can pass to bool whether to display the result on the screen, or pass it back as a string
I’ve also added several utility functions:
[php]
$sql->quick_select();
$sql->quick_update();
$sql->quick_insert();
// quick_select() example:
$columns = array(‘column1′, ‘column2′);
$sql->quick_select(‘table_name’, $columns, ‘LIMIT 10′);
[/php]
To make it easier to do simple SELECT’s and UPDATEs, and INSERTs.
Another thing it was missing with MySQLi support, so that has been added (though I have not had the time to add support for statements (though that can be accessed rather easily). I had added MSSQL support, but the file has gone AWOL (Icreated and used it for a specific project a while ago). I will update when I get a hold of it.
And another thing I added was a static interface class, which I use all the time on PHP5 projects:
[php]
DB::init(‘mysql’);
DB::connect(‘username’, ‘password’, ‘database_name’, ‘localhost’);
// Now anywhere in your script:
$row = DB::get_row(‘…’);
// Or
$results = DB::get_results(‘…’, ARRAY_A);
[/php]
Makes it much easier to access the database function, without having to do $db = DB::getInstance(), in every single function to get a singleton object of the database. Since that’s for PHP5 only, the whole library has been updated for PHP 5 support, with public/private functions and constructors/destructors as well.
I also condensed it to just one include() now, just keep all the files in the same place (include ‘…/DB.class.php’). From that you can either use the static class, or declare a new ezSQL object.
I also added in phpDoc blocks on all the functions, since IntelliSense is awesome and really helpful.
I hope these improvements make it easier for everyone. Of course, the original credit goes to Justin Vincent. There are some docs and examples here as well.
Hello!
This “upgrade” + an _autoload() function works great!
But… I have an question, I’m starting to use the Singleton pattern and I have a Singleton class for use with other sub-classes. What I did is add an method instance() to your DB class, that calls getInstanceOf() of singleton and returns an instance of the class DB and I change the
self::$DBvariable byself::instance()->DB. This is my modified DB class: http://pastebin.com/fa188f8Now, It’s right this modification? I think that I need add an condition, like
if (self::instance()->connected != TRUE), and thus prevent that init method is executed several times, What think you?I hope you help me, thanks!
@jesus
Hey,
I see what you’re trying to do, but IMO, a singleton parent class is overkill.
You can just do:
public static function getInstance() { return self::$DB; }
To return a singleton instance. That’s the simplest way.
I already check to make sure it’s initialized (the if(!self::instance()->DB = …) in your code) but just checking that it’s not null. If it connected properly, then it’ll return an instance of the ezSQL class.
Wow… I had been thinking how awesome it would be if ezsql had mysqli support and phpdoc style comments added in. This is great!
@Richard
Well, it would be great if the link worked! The download gives a 404!
@Richard
Sorry about that! Moved servers recently, mistyped the download link! Should be workin’ now
I’ve noticed you’re working on this on github now. Is there a newer version? I used your class successfully once or twice, and then started running into errors, and haven’t looked into it since.
@Richard – Yes, there’s bug fixes and all the like now. Github has the Issues page as well, so if you come across anything, I can fix it and distribute it much more easily now. Thanks!
My last note to put in your comments is that your link above to github is a 404 now.
Yes, if you view my latest post, it’s changed
i downloaded and created an example however it will not connect to the db. And when I look at the DB class it in turn tries to create an ezMySQL object before it connects to the db. I am a beginner. I think the db class should connect to the DB first. then you should create the SQL object.
public static function init($type=’mysql’)
{
if($type == ‘mysql’ || $type == ”)
{
if(!self::$DB = new ezSQL_mysql())
{
echo ‘error initializing’;
self::$error = self::$DB->error;
self::$errno = self::$DB->errno;
return false;
}
doesnt generate an error but it should because it is expecting DB connection data which is never passed upon calling init.
just a beginners 2 cents.
here is my example that doesnt work.
DB::init(‘mysql’);
DB::debug();
DB::connect(MYSQLHOST,MYSQLUSER,MYSQLPASSWORD,MYSQLDB);
$fields = array(‘form_id’, ‘name’);
$results = DB::quick_select(‘class_form_elements’, $fields, “form_id=0″);
DB::debug();
if($results)
{
foreach($results as $row)
{
echo $row->name;
}
}
oh and I changed the order of the params in the class to match above so that isnt the issue.
Hi Mike,
Here is my example of usage, I need to update the docs on github to show, but the purpose of creating the class first is for the debug messages.
http://github.com/nshahzad/phpVMS/blob/master/core/codon.config.php#L84
i found the problem i was sending constants instead of vars