I'd like to trap mysql warnings and log them as part of my debugging process. However mySQL warnings just print to screen. I'd appreciate if someone knows how trap warnings along with whatever other information is available and prevent users from seeing the warning.


Thanks.
David

Can you show a code sample, so we can see what you are doing ? Are you calling mysqldump or something similar ?

Hi David,

I use the following function to hide errors from users, write them to a log file and send me an email. You would need to replace the log writing, alerting and emailing functions withn your own.

<?php
function error_handler($errno,$error,$file,$line) {
	$msg  = "<h2>PHP Runtime Error:</h2>";
	$msg .= "<p>Error $errno - $error</p>";
	$msg .= "<p>File: $file Line: $line</p>";
	$msg .= "<h2>Request variables:</h2>";
	$msg .= nl2br(var_export($_REQUEST,true));
	$msg .= "<h2>Session variables:</h2>";
	$msg .= nl2br(var_export($_SESSION,true));
	$msg .= "<h2>Server variables:</h2>";
	$msg .= nl2br(var_export($_SERVER,true));
	$log = str_replace(array("<br/>","<br />","<p>","</p>","<h2>","</h2>"),"\n",$msg);
	$log = str_replace("&nbsp;&nbsp;","\t",$log);
	//$logged = utility::writeLog(array("errors"),date("Ymd-His"),$log,"wb");
	$logged = utility::writeLog(array("errors"),"error",$log,"wb");
	//$mailed = utility::SwiftMail(array("clients@nettsite.co.za"),"Error on {$GLOBALS['client']} website",$msg);
	$mailed = true;
	if (empty($_SESSION['maybe_in_a_loop']) && $logged && $mailed) {
		$_SESSION['maybe_in_a_loop'] = true;
		utility::alert("An error has occured - you are being redirected to the home page.",true);
	}
	else {
		unset($_SESSION['maybe_in_a_loop']);
		if (!$mailed || !$logged) exit($msg);
		else utility::alert("Sorry, we cannot recover from the error. We have informed the system administrator.",false);
		exit();
	}
}
$old_error_handler = set_error_handler("error_handler");
?>

Hope that is of some use.

Can you show a code sample, so we can see what you are doing ? Are you calling mysqldump or something similar ?

There is no specific sample code to show. For example if a user enters a field that passes through my validity checking but generates a mysql warning that warning is just passed on to the web page. I am capturing errors if mysql triggers an error but that is not the same as a warning.

So the question is, 'how do I trap for mysql generated warnings?', so I can take corrective action.

David

Hi David,

I use the following function to hide errors from users, write them to a log file and send me an email. You would need to replace the log writing, alerting and emailing functions withn your own.

<?php
function error_handler($errno,$error,$file,$line) {
	$msg  = "<h2>PHP Runtime Error:</h2>";
	$msg .= "<p>Error $errno - $error</p>";
	$msg .= "<p>File: $file Line: $line</p>";
	$msg .= "<h2>Request variables:</h2>";
	$msg .= nl2br(var_export($_REQUEST,true));
	$msg .= "<h2>Session variables:</h2>";
	$msg .= nl2br(var_export($_SESSION,true));
	$msg .= "<h2>Server variables:</h2>";
	$msg .= nl2br(var_export($_SERVER,true));
	$log = str_replace(array("<br/>","<br />","<p>","</p>","<h2>","</h2>"),"\n",$msg);
	$log = str_replace("&nbsp;&nbsp;","\t",$log);
	//$logged = utility::writeLog(array("errors"),date("Ymd-His"),$log,"wb");
	$logged = utility::writeLog(array("errors"),"error",$log,"wb");
	//$mailed = utility::SwiftMail(array("clients@nettsite.co.za"),"Error on {$GLOBALS['client']} website",$msg);
	$mailed = true;
	if (empty($_SESSION['maybe_in_a_loop']) && $logged && $mailed) {
		$_SESSION['maybe_in_a_loop'] = true;
		utility::alert("An error has occured - you are being redirected to the home page.",true);
	}
	else {
		unset($_SESSION['maybe_in_a_loop']);
		if (!$mailed || !$logged) exit($msg);
		else utility::alert("Sorry, we cannot recover from the error. We have informed the system administrator.",false);
		exit();
	}
}
$old_error_handler = set_error_handler("error_handler");
?>

Hope that is of some use.

Thanks, I will give this technique a try. Do you know if I can set the error_handler to trap for 'warnings' only? I am already using exception handling to deal with errors.

This is also a common method:

$result = @mysql_query(..);
if (! $result)
  echo mysql_error();

The @ makes sure the code continues and does not output a warning to the page.

Hi David,

I use the following function to hide errors from users, write them to a log file and send me an email. You would need to replace the log writing, alerting and emailing functions withn your own.

<?php
function error_handler($errno,$error,$file,$line) {
	$msg  = "<h2>PHP Runtime Error:</h2>";
	$msg .= "<p>Error $errno - $error</p>";
	$msg .= "<p>File: $file Line: $line</p>";
	$msg .= "<h2>Request variables:</h2>";
	$msg .= nl2br(var_export($_REQUEST,true));
	$msg .= "<h2>Session variables:</h2>";
	$msg .= nl2br(var_export($_SESSION,true));
	$msg .= "<h2>Server variables:</h2>";
	$msg .= nl2br(var_export($_SERVER,true));
	$log = str_replace(array("<br/>","<br />","<p>","</p>","<h2>","</h2>"),"\n",$msg);
	$log = str_replace("&nbsp;&nbsp;","\t",$log);
	//$logged = utility::writeLog(array("errors"),date("Ymd-His"),$log,"wb");
	$logged = utility::writeLog(array("errors"),"error",$log,"wb");
	//$mailed = utility::SwiftMail(array("clients@nettsite.co.za"),"Error on {$GLOBALS['client']} website",$msg);
	$mailed = true;
	if (empty($_SESSION['maybe_in_a_loop']) && $logged && $mailed) {
		$_SESSION['maybe_in_a_loop'] = true;
		utility::alert("An error has occured - you are being redirected to the home page.",true);
	}
	else {
		unset($_SESSION['maybe_in_a_loop']);
		if (!$mailed || !$logged) exit($msg);
		else utility::alert("Sorry, we cannot recover from the error. We have informed the system administrator.",false);
		exit();
	}
}
$old_error_handler = set_error_handler("error_handler");
?>

Hope that is of some use.

I implemented this concept and it traps everything without posting warnings that the user can see. Thanks.

Pleasure, glad to be of help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.