Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how can i trap mysql error messages with php?

Status
Not open for further replies.

miahmiah

Programmer
Oct 1, 2002
9
US
does anyone know how to trap error messages that mysql generates, when coding with php? i want to stop mysql from printing to a page its error messages (like the "0 is not an index" error for a bad database connection attempt), and instead create user friendly messages and options.

 
well, it's rather easy

to disable the error mesages just put "@" in front of the commands

to trap the messages and echo them "user friendly" do something like this

@$myconnection = mysql_connect(...);
if (!$myconnection) ErrorPage(__LINE__, __FILE__, mysql_error());

function ErrorPage($eline, $efile, $emessage)
{
echo '<b>Error</b><br>';
echo 'File: '.$efile.'<br>';
echo 'Line: '.$eline.'<br>';
echo 'Error: '.$emessage.'<br>';
exit();
}
 
I use this function in my php pages:
//-----mysql error function---------------
function mysql_checkerror(){

$err_no=mysql_errno();

if ($err_no > 0 ){

echo &quot;<h2> Error:</h1> &quot; . mysql_errno() . &quot;: &quot; . mysql_error() . &quot;<br>\n&quot;;

exit;

}
}
//------------------------------------------------
and call it like this:
--------some php page-----------------------------
$connection=@mysql_connect($host,$user,$password) ;

mysql_checkerror();

All mysql errors are reported by an output code, anything greater than 0 is an error which has set text to describe the problem, all I do is return that code.
This stops the page from being overrun with the usual mysql spam when things aren't quite right. ______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top