5 Replies - 495 Views - Last Post: 26 April 2013 - 04:30 AM Rate Topic: -----

#1 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 762
  • Joined: 31-August 11

Is It Important To Log MYSQL Errors Beyond The PHPMYADMIN Binary Logs?

Posted 25 April 2013 - 02:39 AM

Some people have told me in the past of queries don't execute that you should always log the erros to a safe text file some place on the server. PHPMYADMIN seems to log this in the binary logs but I'll admit it seems poor. Should you log all errors off somewhere to another safe file?
Is This A Good Question/Topic? 0
  • +

Replies To: Is It Important To Log MYSQL Errors Beyond The PHPMYADMIN Binary Logs?

#2 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 795
  • View blog
  • Posts: 1,681
  • Joined: 30-January 09

Re: Is It Important To Log MYSQL Errors Beyond The PHPMYADMIN Binary Logs?

Posted 25 April 2013 - 06:12 AM

It's generally good practice to log all of your errors in files that aren't accessible publicly, whether you log different types of errors to different files is up to you. There are quite a few PHP functions that you should read up on, that can help you with handling and investigating errors, you can find them in the online PHP manual:

set_error_handler()
set_exception_handler()
register_shutdown_function()
error_reporting()
error_get_last()
debug_print_backtrace()
Was This Post Helpful? 0
  • +
  • -

#3 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 762
  • Joined: 31-August 11

Re: Is It Important To Log MYSQL Errors Beyond The PHPMYADMIN Binary Logs?

Posted 25 April 2013 - 12:34 PM

e_i_pi what's wrong with just using something simple and logging things to an error file outside your web directory like for example


$stmt = $dbh->prepare('bogus sql');
if (!$stmt) {
   $Error = print_r($dbh->errorInfo(), true);

$fh = fopen("Log.txt", 'a');
$fwrite($fh, $Error);
fclose($fh);
}




I said MYSQL related errors mind you? IS there something I'm missing here in terms of the fact that there is automatic easy logging of errors or something?
Was This Post Helpful? 0
  • +
  • -

#4 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 795
  • View blog
  • Posts: 1,681
  • Joined: 30-January 09

Re: Is It Important To Log MYSQL Errors Beyond The PHPMYADMIN Binary Logs?

Posted 25 April 2013 - 04:55 PM

Well, for starters, it's better to use the error log, like so:
$stmt = $dbh->prepare('bogus sql');
if (!$stmt) {
   error_log(print_r($dbh->errorInfo(), true));
}



But, that aside, how many times do you want to write out the if(!$stmt) block? It is better to run all of your queries through a function/method that does that logging for you. Also, the error info you get from the DB might not be enough to track down the issue - MySQL gives very little information in it's errors compared to more robust DBs such as PostgreSQL and MSSQL. Soemtimes you need to know information aboput what was going on in PHP leading up to the DB error, which is where functions such as debug_print_backtrace() come in handy.

For instance, would you rather see an error like this:
1049: Unknown database 'nonexistentdb'


...or something like this...
Spoiler

Was This Post Helpful? 1
  • +
  • -

#5 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 762
  • Joined: 31-August 11

Re: Is It Important To Log MYSQL Errors Beyond The PHPMYADMIN Binary Logs?

Posted 26 April 2013 - 02:34 AM

Thanks pi. That log_error function is better but how do you go about automatically catching errors then as you said without using the if every time to see if the return is null and therefore an error happened?
Was This Post Helpful? 0
  • +
  • -

#6 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,991
  • Joined: 08-June 10

Re: Is It Important To Log MYSQL Errors Beyond The PHPMYADMIN Binary Logs?

Posted 26 April 2013 - 04:30 AM

Personally I would extend whatever database API I was using (PDO or MySQLi) and build the error handling right into the calls. Like this:
class MyDB extends \PDO 
{
	public function prepare($sql)
	{
		// Keep in mind that PDO can be configured to throw exceptions *or* return
		// errors, so you'd have to handle both possibilities if you want this to 
		// work reliably.
		try {
			$stmt = parent::prepare($sql);
			if (!$stmt) {
				$ei = $this->errorInfo();
				$log = MyErrorLogClass::getInstance();
				$log->logMessage($ei[2], debug_backtrace()); 
			}
			
			return $stmt;
		}
		catch (\PDOException $e) {
			$stmt = null;
			$log = MyErrorLogClass::getInstance();
			$log->logException($e);
			
			// Re-trow it so the calling code can catch it as well.
			throw $e;
		}
	}
}


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1