Okay, I am stumped with this one. I have a table in my database where I cannot seem to delete rows via PDO (I have noticed this behaviour for a few weeks now, it was working
The PDO::exec()
function returns the number of affected rows, including 0 if no rows are affected.
A line like this will die()
because exec
will return 0
which is interpreted as boolean false.
$dblink->exec("UPDATE `sometable` SET `somecolumn`=0 WHERE `somecolumn`=0") or die("Never use die for error handling.");
The best error handling practice for PDO is to use PDO exceptions. Enable PDO exceptions (of PDOException class, see docs) like this:
//enable Exception mode (uncaught exceptions work just like die() with the benefit of giving you details in logs of where execution was stopped and for what reason)
$pdoDBHandle->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
Remove or die()
and exit();
and enable exception mode. I bet this will fix your "weird" problem. Also take a look at throwing Exceptions in PHP, even with procedural code (to replace die()
and exit()
.
BTW exit
stops execution just like die
, except it is usefull in CLI mode because it returns a success/error code to the operating system. It really isn't meant for error handling.