PHP Friendly SQL Error message
No matter how much you try there will always be the day when you make a mistake in your sql accessing the database.
Sometimes queries are based on some sort of session which, if it times-out the next query to the database will show an error instead of getting the result expected. Sometimes the query comes from an old page in the Search Engines, the possibilities are unlimited.
Showing a Sql error to the visitor, could mean a lost customer, giving out too much details that are not meant to be seen, etc…
The biggest problem is “How do you know you have made a mistake?”
The solution is to send yourself an email when that happens and instead of the error message displayed on the page, you can write a nice message to the visitor that he will accept quicker then “The page can not be displayed“.
Not many visitors knows how to go back to your Home Page if there are no links that will redirect them there.
There is a simple example bellow that will do just that.
function ewd_db_connect($HOST,$USER,$PASS,$DB) {
$conn = @mysql_connect($HOST, $USER, $PASS) or die(ewd_error($sSql="No Connection to the database"));
@mysql_select_db($DB) or die(ewd_error($sSql="No Connection to the database"));
return $conn;
}
function ewd_db_close($conn){
mysql_close($conn);
}
function ewd_query($strsql,$conn) {
$rs = mysql_query($strsql,$conn);
return $rs;
}
function ewd_num_rows($rs) {
return @mysql_num_rows($rs);
}
function ewd_fetch_array($rs) {
return mysql_fetch_array($rs);
} function ewd_free_result($rs) {
@mysql_free_result($rs);
}
function ewd_data_seek($rs,$cnt) {
@mysql_data_seek($rs, $cnt);
}
#this is where the fun begins
#if we get a sql error, will show a friendly message then email the error to the webmaster or yourself
function ewd_error($sSql) {
echo "We have encounter a problem accessing the database. An email has been send to the Administrator.\n
We hope to bring the website back on line very soon.\n
To go back to the home page Click Here (link to your home page)";
#send email
$to = "your_email_address";
$subject = "Error on Website name";
$headers = "Content-type: text/plain\r\n";
$body = "Connection Error:". mysql_error();
$body.="\n\n\nSql Error: ".$sSql;
$body.="\n\n\nPage: ".$_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING'];
$body.="\n\n\nReferer".$_SERVER['HTTP_REFERER']."\n\nIP Address: " .$_SERVER['REMOTE_ADDR'];
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
ini_set("SMTP","localhost");
ini_set("smtp_port","25");
}
#send email
mail($to, $subject, $body, $headers);
}
//define your connection settings
define("HOST", "localhost"); //domain
define("USER", "user_name"); //user name
define("PASS", "password"); //password
define("DB", "database_name"); //database name
// Open connection to the database
$conn = ewd_db_connect(HOST, USER, PASS,DB);
Example how to use it:
$sSql= " Select , Update, Insert statement here ";
$rs = ewd_query($sSql,$conn) or die(ewd_error($sSql)); //this is where the magic happens
if (ewd_num_rows($rs) > 0){
while($row = ewd_fetch_array($rs)){//do the loop
//...display data
//....................................................................................
//....................................................................................
} //end while
} //end if
ewd_free_result($rs); // free rs
ewd_db_close($conn);//close connection
Tags: php mysql error
















