troubleshooting Question

Handling mysql errors in PHP

Avatar of EICT
EICTFlag for United Kingdom of Great Britain and Northern Ireland asked on
3 Comments1 Solution388 ViewsLast Modified:
I've been reading various stuff on error handling and none of them seem to do quite what I want.  My understanding is that there are two type of errors.
php errors  - such as php code errors.
mysql erros - when you try and insert a duplicate record or an incorrect data type value.

At the moment i'm using the following to return the mysql_error and the query

echo '<p>' . mysql_error() . '<br /><br />Query: ' . $query . '</p>'; //Debugging Message.

This allows for easy debugging but is bad for a live production environment.

From what I've read it seems like a good idea to handle each error type seperately. As the only error which could require user correction is an attempt to add a duplicate record I thought I'd write something like.

if (mysql_errno()=='1062') {echo 'A record with this Review date already exist for this PDP Issue';} else {echo 'An error has occurred. Please contact your administrator.';}

I'd also like to write the mysql_error() and $query values to a log file instead of displaying them. I've set the 'error_log' and 'log_errors:On' value in the php.ini file but this does not do what I want.  Please can you give me some advice on this topic, particularly how to right the mysql_error() and $query values to a custom log file?

Thank you
Join our community to see this answer!
Unlock 1 Answer and 3 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros