PHP mysqli: connect_error() function

mysqli_connect_error() function / mysqli::$connect_error

The mysqli_connect_error() function / mysqli::$connect_error returns the error description from the last connection error, if any.


Object oriented style

string $mysqli->connect_error;

Procedural style

string mysqli_connect_error ( void )

Usage: Procedural style


Return value:

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

Version: PHP 5, PHP 7

Example of object oriented style:

$mysqli = @new mysqli('localhost', 'user1', 'datasoft123', 'hr');

// Works as of PHP 5.2.9 and 5.3.0.
if ($mysqli->connect_error) {
    die('Connect Error: ' . $mysqli->connect_error);

Example of procedural style:

$link = @mysqli_connect('localhost', 'user1', 'datasoft123', 'hr');

if (!$link) {
    die('Connect Error: ' . mysqli_connect_errno());


// Check connection
if (!$con)
  die("Connection error: " . mysqli_connect_errno());


Connection error: 0

See also

PHP Function Reference

Previous: connect_errno
Next: connect

Follow us on Facebook and Twitter for latest update.

PHP: Tips of the Day

PHP - how to create a newline character?

Only double quoted strings interpret the escape sequences \r and \n as '0x0D' and '0x0A' respectively, so you want:


Single quoted strings, on the other hand, only know the escape sequences \\ and \'.

So unless you concatenate the single quoted string with a line break generated elsewhere (e. g., using double quoted string "\r\n" or using chr function chr(0x0D).chr(0x0A)), the only other way to have a line break within a single quoted string is to literally type it with your editor:

$s = 'some text before the line break
some text after';

Make sure to check your editor for its line break settings if you require some specific character sequence (\r\n for example).

Ref : https://bit.ly/3hcyege


We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook