ezSQL Database Library (Improved!)

9 comments

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:

$sql->error(); // Get the error string
$sql->errno(); // Get the error number

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:

$sql->quick_select();
$sql->quick_update();
$sql->quick_insert();

// quick_select() example:
$columns = array('column1', 'column2');
$sql->quick_select('table_name', $columns, 'LIMIT 10');

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:

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);

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.

You can download it from here

Share and Enjoy:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • HackerNews
  • Netvibes
  • Reddit
  • StumbleUpon
  • Twitter
  • Yahoo! Buzz

Written by Nabeel

February 3rd, 2009 at 11:52 am

Posted in Fun, General, Projects, php

9 Responses to 'ezSQL Database Library (Improved!)'

Subscribe to comments with RSS or TrackBack to 'ezSQL Database Library (Improved!)'.

  1. 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::$DB variable by self::instance()->DB. This is my modified DB class: http://pastebin.com/fa188f8

    Now, 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

    13 Feb 09 at 7:19 am

  2. @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.

    Nabeel

    13 Feb 09 at 5:35 pm

  3. 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

    23 Feb 09 at 5:01 pm

  4. @Richard
    Well, it would be great if the link worked! The download gives a 404!

    Richard

    23 Feb 09 at 5:03 pm

  5. @Richard
    Sorry about that! Moved servers recently, mistyped the download link! Should be workin’ now

    Nabeel

    23 Feb 09 at 10:45 pm

  6. 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

    29 Jun 09 at 6:29 pm

  7. @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!

    Nabeel

    29 Jun 09 at 7:01 pm

  8. My last note to put in your comments is that your link above to github is a 404 now.

    Richard

    1 Jul 09 at 2:34 pm

  9. Yes, if you view my latest post, it’s changed

    Nabeel

    1 Jul 09 at 2:35 pm

Leave a Reply