Jump to content

[SOLVED] Moving to PDO


barrywood

Recommended Posts

So far I've done all my PHP coding using the mysql_* functions in PHP. I'm converting my code over to use the PHP PDO class so that if I ever need to use something other than MySQL it won't be too painful.

 

My question concerns the error reporting. I'm doing an UPDATE using the PDO prepared statement functions:

 

$stmt = $dbh->prepare($query);
$stmt->bindParam(':param', $paramVal, PDO::PARAM_STR);
$stmt->execute();

 

I've found that if the query fails for a reason (such as the WHERE clause specifying a record that doesn't exist) there will be no exception thrown. To detect that type of error it seems I've got to check to see if there were any rows affected:

 

if ($stmt->rowCount() == 0) (update failed here)

 

In this case I can't seem to find any information about the error.

 

I've tried using $stmt->errorCode() and $stmt->errorInfo() but both of those are not showing any error info even when I induce one.

 

Am I going about this the right way, and if so, is there any way to log the reason for this type of error?

 

Link to comment
Share on other sites

Yes, an insert failing would definitely be an error that would throw an exception.

 

 

By the way, if you did want your application to error on an update statement that doesn't affect any rows, you could always just throw an exception your self, or catch it in an if clause and do something.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.