A Drupal PHP script to log database errors

I've been having some problems with a GoDaddy website lately (see my GoDaddy 4GH performance problems page, and in an effort to see if the errors were related to a MySQL database problem, I created the Drupal/PHP script shown below.

As a little background information, at one point I was able to catch the website when it was in failure mode, and when I did, Drupal was able render a MySQL error message. However, because Drupal logs error messages to the database -- and since this was a database error -- I wasn't seeing any error messages like this in the Drupal watchdog database logs (error reports).

So, I created this script. The reason I call it a "Drupal PHP" script is that it's really a PHP script that attempts to partially boot up Drupal when it is called.

<?php

#-----------------------------------------------------------
# created by alvin alexander, http://www.devdaily.com
#-----------------------------------------------------------
# use this script to run a query against the drupal database
# without having to do anything special to hook up to the 
# database.
#-----------------------------------------------------------
# released under the creative commons share-alike license:
# http://creativecommons.org/licenses/by-sa/2.5/
# link to this web page without the 'nofollow' tag if you
# copy this work.
#-----------------------------------------------------------

# your drupal installation directory
$html_dir = '/my/home/dir/html';

# make your drupal directory the root php directory
chdir($html_dir);

# set this drupal root constant
define('DRUPAL_ROOT', $html_dir);

# bootstrap drupal up to the point the database is loaded
include_once('./includes/bootstrap.inc');
drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE);

# now try a simple drupal query.
# catch any exceptions that occur, and write them to a plain text
# log file.
# note: found drupal exception handling example here:
# http://api.drupal.org/api/examples/dbtng_example--dbtng_example.module/7/source
try {
  # the simple drupal 7 query
  $query = db_select('node', 'n');
  $query->fields('n',array('title','created'))
        ->range(0,5); // limit to five records
  $result = $query->execute();
  echo '<pre>';
  while($record = $result->fetchAssoc()) {
    print_r($record);
  }
  echo '</pre>';
}
catch (Exception $e) {
  # drupal database errors will be shown in the browser, but also
  # log the errors to a plain text file:
  # (a) write exception to browser
  echo "caught exception";
  echo '<pre>';
  echo 'message' . $e->getMessage();
  echo '</pre>';
  # (b) write exception to log file
  $myFile = '/my/home/dir/logs/database-errors.out';
  $fh = fopen($myFile, 'a') or die("can't open file");
  fwrite($fh, "\n");
  $today = date("F j, Y, g:i a");
  fwrite($fh, $today . "\n");
  fwrite($fh, $e->getMessage() . "\n");
  fflush($fh);
  fclose($fh);
}
?>

If you understand PHP and Drupal (specifically Drupal 7 queries), the only magic in this process is the Drupal bootstrap process I use. I've described this technique in detail in my How to run a Drupal 7 query from PHP tutorial, so I won't repeat all of that here. Hopefully all those comments in the code will help explain everything else that's going on there.

Set this up as a web page

Once this PHP/Drupal script was properly tested, I put it at an undisclosed location on my web server, and then called it from my home computer every two minutes, and logged the results. I describe most of that process in my Shell script to download a URL and time the download speed article.

In summary, if you're looking for a PHP script to load Drupal 7, or test Drupal 7 MySQL database errors, or logs Drupal database errors to a plain text file, I hope this article has been helpful.