php8.5
Home/ Manual/ mysqli / mysqli/ mysqli::$connect_error

mysqli::$connect_error

PHP function Edit on GitHub ✎

Returns a description of the last connection error

Description

Oop

string|nullmysqli->connect_error

Procedural

mysqli_connect_error(): string|null

Returns the error message from the last connection attempt.

Parameters Parameters

Return Values

A string that describes the error. null is returned if no error occurred.

Returns the last connection error regardless of the instance on which it is called.

Examples

$mysqli->connect_error example

Oop

php
<?php

mysqli_report(MYSQLI_REPORT_OFF);
/* @ is used to suppress warnings */
$mysqli = @new mysqli('localhost', 'fake_user', 'wrong_password', 'does_not_exist');
if ($mysqli->connect_error) {
    /* Use your preferred error logging method here */
    error_log('Connection error: ' . $mysqli->connect_error);
}

Procedural

php
<?php

mysqli_report(MYSQLI_REPORT_OFF);
/* @ is used to suppress warnings */
$link = @mysqli_connect('localhost', 'fake_user', 'wrong_password', 'does_not_exist');
if (!$link) {
    /* Use your preferred error logging method here */
    error_log('Connection error: ' . mysqli_connect_error());
}

See Also

Source: reference/mysqli/mysqli/connect-error.xml · from the official PHP manual (php/doc-en)