English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The mysqli_connect_error() function returns the string description of the last connection error
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).
mysqli_connect_error()
This method does not accept any parameters.
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.
This function was initially introduced in PHP version5introduced and can be used in all higher versions.
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)
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)
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
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)