4th April 2008 - 3 minutes read time
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, which is clearly wrong.
ELECT * FROM table;
I tend to do this at least every now and then, and don't usually spot it until it's too late. So when I then run mysql_query() on this statement nothing happens. As far as I can tell form the code the table contains no data.
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.