MorkaLork Development

Interesting stuff I've picked up over the years...


2009-03-15 14:18:45 | 202 views | mysql php mysql_error mysql_error() mysql_errno mysql_errno()

Error handling

Sometimes your PHP-script won't work due to a sql meltdown. If this happens it won't output the error but rather just die. Enter mysql_error().

This function will come in handy when you want the error outputted either directly on your screen or to a textfile.

The mysql_error() function has a best friend, mysql_errno() that gives you the correct number for the error.



string mysql_error ([ resource $link_identifier ] )
string mysql_errno ([ resource $link_identifier ] )



//Create a connection
$link = mysql_connect("localhost", "username", "password");
//Select database
mysql_select_db("examples", $link);
//Create a query
$sql = "SELECT * FOM Person"; //Faulty query....
//Execute the query
$result = mysql_query($sql, $link);

//If correct, do something here...
echo "The query worked!!!";
//If the query was unsucceful, we will output the error
echo "<b>Error ".mysql_errno($link).":</b> ".mysql_error($link);
//Close the connection


Error 1064: 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 'FOM Person' at line 1

Since the query sent in was faulty(FOM instead of FROM) we now get the error-output that first tells us which number the error has, 1064, then the reason for this error. If you want to look into the error even more go here to see a list of all mysql errors..

It's as simple as that.

Article comments

Feel free to comment this article using a facebook profile.

I'm using facebook accounts for identification since even akismet couldn't handle all the spam I receive every day.