Current course:

PHP: Finding errors in a MySQL statement


The code


mysql_query("SELECT * FROM students") or die("Sorry, there was an error.");
mysql_query("SELECT * FROM students") or die(mysql_error());

Explanation

The "or die()" at the end of the statement is triggered if there is an error with the mysql_query().
The parenthasis after the "or die()" contain what we'd like to be written to the page (like an "echo) if the query has an error.

In the first example, if there was an error in the query, it would echo "Sorry, there was an error." This is good to know, but it doesn't give us any information regarding the problem.

In the second example, we echo mysql_error(), which will show us more details about the error. This is great to use while testing, but should be removed when the site is live, as it could give information about your database to potential hackers.