eHow launches Android app: Get the best of eHow on the go.

How To

How to Log MySQL Errors in PHP

Member
By Altari
User-Submitted Article
(11 Ratings)

Sometimes, our MySQL queries throw an error. Usually, we don't want our users to see the error, but we'll still need to know what the error was.

Difficulty: Moderate
Instructions

Things You'll Need:

  • MySQL Database
  • PHP File
  • Log File
  • Text Editor
  1. Step 1

    The complete code for this tutorial can be found at the bottom in the "tips" section.

  2. Step 2
     

    Create a junk MySQL query in your PHP file. I just put in random characters for the "user name" and "password" fields in the mysql_connect() statement. You should get an error similar to that shown in the image.

  3. Step 3
     

    The error shown may be good when you're developing, but you wouldn't want everyone to see what your user name and host name are. You could get hacked, and that's never a good thing. We also don't want to just silence all errors (@), since if there is an error we want to know what it is. So, we'll use a log file (in this case, log.txt). We're going to create a function called writeErrors().

  4. Step 4
     

    We want to tell our function to open the file log.txt for writing whenever it is called, by using the function fopen(). fopen() needs at least 2 parameters - file name (in this case, log.txt) and write method (we'll be using a+). a+ tells fopen() to open the file, and write to the end of it; if the file doesn't exist, create it. We need to make fopen() to definition of a variable ($handle), because we'll use it later.

  5. Step 5
     

    Now that our file is open, we want our function to write whatever error it receives (we'll get to that in a few steps) to file. We'll use the function fwrite(), which takes two parameters : handle (from above, $handle) and what to write (in this case ($error).

  6. Step 6
     

    Finally, now that we're written to the file, we're going to finalize our writing by closing it. We'll use the function fclose(). fclose() takes a single parameter - handle ($handle).

  7. Step 7
     

    Outside of our function, we'll need to call the function. So, we'll change our MySQL query to use it. When we call writeErrors(), we'll be adding a new paramenter - mysql_error(). We need to return to our function text, and tell it to expect the error message by adding $error in the parenthesis. As the code stands, whichever error is sent to writeErrors() will be written too log.txt.

  8. Step 8
     

    The error message we get from the previous step won't be very descriptive, so we want to add a few pieces of information, namely the file name (__FILE__), the line number (__LINE__), the user's IP address ($_SERVER['REMOTE_ADDR']) and the date/time of the error. We'll also want to add "@" before the query to prevent the errors from showing themselves to the user(s).

  9. Step 9
     

    We also need to update the function writeErrors() to handle the new parameters, and generate a complete error. When you put the error into your PHP editor, use "\n" for a line break, and add tabs (if you want) to make the output more readable. At the very least, every error should end with "\n" to separate individual errors.

  10. Step 10
     

    The output of the above code will look something like this.

Tips & Warnings
  • function writeErrors($error,$ip,$date,$page,$line) {
  • $error = "\n".$date." - IP ".$ip." caused the following error in ".$page." on line ".$line." ".$error."\n";
  • $handle = fopen("log.txt","w+");
  • fwrite($handle,$error);
  • fclose($handle);
  • }
  • $page = $_SERVER['PHP_SELF'];
  • $ip = $_SERVER['REMOTE_ADDR'];
  • $date = date("m.d.Y h:iA T");
  • mysql_connect("localhost","kgnfdj","nlgjn") or die(writeErrors(mysql_error(),$ip,$date,__FILE__,__LINE__));
  • Make sure not to use "w+" in your fopen() function - you'll delete the old contents of the file and start over
Subscribe

Post a Comment

Post a Comment

Related Ads

  • Have you done this? Click here to let us know.
I Did This
Tags
Get Free Computers Newsletters

Copyright © 1999-2009 eHow, Inc. Use of this web site constitutes acceptance of the eHow Terms of Use and Privacy Policy.   en-US Portions of this page are modifications based on work created and shared by Google and used according to terms described in the Creative Commons 3.0 Attribution License.

eHow Computers
eHow_eHow Technology and Electronics