w3resource

PHP MySQLi: errno() function

mysqli_errno() function / mysqli::$errno

The mysqli_errno() function / mysqli::$errno return the last error code for the most recent function call, if any.

Syntax:

Object oriented style

int $mysqli->errno;

Procedural style

int mysqli_errno ( mysqli $link )

Usage: Procedural style

mysqli_errno(connection);

Parameter:

Name Description Required/Optional
connection Specifies the MySQL connection to use Required

Return value:

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

Version: PHP 5, PHP 7

Example of object oriented style:

<?php
$mysqli = new mysqli("localhost", "user1", "datasoft123", "hr");

/* check connection */
if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();
}

if (!$mysqli->query("SET a=1")) {
    printf("Errorcode: %d\n", $mysqli->errno);
}

/* close connection */
$mysqli->close();
?>

Output:

Errorcode: 1193

Example of procedural style:

<?php

$link = mysqli_connect("localhost", "user1", "datasoft123", "hr");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

if (!mysqli_query($link, "SET a=1")) {
    printf("Errorcode: %d\n", mysqli_errno($link));
}

/* close connection */
mysqli_close($link);
?>

Output:

Errorcode: 0

Example:

<?php
$con=mysqli_connect("localhost","user1","datasoft123","hr");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

// Perform a query, check for error
if (!mysqli_query($con,"INSERT INTO employees (First_Name) VALUES ('David')"))
  {
  echo("Errorcode: " . mysqli_errno($con));
  }

mysqli_close($con);
?>

Sample Output:

Errorcode:

See also

PHP Function Reference

Previous: dump_debug_info
Next: error_list



Follow us on Facebook and Twitter for latest update.