• PHP Video Tutorials

PHP mysqli_stmt_error() Function



Definition and Usage

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

Syntax

mysqli_stmt_error($stmt)

Parameters

Sr.No Parameter & Description
1

stmt(Mandatory)

This is an object representing a statement.

Return Values

PHP mysqli_stmt_error() function returns an string value representing the description of the error occurred during the execution of the last statement. If there are no errors this function returns an empty string.

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

   //Closing the statement
   mysqli_stmt_close($stmt);

   //Closing the connection
   mysqli_close($con);
?>

This will produce following result −

Table Created.....
Record Inserted.....
Error : Table 'mydb.myplayers' doesn't exist

Example

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

   //Closing the statement
   $stmt->close();

   //Closing the connection
   $con->close();
?>

This will produce following result −

Table Created.....
Record Inserted.....
Error : Table 'mydb.myplayers' doesn't exist

Example

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

   //Closing the statement
   mysqli_stmt_close($stmt);

   //Closing the connection
   mysqli_close($con);

?>

This will produce following result −

Table Created.....
Record Inserted.....
Error :
php_function_reference.htm
Advertisements