MySQLi Tutorial MySQLi References

mysqli_stmt $errno Property



The mysqli_stmt::$errno / mysqli_stmt_errno() function returns the last error code for the most recent statement call that can succeed or fail.

Syntax

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

//Procedural style
mysqli_stmt_errno(statement)

Parameters

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

Return Value

Returns an error code value. Zero means no error occurred.

Example: Object-oriented style

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

<?php
//establishing connection to the database
$mysqli = new mysqli("localhost", "user", "password", "database");
if ($mysqli->connect_errno) {
  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: %d \n", $stmt->errno);

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

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

The output of the above code will be similar to:

Error: 1146

Example: Procedural style

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

<?php
//establishing connection to the database
$mysqli = mysqli_connect("localhost", "user", "password", "database");
if (mysqli_connect_errno()) {
  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: %d \n", mysqli_stmt_errno($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: 1146

❮ MySQLi Functions Reference