• PHP Video Tutorials

PHP mysqli_connect_errno() Function



Definition and Usage

During the attempt to connect to a MySQL server, if an occurs The mysqli_connect_errno() function returns the code of the error occurred (during the last connect call).

Syntax

mysqli_connect_errno()

Parameters

This method doesn't accept any parameters.

Return Values

PHP mysqli_connect_errno() function returns an integer value representing the code of the error from the last connection call, incase of a failure. If the connection was successful this function returns 0.

PHP Version

This function was first introduced in PHP Version 5 and works works in all the later versions.

Example

Following example demonstrates the usage of the mysqli_connect_errno() function (in procedural style) −

<?php
   //Creating a connection
   $con = mysqli_connect("localhost", "root", "wrong_password", "mydb");

   //Client Error
   $code = mysqli_connect_errno();
   print("Error Code: ".$code);

This will produce following result −

Error Code: 1045

Example

In object oriented style the syntax of this function is $con -> connect_errno. Following is the example of this function in object oriented style −

<?php
   //Creating a connection
   $con = @new mysqli("localhost", "wrong_user_name", "password", "mydb");

   //Error code
   $code = $con->connect_errno;
   print("Error Code: ".$code);
?>

This will produce following result −

Error Code: 1045

Example

Following example demonstrates the behaviour of the mysqli_connect_errno() function incase of a successful connection −

<?php
   //Creating a 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");
   }
?>

This will produce following result −

Connection established successfully

Example

<?php
   $connection = @mysqli_connect("localhost","root","wrong_pass","wrong_db");
   
   if (!$connection){
      die("Connection error: " . mysqli_connect_errno());
   }
?>

This will produce following result −

Connection error: 1045
php_function_reference.htm
Advertisements