English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
mysqli_errno() function returns the error code of the last function call
mysqli_errno()The function returns the error code that occurred during the last call to the mysqli function.
mysqli_errno($con)
Serial number | Parameters and descriptions |
---|---|
1 | con(Required) This is an object representing the connection with the MySQL Server. |
mysqli_errno() function returns an integer value that represents the error code from the last MySQLi function call. If there is no error, this function returns0.
This function was originally introduced in PHP version5introduced and can be used in all higher versions.
The following example demonstratesmysqli_errno()Function usage (procedural style)-
<?php //Establish connection $con = mysqli_connect("localhost", "root", "password", "mydb"); //Query to retrieve all rows from the employee table mysqli_query($con, "SELECT * FROM employee"); //Error code $error = mysqli_errno($con); print("Error occurred: ".$error); //Close Connection mysqli_close($con); ?>
Output Results
Error occurred: 1064
In object-oriented style, the syntax of this function is$con-> errno. The following is an example of this function in object-oriented style-
<?php //Establish connection $con = new mysqli("localhost", "root", "password", "mydb"); //Query to retrieve all rows from the employee table $con -> query("SELECT * FROM wrong_table_name"); //Error code $error = $con -> errno; print("Error occurred: ".$error); //Close Connection $con -> close(); ?>
Output Results
Error occurred: 1146
The following ismysqli_errno()Another example of the function-
<?php //Establish connection $con = mysqli_connect("localhost", "root", "password", "mydb"); //Query all rows from the employee table mysqli_query($con, "SELECT * FROM employee"); print("Error in SELECT query: ".mysqli_errno($con)."\n"); //Query to update rows in the Employee table mysqli_query($con, "UPDATE employee set INCOME=INCOME+5000 where FIRST_NAME in (*); print("Error in UPDATE query: ".mysqli_errno($con)."\n"); //Query to insert a row into the Employee table mysqli_query($con, "INSERT INTO employee VALUES (Archana, 'Mohonthy', 30, 'M', 13000, 106); print("Error in INSERT query: ".mysqli_errno($con)."\n"); //Close Connection mysqli_close($con); ?>
Output Results
Error in SELECT query: 0 Error in UPDATE query: 1064 Error in INSERT query: 1054
Returns the last error code of the last called function:
<?php $connection_mysql = mysqli_connect("localhost", "root", "password", "mydb"); if (mysqli_connect_errno($connection_mysql)){ echo "Failed to connect to MySQL: ".mysqli_connect_error(); } if (!mysqli_query($connection_mysql, "INSERT INTO employee (FirstName) VALUES ('Jack')")){ echo("Error Code: ").mysqli_errno($connection_mysql); } mysqli_close($connection_mysql); ?>
Output Results
Error Codes: 1054