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

Basic PHP Tutorial

Advanced PHP Tutorial

PHP & MySQL

PHP Reference Manual

Usage and example of PHP mysqli_connect_error() function

PHP MySQLi Reference Manual

The mysqli_connect_error() function returns the string description of the last connection error

Definition and usage

During the process of attempting to connect to the MySQL server, if an error occursmysqli_connect_error()The function will return the description of the error that occurred (during the last connection call).

Syntax

mysqli_connect_error()

Parameter

This method does not accept any parameters.

Return value

If the connection fails, the PHP mysqli_connect_error() function will return a string value indicating the error description of the last connection call. If the connection is successful, this function returnsNull.

PHP version

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

Online example

The following examples demonstratemysqli_connect_error()Usage of the function (procedural style)-

<?php
   //Establish connection
   $con = @mysqli_connect("localhost", "root", "wrong_password", "mydb");
   //Connection error
   $error = mysqli_connect_error($con);
   print("Error: " . $error);
?>

Output result

Error: Access denied for user 'root'@'localhost' (using password: YES)

Online example

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

<?php
   //Establish connection
   $con = @new mysqli("localhost", "root", "wrong_password", "mydb");
   //Connection error
   $error = $con-> connect_error;
   print("Error: " . $error);
?>

Output result

Error: Access denied for user 'root'@'localhost' (using password: YES)

Online example

The following examples demonstrate the successful connection aftermysqli_connect_error()Behavior of the function-

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

Output result

Connection established successfully

Online example

Return the error description of the last connection error:

<?php
   $connection = @mysqli_connect("localhost", "root", "wrong_pass", "wrong_db");
   
   if (!$connection) {
      die("Connection error: " . mysqli_connect_error());
   }
?>
Test and see‹/›

Output result

Connection error: Access denied for user 'root'@'localhost' (using password: YES)

PHP MySQLi Reference Manual