Using die() with forced MySQL errors to test error capture

I’m reposting and separating my two questions with a new post.

I’m trying to add an error array directly to the die() exception.


$dbSuccess_3 = mysql_query($insertPermsn,$connectID) or die (($Errors[]="ERROR - Unable to save").error_get_last().mysql_error($connectID)); 

I can’t find a lot of documentation on the die() exception. Will someone tell me if the above code snippet should work? And how can I force an error in one MySQL insert with the server (I’m doing three inserts. I know how to test one possible error where there is no DB or table. Any other useful forced errors I can use)?

Chris

If you believe your script will encounter multiple errors that you want to capture, leave the die(); out of each of the attempts, attempt to process all database interaction before output (or ob_start), and then

if(count($Errors) > 0) { 
  mysql_query("Insert INTO Errors('thewords') VALUES ('".implode("'),('",$Errors)."')",$db);
   die("Errors Occured. Aborting.");
}

@SXTX, To answer your question, I’m working on an error array for an insert command. It just makes sense to capture the error at the closest point to the generation.

Since I want the error to originate from the MySQL server, I’ll try the “Key violation”
Cheers!

@StarLion, The errors are not saved, only reported using the error array in much the same way you’re showing an insert. IF ($errors < 0) THEN, print $error;. And, I understand the point of not using the die() command with the insert. Capture the last error with an IF statement. I like it!

I suppose the need for the exit command on the mysql insert is to close the connection?

C

Yes that sure makes sense and it’s a good idea to that. However:

(1) Once you call die() or exit() PHP stops. This means you have no way of doing anything with the array you created, since you cannot perform any statements anymore

(2) You can overcome (1) by doing the following:


$dbSuccess_3 = mysql_query($insertPermsn,$connectID);
if ($dbCusccess_3 === false)
{
  $Errors[]="ERROR - Unable to save: ".error_get_last().', '.mysql_error($connectionID);
  // Insert error in DB here
  die();
}

Of course if the first query can’t be performed because MySQL went away, the second query can’t be performed either.

No it won’t. die() is an equivalent of exit() and only takes one parameter: a string or an integer (as exit status for CLI). Why do you want to set an array in die() anyway? After you’ve stopped PHP with die() there is no way to access that array anymore …

From the top of my head:

  • Syntax error: type gibberish for a query
  • Key violation: insert a row with a primary key that already exists for example
  • (InnoDB only) Foreign key violation
  • Shutdown MySQL and then query