Hello folks! welcome back to a new edition of our tutorial on PHP. In this tutorial guide, we are going to be studying about the PHP mysqli_stmt_errno() Function.
The built-in mysqli_stmt_errno() function in PHP returns the code of the error occurred during the execution of the last statement.
The built-in mysqli_stmt_errno() function in PHP returns the code of the error occurred during the execution of the last statement.
Syntax
Following below is the syntax to use this function -
mysqli_stmt_errno($stmt)
Parameter Details
Sr.No | Parameter & Description |
---|---|
1 | stmt(Mandatory) This is an object representing a statement. |
Return Value
It returns an integer value representing the code of the error which occurred from the execution of the last statement. If there are no errors, this function returns 0.
PHP Version
This PHP function was first introduced in PHP version 5 and works in all the later versions.
Example1
The following below is an example which demonstrates the usage of PHP mysqli_stmt_errno() function (in a 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, 'Kennedy', 'Nkpara', 'PortHarcourt', 'Nigeria')"); 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); ?>
Output
When the above code is executed, it will produce the following result -
Table Created..... Record Inserted..... Error Code: 1146
Example2
In an object oriented style the syntax of this function is $stmt->errno(); Following is the example of this function in an 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, 'Kennedy', 'Nkpara', 'PortHarcourt', 'Nigeria')"); 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(); ?>
Output
When the above code is executed, it will produce the following result -
Table Created..... Record Inserted..... error Code: 1146
Example3
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, 'Kennedy', 'Nkpara', 'PortHarcourt', 'Nigeria'),(2, 'Jonathan', 'Trott', 'CapeTown', 'SouthAfrica'),(3, 'Queen', 'Douglas', 'Texas', 'UnitedStates')"; //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); ?>
Output
When the above code is executed, it will produce the following result below -
Table Created..... Record Inserted..... Error Code: 0
Alright guys! This is where we are going to be rounding up for this tutorial post. In our next tutorial, we are going to be discussing about the mysqli_stmt_error() Function in PHP.
Do feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.
Do follow us on our various social media handles available and also subscribe to our newsletter to get our tutorial posts delivered directly to your emails.
Thanks for reading and bye for now.
Do feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.
Do follow us on our various social media handles available and also subscribe to our newsletter to get our tutorial posts delivered directly to your emails.
Thanks for reading and bye for now.