How to See MySQL Query Errors in PHP


Some people create queries that do not work and don’t have a clue why they don’t, as no error is displayed. I’ll show you how to display the MySQL error, if any, when the query is run. A must know for programmers. Great tool for debugging queries.

1. Open the file where you have your query. For this article, I will be using the following code:

//Connection to DB already established
$query = "SELECT * FROM mytable WHERE column='value'";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)) { //loop through results
 print $row['somecolumn'];

2. In the previous code you see that if “column” with a value of “value” existed, it would output “somecolumn”. But what if you face for example an error saying that mysql_fetch_array() doesn’t accept booleans? Then it would mean that there was a problem with the $query. To see the error, we would simply add mysql_error() function to see what’s going on.

$result = mysql_query($query) or die(mysql_error());

3. This way, we would now see if there was a problem with the query and try to fix it. Most of the time, it is a typo, a quotes problem or a nonexistent column.

So to summarize, remember to add a mysql_error() function for debugging queries. Most of the time you will see what is the problem with that. If not, feel free to contact me so I can personally assist you through the troubleshooting. Any question or comment, please write it below!

Best Regards,


Owner of

Looking for quality ClipBucket Web Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , , , , , , | Posted under Programming/Coding, Security/Vulnerability | RSS 2.0

Author Spotlight

Richi González

I started programming when I was 12. I'm a Developer and have been working with the Clip-Bucket script since it came out. I'm the Liaison between Arvixe and Clip-Bucket, so anything you need regarding Clip-Bucket or any particular computer-related subject you need, let me know so I can assist you.

2 Comments on How to See MySQL Query Errors in PHP

  1. Abby says:

    I want to learn PHP and was wondering, if I should learn it with SQL or PHP alone is enough to create websites and yes I want to do it online as I am working so I do not have much time to travel.
    I found online website and I shortlisted two courses Could you please confirm with which one I should go.


    Please do suggest me as its urgent and want to start the course asap.

    • Hello,

      Well, it all depends on what you want to achieve. You can create really nice backend websites with plain PHP, but MySQL becomes essential when you want to store data.

      If you are starting, I advice to learn CSS3 and HTML5 right now. I am telling you this, because me, as a heart PHP developer, took me some time to learn CSS. I can guide you though all your learning, just hit me up with a message so I can give you further assitance.

      Best Regards,

Leave a Reply

Your email address will not be published. Required fields are marked *