EasyPDO

March 15th, 2010

This will be the last post in my blog regarding database connectivity. I’ve set up a site for my new project called EasyPDO. EasyPDO incorporates all of the ideas I’ve been working on over the last few years with the MySQLi and PDO database connectivity software.

EasyPDO currently supports connections to MySQL, SQLite and PostgreSQL databases. It provides a simple, secure and fast system for interacting with a database, and helps to eliminate the possibility of SQL injection attacks.

Will be interested to hear any feedback you may have.

7 Responses to “EasyPDO”

  1. T Says:

    I like it a lot, thanks for the update! One addition I’d like to make is a FetchArrays function that gets all results in a single array. What’s really cool about this wrapper is that because it interacts directly with PDO, the FetchArrays function is simply FetchArray with ‘fetchAll’ instead of ‘fetch’.

    public function FetchArrays($sql)
    {
    $this->Query = null;
    $this->Query = $this->PDO->prepare($sql);
    $args = func_get_args();
    array_shift($args);
    $this->BindParams($args);
    $this->Query->execute();
    if (EasyPDO::GetFetchMode() === EasyPDO::FETCH_MODE_NUMERIC_ARRAY)
    return $this->Query->fetchAll(PDO::FETCH_NUM);
    else
    return $this->Query->fetchAll(PDO::FETCH_ASSOC);
    }

    Thanks again!

  2. Bas Says:

    Congrats. It has become a very nice library. Did you implement a solution in this version for when the same query is executed multiple times in a row? This is still an essential feature I think, since prepared statements should have the option to be called multiple times, but with different parameters.

    T’s suggestion above is a useful one too. I find myself looping through the result set and assigning it to one result array quite often.

    I’m a little busy right now, but I’ll try to incorporate EasyPDO in an upcoming project. The previous version is powering a website which has gotten 10.000 registered users in two months. Everything is working like a charm.

    Regards,
    Bas

  3. RRP Says:

    Hi Bas,

    That bug always frustrated me! EasyPDO does indeed solve the problem, and I’m a lot happier with the code because of that.

    Thanks for your comments, and glad the original version is working well for you.

  4. RRP Says:

    Hi T,

    Thanks for your kind words, and for the additional function. I’ve always been hesitant to include this sort of functionality, mainly because unless you know beforehand how many rows your query will return, you may end up running out of memory if your result set is large.

    Whilst I’m yet to be convinced that it should put into the EasyPDO interface, I’ve added an additional public function to the EasyPDO class called FetchAll, based on your code. This provides the ability to return all results as an array of objects, associative arrays or numerically indexed arrays.New version (0.1.3) has been posted.

    Again – thanks for your suggestion. I’ve got a lot of documentation to write… :~

  5. Bas Says:

    Hi Rob,

    It seems the same query is still not working. First of all, the variable $this->LastSQL in the ExecuteSQL method couldn’t be found. If I add that to the class the method seems to quietly fail. This is when I execute the same query (an Update) with different parameters. I get no error message but the Updates are only performed for the first one. If I keep the bound parameters the same, all updates work fine.

    For example, I can execute this query multiple times if I keep index the same, but if I increment index on subsequent calls only the first update is executed.
    $db->ExecuteSQL(“UPDATE article SET lastModified=NOW() WHERE index=?”);

    Regards,
    Bas

  6. RRP Says:

    Hi Bas,

    The missing ‘LastSQL’ variable wouldn’t have caused this to fail, and should have just raised a warning. However, thanks for spotting that, and I’ve added it back to the class, and uploaded a new version with this fix in place.

    I cannot reproduce the problems you are seeing regarding the ExecuteSQL function, however. I’ve done the following (using the MySQL driver):
    – MySQL
    CREATE TABLE article (ID INT NOT NULL AUTO_INCREMENT, lastmodified DATETIME NOT NULL, PRIMARY KEY (ID)) ENGINE=InnoDB;
    INSERT INTO article (lastmodified) VALUES (NOW()), (NOW()), (NOW()), (NOW()), (NOW()), (NOW()), (NOW()), (NOW()), (NOW()), (NOW());

    // PHP
    for ($i = 1; $i < = 10; $i++)
    $db->ExecuteSQL(‘UPDATE article SET lastmodified = NOW() WHERE ID = ?’, ‘i’, $i);

    This code is working fine for me. I’ve set up a quick test page to demonstrate this – see http://easypdo.robpoyntz.com/test.php. Full source code is included on the page itself.

    Please let me know if you’re still having problems and I’ll see if I can help.

    Cheers,
    Rob

  7. Seranmephisto Says:

    hmm, kind words… Well, I am happy to provide some here too. I haven’t touched PHP/mysql(i)/Apache in my case in hmm 8 years; getting back now. Also getting back to the css/html/jscript (jQuery .. wow what a nice lib) etc etc. And I can say this. Thank you so much for so much useful code, ideas, considerations and providing it with a useful license.

    I am a perfectionist and even if I can be considered definitely a ‘noob’ in much of these things.. I also am well aware of where my coding often has limits (with the correct research of topics of course). However, being aware of all my shortcomings and trying to write everything from scratch (no way should one EVER just copy and paste things from internet and think hey.. I am done.. sure it’s ok for getting started but any system one will rely on saying is production worthy, has to be understood by the author hehe). Anyway, past two weeks, my DB classes, sql injection preventions , validations really were the work of an inexperienced coder and I could see it. Was getting frustrating.. but then finding your blog, really shows me many ideas, tips and ‘thank you hallelujah’ examples (eg. I am playing with making a puzzle to enter my site.. sure it’s easily circumvented ..it is *not* meant to be a kind of login thing .. but a novelty for first time users to my site.. like a ‘skip intro’ kind of thing). Now, your fading jQuery thingy for example, helps me with ideas for how i make some trannsitions in the puzzle. I been playing with gimp , making animated gif’s hihi (given .swf for it’s size seems to be fairly insecure for xss attacks for a new coder as myself).
    Anyway.. the point I wanted to make withi this ramble was just to really give you a really appreciative ‘heads-up’…

    Thanks a lot Rob.. when people who have their proven track record and are less hmm protective of their skills as they are well established; it gives a lot of wonderful yaaay for newcomers.

    have a nice one!

    micca

Leave a Reply