mysql_errno() Function in PHP
What is mysql_errno() function in PHP?
How does mysql_errno() works?
Explanation
mysql_errno() function returns the numerical value of the error message from previous MySQL operation.
Syntaxint mysql_errno ( [resource link_identifier])
Returns the error number from the last MySQL function, or 0 (zero) if no error occurred.
mysql_errno() function is used to retrieve the error code from the most recently executed MySQL function.
Example:
<?php //Attempt to connect to the default database server $link = mysql_connect("mysql_host", "mysql_user", "mysql_password") or die ("Could not connect"); //select non existing database if (!mysql_select_db("my_database", $link)) { //prints error number on failure echo " ERROR NO: " . mysql_errno($link) . "n"; } //select from non existing table if (!mysql_query("SELECT * FROM nonexistenttable", $link)) { //print error number on failure echo " ERROR NO: " . mysql_errno($link) . "n"; } mysql_close($link); ?> |
In the above example, the query fails due to non existance of database 'my_database' and table 'nonexistenttable'. Here mysql_errno function prints the occured error number.
RESULT:
ERROR NO : 1049
ERROR NO : 1146
See also: mysql_error() and MySQL error codes