barrywood Posted November 2, 2009 Share Posted November 2, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/179904-solved-moving-to-pdo/ Share on other sites More sharing options...
corbin Posted November 2, 2009 Share Posted November 2, 2009 If an UPDATE statement doesn't affect any rows, that's not an error. It may not happen how you expect it, but as far as the MySQL engine goes (and PDO), that is not considered an error. Quote Link to comment https://forums.phpfreaks.com/topic/179904-solved-moving-to-pdo/#findComment-949052 Share on other sites More sharing options...
barrywood Posted November 2, 2009 Author Share Posted November 2, 2009 That makes perfect sense. I suppose I won't run into the same thing with an INSERT because if that didn't affect any rows then there will likely to be an exception generated. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/179904-solved-moving-to-pdo/#findComment-949072 Share on other sites More sharing options...
corbin Posted November 2, 2009 Share Posted November 2, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/179904-solved-moving-to-pdo/#findComment-949080 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.