PHP PDO execute() .... somewhat confused ?
Results 1 to 4 of 4

Thread: PHP PDO execute() .... somewhat confused ?

  1. #1
    PHP Lover
    Join Date
    Oct 2009
    Location
    Wales
    Posts
    15

    Question PHP PDO execute() .... somewhat confused ?

    Hi,

    Below i have some sample code and am confused over execute(), i have the code below in a try and catch block, in the catch block i call a function i created to log any error that is caught in catch block to a .txt file.

    I then looked online and it seems that i should do an if statement check on execute to ensure it executed the query, the part that confuses me if the execute failed i thought it would be caught in the catch block but it seems that is not the case.

    To explain better i have commented the code in depth on the area that i am confused about.

    Any help in making me understand would be great. I have not included all code above try and catch to keep things simple

    PHP Code:
    <?php

    try {
            
    // connect to database
            
    $dbh sql_con();

            
    // checke if username exists on users table or users banlist table
            
    $stmt $dbh->prepare("
            SELECT
              users.user_login
            FROM
              users
            WHERE
              users.user_login = ?
            UNION ALL
            SELECT
              users_banlist.user_banlist
            FROM
              users_banlist
            WHERE
              users_banlist.user_banlist = ?"
    );
            
            
    // this is the part i am confused with, why is it i would use an if statement on execute() ?
            // i thought using a try and catch block any errors would be caught in the catch block.
            // using an if statement to check if execute() worked, i thought if execute failed it would
            // be handled by the catch block, i mean in my exmaple code here, what could cause the execute to fail ?
            // and why if execute failed it would not be caught by catch block ?
            // i am looing at exmaple online and i am reading different things and its all confusing me
            
            
            // execute query
            
    if(!$stmt->execute(array($username$username))){
                echo 
    'something went wrong .. ';
            } else {
                
    // execute worked
                
    }
        } 
    // if any errors found log them  in my ExceptionErrorHandler() function and display friendly message
        
    catch (PDOException $e) {
            
    // this function catches an error and logs them to file
            
    ExceptionErrorHandler($e);
            require_once(
    $footer_inc);
            exit;
        }
        }

    ?>
    Thanks or any help!

  2. #2
    High Energy Magic Dept. NogDog's Avatar
    Join Date
    Aug 2006
    Location
    Ankh-Morpork
    Posts
    14,478
    PHP Code:
    $dbh->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION); 
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  3. #3
    PHP Lover
    Join Date
    Oct 2009
    Location
    Wales
    Posts
    15

    Question

    Hi,

    Thanks for replying. I already have it set to the following in my sql_con() function which establishes connection to database:
    PHP Code:
    $dbh->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION); 

    My confusion is in my try block i try to check if a username exists on the users table and user ban list table, it then gets executed using execute(), now my confusion is why would i use an if statement around the execute() as shown in my code ?

    I thought if the execute() failed it would be caught in the catch block, but seeing examples online i see people wrap the execute() in an if statement as illustrated in my code.

    and back to my questions:

    1) In example code, why is it i would use an if statement on execute() ? (if it failed to execute shouldn't it be caught in the catch block ? )

    2) What could cause the execute to fail ?

    3) and why if execute failed it would not be caught by catch block ?

    I am confusing myself somewhere and am not quite getting it, hence my questions.

    Basically anything caught in the catch block in example code is written to a log file, in example code in catch block i call my function ExceptionErrorHandler($e);, this function writes any error to file.

    Hopefully i have explained better.

    Thanks for trying to help.

  4. #4
    High Energy Magic Dept. NogDog's Avatar
    Join Date
    Aug 2006
    Location
    Ankh-Morpork
    Posts
    14,478
    Looks like it's because the default behavior for MySQL is to emulate prepared statements, which then seems to short-circuit the error-to-exception process. You can tell it not to do this, though it may cost some in terms of performance:
    PHP Code:
    <?php
    try {
        
    $dbh = new PDO('mysql:dbname=test;host=localhost''foo''bar');
        
    $dbh->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION);
    // this line:
        
    $dbh->setAttribute(PDO::ATTR_EMULATE_PREPARESfalse);
        echo 
    "<p>Test 1:</p>\n";
        
    $sql "SELECT 'foo'";
        
    $stmt $dbh->prepare($sql);
        
    $stmt->execute();
        echo 
    "<p>Test 2:</p>\n";
        
    $sql "SELECT * FROM bogus_table_name";
        
    $stmt $dbh->prepare($sql);
        
    $stmt->execute($sql);
    }
    catch(
    PDOException $e) {
        echo 
    "<pre>".print_r($e1)."</pre>";
    }
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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