...

View Full Version : Error handling for my SQL queries



chornbeck
05-08-2006, 03: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!

cdwhalley.com
05-08-2006, 07: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()...

chornbeck
05-08-2006, 08: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?

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

Will do what you're asking for.

chornbeck
05-08-2006, 08: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?

GJay
05-08-2006, 08: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.

[edit]
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';
}

chornbeck
05-08-2006, 09: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...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum