djm61
djm61

Reputation: 473

PDO Issue With No Error Message

I have the following snip of code:

error_reporting(E_ALL);
$hostname = "localhost";
$username = "user";
$password = "password";
try {
    $db = new PDO("mysql:host=$hostname;dbname=DBNAME", $username, $password);
    echo "connected...<br/>\n";flush();
    $sql = "SELECT COLA, COLB FROM TABLEA LIMIT 10";
    echo "query:$sql--<br/>\n";flush();
    $stmt = $dbh->query($sql);
    echo "statement:<pre>";print_r($stmt);echo "</pre>\nfetching...<br/>\n";flush();
    $result = $stmt->fetch(PDO::FETCH_ASSOC);
    if ($result === true) {
        echo "result is true<br/>\n";flush();
    }
    else if ($result === false) {
        echo "result is false<br/>\n";flush();
    }
    else {
        echo "what?<br/>\n";flush();
    }
    foreach ($result as $key=>$val) {
        echo "key:$key - val:$val--<br/>\n";flush();
    }
    echo "done";
    $dbh = null;
}
catch (Exception $e) {
    die ($e->getMessage());
}

As you can see, I have a pile of debugging echo statements, yet the only ones I see are:

connected...
query:SELECT COLA, COLB FROM TABLEA LIMIT 10

I'm was expecting to see my statement object being displayed as well as my results, am I missing something?

Upvotes: 0

Views: 1658

Answers (1)

brian_d
brian_d

Reputation: 11360

After you initialize the PDO object try setting the error mode higher.

$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

The default is PDO::ERRMODE_SILENT which will output no warnings/errors. With this default setting, you have to poll errorInfo() to see error details.

Upvotes: 5

Related Questions