PHP Function Reference

PHP mysqli $connect_errno Property



The PHP mysqli::$connect_errno / mysqli_connect_errno() function returns the error code from the last connect call.

Syntax

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

//Procedural style
mysqli_connect_errno()

Parameters

No parameter is required.

Return Value

Returns an error code for the last connection attempt, if it failed. Zero means no error occurred.

Example: Object-oriented style

The example below shows the usage of mysqli::$connect_errno property.

<?php
//establishing connection to the database
$mysqli = new mysqli("localhost", "fake_user", "password", "database");
if ($mysqli->connect_errno) {
  echo "Connection error: ". $mysqli->connect_errno;
  exit();
}

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

The output of the above code will be similar to:

Connection error: 1045

Example: Procedural style

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

<?php
//establishing connection to the database
$mysqli = mysqli_connect("localhost", "fake_user", "password", "database");
if (mysqli_connect_errno()) {
  echo "Connection error: ". mysqli_connect_errno();
  exit();
}

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

The output of the above code will be similar to:

Connection error: 1045

❮ PHP MySQLi Reference