Skip Navigation

Debug MySQL Queries in PHP

do_query() is a replacement for mysql_query in PHP, providing help with debugging of MySQL queries, and notification from your site when something goes wrong.

Usage is quite simple. First, you must define the two variables at the top of the script, client name and email address. The client name is only useful if you manage several sites, and the email address is where you wish to have error notifications send. You need to include the function itself on each page you wish to use it. Normally, a developer will have a selection of functions in one file, then include that file on every page - and that's where to add this to.

To call it, you replace the "mysql_query" function call with "do_query" function call, and add "__LINE__" towards the end. The addition of the "__LINE__" variable at the end of the call means that rather than needing to hunt through an entire script once the system has informed you of an error, you can see exactly where it is called from. The two boxes below demonstrate, first, the usual way to execute a query and, second, how to execute a query with do_query.

  1. $result = mysql_query("SELECT * FROM table");
  1. $result = do_query("SELECT * FROM table", __LINE__);

If the query executes correctly, you will see no difference in the running of the site. On any error though, the system (instead of the usual message from MySQL) will display a customised error notification telling the user you have been informed of the error. The system will also email you to tell you the file name and line on which the error occurred, and send you the query that made it fail.

The other purpose of do_query is to aid in debugging of script that isn't behaving itself, specifically by actually showing any queries on the page itself. Simply add "debug=1" to your querystring, and any query executed on the page using do_query will be displayed, along with the number of results returned, hopefully allowing you to spend more time fixing bugs and less time hunting them down.

  1. function do_query($sql, $line) {
  2.  
  3. $client = ""; // Client Name
  4. $email = ""; // Email to notify on error
  5.  
  6. $result = @mysql_query($sql);
  7. $total = @mysql_num_rows($result);
  8. if (@mysql_error() <> "") {
  9. echo " <br><font face=\"Verdana\" size=\"1\"><small><b><p align=\"center\">Sorry, there has been an unexpected database error. The webmaster has been informed of this error.</p></b></small></font>";
  10. // Error number
  11. $error_message = "<table border=\"0\" cellpadding=\"3\" cellspacing=\"1\" style=\"border: 1px solid #bbbbbb;\" bgcolor=\"#ffffff\" width=\"80%\" align=\"center\"><tr><td align=\"right\" width=\"25%\"><font face=\"Verdana\" size=\"1\"><small><b>Error Number:</b></small></font></td><td width=\"75%\"><font face=\"Verdana\" size=\"1\"><small>" . @mysql_errno() . "</small></font></td></tr>";
  12.  
  13. // Error Description
  14. $error_message .= "<tr><td align=\"right\"><font face=\"Verdana\" size=\"1\"><small><b>Error Description:</b></small></font></td><td><font face=\"Verdana\" size=\"1\"><small>" . @mysql_error() . "</small></font></td></tr>";
  15.  
  16. // Error Date / Time
  17. $error_message .= "<tr><td align=\"right\"><font face=\"Verdana\" size=\"1\"><small><b>Error Time:</b></small></font></td><td><font face='Verdana' size='1'><small>" . date("H:m:s, jS F, Y") . "</small></font></td></tr>";
  18.  
  19. // Client
  20. $error_message .= "<tr><td align=\"right\"><font face=\"Verdana\" size=\"1\"><small><b>Client:</b></small></font></td><td><font face=\"Verdana\" size=\"1\"><small>" . $client . "</small></font></td></tr>";
  21.  
  22. // Script
  23. $error_message .= "<tr><td align=\"right\"><font face=\"Verdana\" size=\"1\"><small><b>Script:</b></small></font></td><td><font face=\"Verdana\" size=\"1\"><small>" . $_SERVER["SCRIPT_NAME"] . "</small></font></td></tr>";
  24.  
  25. // Line Number
  26. $error_message .= "<tr><td align=\"right\"><font face=\"Verdana\" size=\"1\"><small><b>Line:</b></small></font></td><td><font face=\"Verdana\" size=\"1\"><small>" . $line . "</small></font></td></tr></table>";
  27.  
  28. // SQL
  29. $error_message .= "<table border=\"0\" cellpadding=\"3\" cellspacing=\"1\" style=\"border: 1px solid #bbbbbb;\" bgcolor=\"#ffffff\" width=\"80%\" align=\"center\"><tr><td align=\"right\"><font face=\"Verdana\" size=\"1\"><small><b>Query:</b></small></font></td><td><font face=\"Verdana\" size=\"1\"><small>" . $sql . "</small></font></td></tr>";
  30. $error_message .= "<tr><td align=\"right\" valign=\"top\" width=\"25%\"><font face=\"Verdana\" size=\"1\"><small><b>Processes:</b></small></font></td><td><font face=\"Verdana\" size=\"1\"><small>";
  31. $result = @mysql_list_processes();
  32. while ($row = @mysql_fetch_assoc($result)){
  33. $error_message .= $row["Id"] . " " . $row["Command"] . " " . $row["Time"] . "<br>";
  34. }
  35. @mysql_free_result($result);
  36. $error_message .= "</small></font></td></tr></table>";
  37. $headers = "From: \"MySQL Debug\" <" . $email . ">\r\n";
  38. $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
  39. //mail($email, "[MySQL Error] ". $client, $error_message, $headers);
  40. die();
  41. }
  42. if ($_GET["debug"]=="1") {
  43. echo "<div width=\"100%\" style=\"margin: 10px; margin-bottom: 0; padding: 3px; border: 1px solid #ff0000; background-color: #ffffff; font: 10px verdana; overflow: auto;\">".$sql."</div><div width=\"100%\" style=\"margin: 10px; margin-top: 0; padding: 3px; border: 1px solid #ff0000; background-color: #ff0000; font: 10px verdana; color: #ffffff; font-weight: bold;\">" . $total . " rows found.</div>";
  44. }
  45. return $result;
  46. }

9 comments

Alexander
Canada #1: May 13, 2005
This is pretty cool.
Only one thing I would change - is use of CSS in order to format ouptput tables.
Peter Namtvedt
United States #2: July 26, 2005
what do you mean by "add "debug=1" to your querystring?"
I already have a string there (viewdb?acct=xxxxx)

peterna@adabyron.net
Hi Peter. To debug, you'd use the URL: viewdb?acct=xxxxx&debug=1
What about those of us using various db abstractions ?
Strip those ugly tables off and it is one handy function :)
LKRaider: I should probably either retire this or update it - the code is far older than the article and the site of that HTML now makes me shudder :).
One of the things i love about browsing the web is finding inspiration and new methods.

i've always added error reporting codes in the "or die( 'messages, and codes here' . mysql_error())" after the query call.

once again, thanks for some nice info !
muLLet
United Kingdom #8: June 13, 2007
very nice peace of code, works a treat thanks :)
chetan
Unknown #9: March 25, 2008
hi if u have more debugging tech. for php and mysql so please reply me on cgadgilwar@gmail.com

Post Your Comment

· Comments with keywords instead of a name have their URLs removed.
· Your email address will not be displayed or shared.

Live Comment Preview

 United States #10: 1 minute ago