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