MySQL Error Checking in PHP
So often it seems that I am seeing someone struggling to find out what is wrong with their PHP script. They see the following error message:
So we can see how it is essential to perform error checking every step of the way to eliminate any guesswork as to what went wrong, where it went wrong, and why. The remaining item to discuss is how to check for errors. There are numerous different styles and techniques, each with their own pros and cons. My personal preference is to do several things:
Warning: mysql_fetch_assoc():and have no clue what this means. The line number has no bearing on where the real problem is, and there may be a whole cascade of these messages. Let's assume the code looks something like this:
supplied argument is not a valid MySQL result resource
in /var/www/htdocs/somefile.php on line 18
mysql_connect ("server", "user", "password");Where did the error occur? The answer is it could be any number of places. Let's make a list:
mysql_select_db ("dbname");
$result = mysql_query ("SELECT something FROM mytable");
while ($row = mysql_fetch_assoc ($result))
{
...
}
- The server name is wrong.
- MySQL is not running on the server
- The script is being run on a host that is not allowed to connect to the MySQL server
- Incorrect username
- Incorrect password
- The database does not exist
- The username does not have permission to access the database at all
And finally: - There is an error in the query
Access denied for user 'nobody'@'localhost' (using password: NO)At this point our user is scratching his head, because he knows he put in the right credentials, he's double-checked. This error message gives a complete different set of credentials: wrong username, no password, maybe even a diffrent server name. The reason for this, which is not always obvious, is that a connection did not get established via mysql_connect, so PHP reverts to a default set that is in its config file. Chances are nobody has edited it to put valid credentials in, and I wouldn't personally recommend it for security reasons.
So we can see how it is essential to perform error checking every step of the way to eliminate any guesswork as to what went wrong, where it went wrong, and why. The remaining item to discuss is how to check for errors. There are numerous different styles and techniques, each with their own pros and cons. My personal preference is to do several things:
- Store the credentials in a separate script. This might even be outside of the htdocs directory tree entirely, so there is no chance of someone referencing it maliciously.
- Write a small script that references the credentials script, connects to the database and selects the database, using proper error checking of course.
(At this point we have eliminated the possibility of any connection error being mistaken for a faulty query) - Display a compound error message with detailed information about a failing query.
- or die()
- or die("Query failed!")
- or die ("Query failed: " . mysql_error())
- or die ("Query failed: " . mysql_error() . " Actual query: " . $query)
9 Comments:
Thank you but the last one is very dangerous if you have an error into your website.
(haker can show the field names)
So i think that this one is correct
$sql_0= {request} or die ("Query failed 0: " . mysql_error());
"0" is a var whose you can change over your code.
Now, you know exactly where error into your code and it will not show your request.
Thanks
Maybe not for a production site, and maybe you want a debug flag that you can turn on to enable this display, but I find this information invaluable for debugging. Something may have gone wrong with the symbol substitution. Being able to see the actual query can save me hours of guesswork.
This was meant as an example of how to diagnose issues with MySQL and not as a PHP error-handling tutorial, seeing as how people cobble together a page, get misleading diagnostics, and then get onto the forums saying "it doesn't work" but they don't know what went wrong and neither does anyone else since proper diagnostics were not displayed.
Hey Bob,
this is exactly what I was looking for. I use to recreate my querys and paste them into the mysql cmd procesor. This will save me a ton of time.
Thanks,
Brian
Hey Bob, Thanks for that. Two and a half years on and your posting is still helping people. Feel good mon amis. :)
Good article.
Hi Bob,
Thanks for a great article. I've been programming for 35 years but not much in PHP so it helped a lot.
( '".$a"', '".$b"', '".$c"', '".$d"', '".$e"', '".$f"', '".$g"', '".$h"', '".$i"','".$j"', '".$k"', '".$l"')
please find the error in this code
( '".$a"', '".$b"', '".$c"', '".$d"', '".$e"', '".$f"', '".$g"', '".$h"', '".$i"','".$j"', '".$k"', '".$l"')
please let me know the error in this code
Post a Comment
<< Home