PHP MySQL Query Function with Easy Error Message

This is post is now quite old and the the information it contains may be out of date or innacurate.

If you find any errors or have any suggestions to update the information please let us know or create a pull request on GitHub

This is a simple mysql query function that gives you a nice understandable error message if something goes wrong. You probably would not want this kind of error reporting on a live site, but for testing and developing its pretty handy.

What the function does it perform the database query as normal, but if the query fails, the function gives you a full breakdown of the mysql error messsage allowing you to easily see what has gone wrong and fix it.

function db_query($query){

$output = mysql_query($query) or die('
<h1 style="color: red">Uh Oh......MySQL Error:</h1>
<pre>' . htmlentities($query) . '</pre>
<h3>MySQL Error:</h3>
' . mysql_error() . '
<hr /> <hr />');	return $output;


You would not want to use this query on a live site that is public facing as the error messages will help someone who is trying to break into your system. If you wanted to be able to use this function on a live site and quickly turn the error reporting on and off, we could use a definition like this:

define(MYSQL_ERROR_REPORTING: false;);

Then change the function to be like this:


function db_query($query){ if(MYSQL_ERROR_REPORTING){ $output = mysql_query($query) or die(’

Uh Oh……MySQL Error:


’ . htmlentities($query) . ‘

MySQL Error:

‘ . mysql_error() . ‘

’); }else{ $output = mysql_query($query); } return $output; }

```Then if you need to see the mysql errors, you can quickly edit the definition to allow mysql error reporting by changing the definition to true.

We would always recommend developing and debugging web sites on the localhost and only making changes to the live site once you are sure that everything works. However sometimes for whatever reason it is hard to replicate an error on the localhost so you really need to see what’s happening on the live site.

Alternatively if you are developing an internal system that will only be accessed by your own staff, then some built in error reporting helps you to fix problems that may occur, and also helps your more savvy staff to understand what has gone wrong and give you a good idea so that you can fix it.