Home > PHP > Latest MySQL Errors With PHP

Latest MySQL Errors With PHP

Pinpointing where an error in a program is occurring is not always easy. This can be even more of a problem when you write an SQL query that produces an error. Take the following select statement.

ELECT * FROM table;

I tend to do this at least once a week, and don’t usually spot it until it’s too late. So when I then run mysql_query() on this statement nothing happens.

To get the error message that is produced from the SQL you can use the mysql_error() function to return the error message. The single optional parameter is the resource identifier for the MySQL connection, but if you leave this out then PHP will use the latest resource created.

echo mysql_error();

This will print the following line in your PHP output.

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ELECT * FROM table' at line 1

To more intelligently print off your error message you can use the following

$error = mysql_error();
if(strlen($error) > 0){
 echo $error;
};

This code will only print off the error message if it is present.

Information, services, and products:
suriplace
Categories: PHP Tags: , , , , ,
  1. No comments yet.
  1. No trackbacks yet.