English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP mysqli_errno() Function Usage and Example

PHP MySQLi Reference Manual

mysqli_errno() function returns the error code of the last function call

Definition and usage

mysqli_errno()The function returns the error code that occurred during the last call to the mysqli function.

Syntax

mysqli_errno($con)

Parameter

Serial numberParameters and descriptions
1

con(Required)

This is an object representing the connection with the MySQL Server.

Return value

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.

PHP version

This function was originally introduced in PHP version5introduced and can be used in all higher versions.

Online Example

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

Online Example

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

Online Example

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

Online Example

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

PHP MySQLi Reference Manual