PDO connection/query script

Discussion in 'PHP' started by Tony Brar, Dec 9, 2012.

  1. #1
    Hi guys,

    I'm looking to convert my entire site from mysql_* functions to PDO.
    Unfortunately, I'm very new to PHP, and I had to learn objects and try/catch for this.
    Also unfortunately, I learned from W3Schools, proudly offering incomplete information in all web development technologies. (see w3fools.com)
    So here I am.
    I've been looking through the PHP manual for Objects and PDO, and came up with this script:
    
    <?
    try {
       $dbh = new PDO('mysql:host=localhost;dbname=*******','*******','*******',array(PDO::ATTR_PERSISTENT => true));
       $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }
    catch (PDOException $e) {
       die('Error:' . $e->getMessage() . '<br />');
    }
    try {
        $dbh->beginTransaction();
        $dbh->exec("query goes here");
        $dbh->commit();
    }
    catch (PDOException $e) {
        $dbh->rollBack();
        die('Error:' . $e->getMessage() . '<br />');
    }
    ?>
    
    PHP:
    Anything wrong with this script?
    How could it be improved? (there's got to be something:))

    Thanks,
    -Tony
     
    Tony Brar, Dec 9, 2012 IP
  2. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #2
    Just something from experience. Your code looks like "Indian English" sounds. It's understandable (to me and to PHP), but

    
    <?
    try {
       $dbh = new PDO('mysql:host=localhost;dbname=*******','*******','*******',array(PDO::ATTR_PERSISTENT => true));
       $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }
    catch (PDOException $e) {
       die('Error:' . $e->getMessage() . '<br />');
    }
    try {
        $dbh->beginTransaction();
        $dbh->exec("query goes here");
    }
    catch (PDOException $e) {
        $dbh->rollBack();
        die('Error:' . $e->getMessage() . '<br />');
    }
    $dbh->commit();
    ?>
    
    PHP:
    is more "colloquial" PHP. (The commit won't fail if nothing before it did, unless the database crashes, in which case your code can't do anything anyway.)

    BTW, transactions are normally used with multiple queries. IOW, if you update table1 and table2, but the update to table2 fails, you want the update to table1 to be rolled back, so you don't end up with partial data, which will probably lead to weird results (an invoice for no items, for example, or items sold with no invoice). With a single query, whether enclosing it in a transaction will make any difference depends on the database. (RDB7, for example, should always be run with transactions, even for SELECT queries.)
     
    Rukbat, Dec 9, 2012 IP
  3. Tony Brar

    Tony Brar Active Member

    Messages:
    220
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    75
    #3
    Alright. I'll move the commit().
    I know that you usually use multiple queries, I'm gonna do that to.

    Thanks,
    -Tony
     
    Tony Brar, Dec 11, 2012 IP