How to See MySQL Query Errors in PHP

Hello,

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,

Richi

Owner of Juapo2Services.com

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

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 became a Liaison between Arvixe and Clip-Bucket, so I am here to assist you with anything you need regarding Clip-Bucket or any particular computer-related subject you need. Best Regards, Richi

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.
    Link1:http://www.wiziq.com/course/5156-build-dynamic-interactive-web-applications-using-php-mysql

    Link2:http://www.wiziq.com/course/5871-php-mysql-with-basic-javascript-integrated-course

    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,
      Richi

Leave a Reply

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


5 + 2 =

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>