PHP mysqli_stmt_errno() Function
Definition and Usage
The mysqli_stmt_errno() function returns the code of the error occurred during the execution of the last statement.
Syntax
mysqli_stmt_errno($stmt)
Parameters
| Sr.No | Parameter & Description |
|---|---|
| 1 |
stmt(Mandatory) This is an object representing a statement. |
Return Values
PHP mysqli_stmt_errno() function returns an integer value representing the code of the error from the execution of the last statement. If there are no errors this function returns 0.
PHP Version
This function was first introduced in PHP Version 5 and works works in all the later versions.
Example
Following example demonstrates the usage of the mysqli_stmt_errno() function (in procedural style) −
<?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("Table Created.....\n");
mysqli_query($con, "INSERT INTO myplayers values(1, 'Sikhar', 'Dhawan', 'Delhi', 'India')");
print("Record Inserted.....\n");
$stmt = mysqli_prepare($con, "SELECT * FROM myplayers");
mysqli_query($con, "DROP TABLE myplayers");
//Executing the statement
mysqli_stmt_execute($stmt);
//Error Code
$code = mysqli_stmt_errno($stmt);
print("Error Code: ".$code);
//Closing the statement
mysqli_stmt_close($stmt);
//Closing the connection
mysqli_close($con);
?>
This will produce following result −
Table Created..... Record Inserted..... Error Code: 1146
Example
In object oriented style the syntax of this function is $stmt ->errno. Following is the example of this function in object oriented style −
<?php
//Creating a 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("Table Created.....\n");
$con -> query("INSERT INTO myplayers values(1, 'Sikhar', 'Dhawan', 'Delhi', 'India')");
print("Record Inserted.....\n");
$stmt = $con ->prepare("SELECT * FROM myplayers");
$con ->query("DROP TABLE myplayers");
//Executing the statement
$stmt->execute();
//Error Code
$code = $stmt ->errno;
print("error Code: ".$code);
//Closing the statement
$stmt->close();
//Closing the connection
$con->close();
?>
This will produce following result −
Table Created..... Record Inserted..... error Code: 1146
Example
If there are no errors in the last executed statement object this function returns 0 −
<?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("Table Created.....\n");
query = "INSERT INTO myplayers values(1, 'Sikhar', 'Dhawan', 'Delhi', 'India'),(2, 'Jonathan', 'Trott', 'CapeTown', 'SouthAfrica'),(3, 'Kumara', 'Sangakkara', 'Matale', 'Srilanka')";
//Preparing a statement
$stmt = mysqli_prepare($con, $query);
//Executing the statement
mysqli_stmt_execute($stmt);
print("Record Inserted.....\n");
//Error Code
$code = mysqli_stmt_errno($stmt);
print("Error Code: ".$code);
//Closing the statement
mysqli_stmt_close($stmt);
//Closing the connection
mysqli_close($con);
?>
This will produce following result −
Table Created..... Record Inserted..... Error Code: 0