PHP | mysqli_stmt_error() Function

PHP mysqli_stmt_error() Function


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_error() Function.

The built-in mysqli_stmt_error() function in PHP returns the description of the error occurred during the execution of the last statement.

Syntax

Following below is the syntax to use this function -

mysqli_stmt_error($stmt)


Parameter Details

Sr.NoParameter & Description
1

stmt(Mandatory)

This is an object representing a statement.


Return Value

This built-in PHP function returns a string value representing the description of the error occurred during the execution of the last statement. If there are no errors, this PHP function returns an empty string.

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_error() 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 
   $error = mysqli_stmt_error($stmt);
   print("Error : ".$error);

   //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 : Table 'mydb.myplayers' doesn't exist

Example2

In an object oriented style the syntax of this function is $stmt->error(); 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
   $error = $stmt ->error;
   print("Error: ".$error);

   //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 : Table 'mydb.myplayers' doesn't exist

Example3

If there are no errors in the last executed statement object, this function returns an empty string -

<?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 
   $error = mysqli_stmt_error($stmt);
   print("Error : ".$error);

   //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  :


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_execute() 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.