MySQLi Tutorial MySQLi References

mysqli $errno Property



The mysqli::$errno / mysqli_errno() function returns the last error code for the most recent MySQLi function call that can succeed or fail.

Syntax

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

//Procedural style
mysqli_errno(mysql)

Parameters

mysql Required. For procedural style only: Specify a mysqli object returned by mysqli_connect() or mysqli_init().

Return Value

Returns an error code value for the last call, if it failed. zero means no error occurred.

Example: Object-oriented style

The example below shows the usage of mysqli::$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();
}

//executing an SQL query
$sql = "SET x=1";

if (!$mysqli->query($sql)) {
  printf("Error code: %d\n", $mysqli->errno);
}

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

The output of the above code will be similar to:

Error code: 1193

Example: Procedural style

The example below shows the usage of mysqli_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();
}

//executing an SQL query
$sql = "SET x=1";

if (!mysqli_query($mysqli, $sql)) {
  printf("Error code: %d\n", mysqli_errno($mysqli));
}

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

The output of the above code will be similar to:

Error code: 1193

❮ MySQLi Functions Reference