View Full Version : Error handling for my SQL queries

05-08-2006, 04:45 PM
I'd like to set up some custom error handling for my php scripts.

I see the string "or die(mysql_error());" at the end of each of my queries, so I figure there's a way to define a global error handling variable that can get put into this statement, I'm just not sure how to do it..

I just want to insert a line that tells the user of the error, and provides a link to the original page, so they can try again...

Any help is greatly appreciated!

05-08-2006, 08:04 PM
Firstly, what do you mean by 'global error handling variable'?
To put a link to let them go back to where they were previously, just put:

"<a href=\"previous_page.php\">Back</a>"

as part of the parameter for the die()...

05-08-2006, 09:18 PM
I mean defining something like

$error = echo 'Error connecting to the DB, click <a href="http://***>HERE</a> to return to the home page';

and then using that in this sort of way

or die(mysql_error($error));

or something to that effect.. Is this possible, and if so, what's the proper syntax?

05-08-2006, 09:32 PM
$rs=mysql_query($query) or die($error);

Will do what you're asking for.

05-08-2006, 09:49 PM
So all I have to do is replace the mysql_error() with the $error variable that I defined and I'm good to go?

05-08-2006, 09:53 PM
yes, although while developing whatever it is you're making, it'd be sensible to keep the mysql_error there, so you've got a better idea of what's happened should thnings go wrong.

To this effect, you could have something like:

$rs=mysql_query($query) or die(handle_error());

function handle_error() {
gloabl $debug;
echo ($debug)?mysql_error():'nice error message with link';

05-08-2006, 10:20 PM
Actually, I'm done debugging it, it's ready to release to the public, I just need some kind of fancy message in case the MySQL DB craps itself one day...