Finding Out About Errors

So far, we have tested the return values of the MySQL functions that we have used and called die() to end script execution if a problem occurs. You might, however, want to print more informative error messages to the browser to aid debugging. MySQL sets an error number and an error string whenever an operation fails. You can access the error number with mysql_errno() and the error string with mysql_error(). Listing 13.1 brings our previous examples together into a simple script that connects to the server and selects a database. We use mysql_error() to make our error messages more useful. On line 14 we connect to the database. If this is successful, we then select a database on line 19 before closing the connection on ...

Get Sams Teach Yourself PHP in 24 Hours, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.