PHP Function Reference

PHP mysqli_stmt $error Property



The PHP mysqli_stmt::$error / mysqli_stmt_error() function returns the last error message for the most recent statement call that can succeed or fail.

Syntax

//Object-oriented style
$mysqli_stmt->error;

//Procedural style
mysqli_stmt_error(statement)

Parameters

statement Required. For procedural style only: Specify a mysqli_stmt object returned by mysqli_stmt_init().

Return Value

Returns a string that describes the error. An empty string if no error occurred.

Example: Object-oriented style

The example below shows the usage of mysqli_stmt::$error property.

<?php
//establishing connection to the database
$mysqli = new mysqli("localhost", "user", "password", "database");
if ($mysqli->connect_error) {
  echo "Failed to connect to MySQL: ". $mysqli->connect_error;
  exit();
}

$sql = "SELECT Name, Age FROM Employee ORDER BY Age";
if ($stmt = $mysqli->prepare($sql)) {

  //dropping the table
  $mysqli->query("DROP TABLE Employee");

  //executing the query
  $stmt->execute();

  printf("Error: %s.\n", $stmt->error);

  //closing the statement
  $stmt->close();
}

//closing the connection
$mysqli->close();
?>

The output of the above code will be similar to:

Error: Table 'database.Employee' doesn't exist.

Example: Procedural style

The example below shows the usage of mysqli_stmt_error() function.

<?php
//establishing connection to the database
$mysqli = mysqli_connect("localhost", "user", "password", "database");
if (mysqli_connect_error()) {
  echo "Failed to connect to MySQL: ". mysqli_connect_error();
  exit();
}

$sql = "SELECT Name, Age FROM Employee ORDER BY Age";
if ($stmt = mysqli_prepare($mysqli, $sql)) {

  //dropping the table
  mysqli_query($mysqli, "DROP TABLE Employee");

  //executing the query
  mysqli_stmt_execute($stmt);

  printf("Error: %s.\n", mysqli_stmt_error($stmt));

  //closing the statement
  mysqli_stmt_close($stmt);
}

//closing the connection
mysqli_close($mysqli);
?>

The output of the above code will be similar to:

Error: Table 'database.Employee' doesn't exist.

❮ PHP MySQLi Reference