Results 1 to 5 of 5
  1. #1

    Default pdo vs mysqli opinions

    anybody have any opinions on using pdo vs mysqli for the database interface?

    from what little i know, pdo seems to be something of a standard.

    one of my early complaints about php was that your code ends up completely "married" to mysql. with pdo (and maybe mysqli) it makes it far easier to switch databases on the chance that Oracle ruins mysql.

  2. #2
    Moderator wildjokerdesign's Avatar
    Join Date
    Jun 2003
    Kansas City Mo


    I don't really have an opinion on pdo vs mysqli but in regards of having your php married to your your mysql database, that should be avoidable if you use a MVC approach.

    For example both Zend Framework and CakePHP use this approach. Both are actually fashioned or modeled after Ruby. I think I mentioned via email that I am just starting to work with CakePHP, and while it can take some time to adjust to it does have it advantages. I've also dabbled some with Zend.
    Please remember your charity of choice:

    Handy Links: | Plain Text Editors: EditPlus | Crimson

  3. #3


    until i learn a framework, i want to come up with a clean way to interface php&mysql. honestly, i never liked the idea of embedding quotes and interpolated variables into strings!

    i come from an oracle & perl background. both have a cleaner interface. but even in the perl world, feeding parameters meant using the question-mark character as a bind parameter. when you are feeding 20+ variables as input parameters, trying to line up all those questionmarks became a real headache.

    however, php-pdo offers the best of both worlds, using "bind variables" - below i included an example from this link:

    this example really worked right out of the box for me - that was very impressive. also, i really like the use of "try" & "catch"


    /*** mysql hostname ***/
    $hostname = 'localhost';

    /*** mysql username ***/
    $username = 'username';

    /*** mysql password ***/
    $password = 'password';

    try {
    . . . $dbh = new PDO("mysql:host=$hostname;dbname=animals", $username, $password);
    . . . /*** echo a message saying we have connected ***/
    . . . echo 'Connected to database<br />';

    . . . /*** set the error reporting attribute ***/
    . . . $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    . . . /*** some variables ***/
    . . . $animal_id = 6;
    . . . $animal_name = 'bruce';

    . . . /*** prepare the SQL statement ***/
    . . . $stmt = $dbh->prepare("SELECT * FROM animals WHERE animal_id = :animal_id AND animal_name = :animal_name");

    . . . /*** bind the paramaters ***/
    . . . $stmt->bindParam(':animal_id', $animal_id, PDO::PARAM_INT);
    . . . $stmt->bindParam(':animal_name', $animal_name, PDO::PARAM_STR, 5);

    . . . /*** reassign the animal_id ***/
    . . . $animal_id = 3;

    . . . /*** execute the prepared statement ***/
    . . . $stmt->execute();

    . . . /*** loop over the results ***/
    . . . while($row = $stmt->fetch()) {
    . . . . . . echo $row['animal_id'].'<br />';
    . . . . . . echo $row['animal_type'].'<br />';
    . . . . . . echo $row['animal_name'];
    . . . }

    . . . /*** close the database connection ***/
    . . . $dbh = null;
    } catch(PDOException $e) {
    . . . echo $e->getMessage();

    side note: in my "catch" section, i send myself [or will be sending] an email of the specific error along with the __FILE__ and the __LINE__ value.

    finally, i find when the query is a union or left join, i am repeating bind variables quite frequently. using pdo, i dont need to be concerned about this anymore.
    Last edited by PhonemeAnalysis; 06-21-2010 at 11:51 AM.

  4. #4
    Moderator wildjokerdesign's Avatar
    Join Date
    Jun 2003
    Kansas City Mo


    Wow you work way to hard! I let the framework deal with the database code. We come at things from completely different angles.... I love it. The only drawback I can see from the above approach is the fact that you are tied to MySQL with this code. With most frameworks the code you actually "write" is universal and well fit most popular database languages. So you can create an app that well work with more then just MySQL. Now the drawback to my approach is that I don't understand the basics as well as you do which often gets me in trouble.
    Please remember your charity of choice:

    Handy Links: | Plain Text Editors: EditPlus | Crimson

  5. #5


    Frameworks are wonderful so long as you can play by the rules of the framework!

    but please note that using the php-pdo approach, you are actually moving far away from being tied to any particular database. Observe the string:


    to change databases, all one needs to do is to change the bolded text!

    this has been an option in perl for years now. one of my only complaints about php was your code ends up married to mysql forever. using the pdo approach makes the code and the database more independent.

    perl supports this approach and i believe that ruby does as well.

    but either way, the sheer beauty of this approach is using the "bind" variables. it makes feeding parameters to a complex query so much easier.

    but hey, i am looking forward to being introduced to frameworks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts