English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Basic PHP Tutorial

Advanced PHP Tutorial

PHP & MySQL

PHP Reference Manual

Usage and Examples of PHP mysqli_connect_errno() Function

PHP MySQLi Reference Manual

The mysqli_connect_errno() function returns the error code of the last connection call

Definition and usage

During the process of attempting to connect to the MySQL server, if any errors occur,mysqli_connect_errno()The function returns the error code that occurred (during the last connection call).

Syntax

mysqli_connect_errno()

Parameters

This method does not accept any parameters.

Return value

If the connection fails, the PHP mysqli_connect_errno() function will return an integer value representing the error code from the last connection call. If the connection is successful, this function returns0.

PHP version

This function was originally in PHP version5introduced and can be used in all higher versions.

Online example

The following examples demonstratemysqli_connect_errno()Function usage (procedural style)-

<?php
   //Establish connection
   $con = mysqli_connect("localhost", "root", "wrong_password", "mydb");
   //Client error
   $code = mysqli_connect_errno();
   print("Error code:  " . $code);

Output result

Error code: 1045

Online example

In the object-oriented style, the syntax of this function is$con->connect_errno. The following is an example of the object-oriented style of this function-

<?php
   //Establish connection
   $con = @new mysqli("localhost", "wrong_user_name", "password", "mydb");
   //Error code
   $code = $con->connect_errno;
   print("Error code:  " . $code);
?>

Output result

Error code: 1045

Online example

The following examples demonstratemysqli_connect_errno()Function behavior-

<?php
   //Establish connection
   $con = @mysqli_connect("localhost", "root", "password", "mydb");
   //Error code
   $code = mysqli_connect_errno();
   if ($code) {
      print("Connection failed:  " . $code);
   } else {
      print("Connection established successfully");
   }
?>

Output result

Connection established successfully

Online example

Returns the last connection error code:

<?php
   $connection = @mysqli_connect("localhost", "root", "wrong_pass", "wrong_db");
   
   if (!$connection) {
      die("Connection error:  " . mysqli_connect_errno());
   }
?>

Output result

Connection error: 1045

PHP MySQLi Reference Manual