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

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

Usage and Example of PHP mysqli_stmt_errno() Function

PHP MySQLi Reference Manual

The mysqli_stmt_errno() function returns the error code of the last statement call.

Definition and Usage

mysqli_stmt_errno()The function returns the error code that occurred during the execution of the last statement.

Syntax

mysqli_stmt_errno($stmt)

Parameter

Serial NumberParameters and Description
1

stmt(Required)

This is an object representing the statement.

Return value

PHP mysqli_stmt_errno() function returns an integer value that represents the error code from the execution of the last statement. If there is no error, this function returns0.

PHP version

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

Online example

The following example demonstratesmysqli_stmt_errno()Usage of the function (procedural style), returns the error code of the last executed statement

<?php
   $con = mysqli_connect("localhost", "root", "password", "mydb");
   mysqli_query($con, "CREATE TABLE myplayers(ID INT, First_Name VARCHAR(255), Last_Name VARCHAR(255), Place_Of_Birth VARCHAR(255), Country VARCHAR(255))");
   print("Creating table.....\n");
   mysqli_query($con, "INSERT INTO myplayers values(1, 'Sikhar', 'Dhawan', 'Delhi', 'India')");
   print("Inserting record.....\n");
   $stmt = mysqli_prepare($con, "SELECT * FROM myplayers");
   mysqli_query($con, "DROP TABLE myplayers");
   //Execute statement
   mysqli_stmt_execute($stmt);
   //Error code
   $code = mysqli_stmt_errno($stmt);
   print("Error code: ". $code);
   //End statement
   mysqli_stmt_close($stmt);
   //Close connection
   mysqli_close($con);
?>

Output result

Creating table.....
Inserting record.....
Error Code: 1146

Online example

In the object-oriented style, the syntax of this function is$stmt-> errnoThe following is an example of this function in an object-oriented style-

<?php
   //Establishing connection
   $con = new mysqli("localhost", "root", "password", "mydb");
   $con -> query("CREATE TABLE myplayers(ID INT, First_Name VARCHAR(255), Last_Name VARCHAR(255), Place_Of_Birth VARCHAR(255), Country VARCHAR(255))");
   print("Creating table.....\n");
   $con -> query("INSERT INTO myplayers values(1, 'Sikhar', 'Dhawan', 'Delhi', 'India')");
   print("Inserting record.....\n");
   $stmt = $con ->prepare("SELECT * FROM myplayers");
   $con ->query("DROP TABLE myplayers");
   //Execute statement
   $stmt->execute();
   //Error code
   $code = $stmt ->errno;
   print("Error code: ". $code);
   //End statement
   $stmt->close();
   //Close connection
   $con->close();
?>

Output result

Creating table.....
Inserting record.....
Error code: 1146

Online example

If the last executed statement object has no error, this function returns0 :

<?php
   $con = mysqli_connect("localhost", "root", "password", "mydb");
   mysqli_query($con, "CREATE TABLE myplayers(ID INT, First_Name VARCHAR(255), Last_Name VARCHAR(255), Place_Of_Birth VARCHAR(255), Country VARCHAR(255))");
   print("Creating table.....\n");
   query = "INSERT INTO myplayers values("1, 'Sikhar', 'Dhawan', 'Delhi', 'India'), (2, 'Jonathan', 'Trott', 'Cape Town', 'South Africa'), (3, 'Kumara', 'Sangakkara', 'Matale', 'Sri Lanka')";
   //Prepare statement
   $stmt = mysqli_prepare($con, $query);
   //Execute statement
   mysqli_stmt_execute($stmt);
   print("Inserting record.....\n");
   //Error code
   $code = mysqli_stmt_errno($stmt);
   print("Error code: ". $code);
   //End statement
   mysqli_stmt_close($stmt);
   //Close connection
   mysqli_close($con);
?>

Output result

Creating table.....
Inserting record.....
Error Code: 0

PHP MySQLi Reference Manual