• PHP Video Tutorials

PHP mysqli_init() Function



Definition and Usage

The mysqli_init() function is used to initialize a mysqli object. The result of this function can be passed as one of the parameter to the mysqli_real_connect() function.

Syntax

mysqli_init($con);

Parameters

Sr.No Parameter & Description
1

con(Mandatory)

This is an object representing a connection to MySQL Server.

Return Values

This function returns a mysqli object.

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_init() function (in procedural style) −

<?php
   $db = mysqli_init();
   print_r($db);
?>

This will produce following result −

mysqli Object
(
    [client_info] => mysqlnd 7.4.5
    [client_version] => 70405
    [connect_errno] => 0
    [connect_error] =>
    [errno] => 0
    [error] =>
)

Example

Following is another example of this function $minus;

<?php
   $db = mysqli_init();
   //Creating the connection
   $con = mysqli_real_connect($db, "localhost","root","password","test");
   if($con){
      print("Connection Established Successfully");
   }else{
      print("Connection Failed ");
   }
?>

This will produce following result −

Connection Established Successfully

Example

<?php
   $connection_mysql = mysqli_init();
   
   if (!$connection_mysql){
      die("mysqli_init failed");
   }
   
   if (!mysqli_real_connect($connection_mysql,"localhost","root","password","mydb")){
      die("Connect Error: " . mysqli_connect_error());
   }
   mysqli_close($connection_mysql);
   print("Connection established successfully.....");
?>

This will produce following result −

Connection established successfully.....
php_function_reference.htm
Advertisements