English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The mysqli_stmt_errno() function returns the error code of the last statement call.
mysqli_stmt_errno()The function returns the error code that occurred during the execution of the last statement.
mysqli_stmt_errno($stmt)
Serial Number | Parameters and Description |
---|---|
1 | stmt(Required) This is an object representing the statement. |
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.
This function was first introduced in PHP version5introduced and can be used in all higher versions.
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
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
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