Reputation: 1086
I'm just converting a site to PDO and this is my first exposure to it, but I'm having difficulty obtaining any kind of useful error information from PDO. I've read numerous other posts on this subject but none of them seem to provide a solution which works for me.
Here is the code:
try {
$dbh = new PDO("mysql:host=".CO_DB_HOST.";dbname=".CO_DB_NAME, CO_DB_UNAME, CO_DB_PWORD);
$dbh ->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
}
catch(PDOException $e) {
echo $e->getMessage();
}
function addRec($data) {
global $part_id, $dbh;
$part_id++;
try {
$sth=$dbh->prepare("
INSERT INTO
non_existent_table
(
name
) VALUES (
?
);
");
for ($i = 0; $i < count($data); $i++) {
$sth->execute($data[$i]);
}
}
catch(PDOException $e) {
echo "Something went wrong. Please report this error.";
file_put_contents('/PDOErrors.txt', PDOStatement::errorInfo(), FILE_APPEND);
}
}
// Insert a database record
$data = array(
array(
'joe blogs', /* name */
)
);
addRec($data);
The table does not exist but I'm getting no error in PDOError.txt and nothing in my httpd error log. Can someone shed some light on what I'm doing wrong please? Thanks.
Upvotes: 0
Views: 539
Reputation: 164871
errorInfo()
is not a static method. Also, it returns an array, not a string.
You should be fine with the information in the exception. Use something like this instead
file_put_contents('/PDOErrors.txt', $e->getMessage(), FILE_APPEND);
For extra information, you could also append the stacktrace.
Edit
Are you sure /PDOErrors.txt
is the correct file path for your error log? That's in the filesystem root which I doubt is what you want or even possible.
Upvotes: 2