PHP | mysqli_init() Function

PHP mysqli_init() Function


Hello folks! welcome back to another edition of our tutorial on PHP. In this tutorial guide, we are going to be discussing about the PHP mysqli_init() Function.

The built-in PHP mysqli_init() function is used for initializing a mysqli object. The result of this built-in function can be passed as one of the parameter to the built-in PHP mysqli_real_connect() function.

Syntax

Following below is the syntax to use this function -

mysqli_init($con);


Parameter Details

Sr.NoParameter & Description
1

con(Mandatory)

This is an object representing a connection to MySQL Server.


Return Value

This built-in PHP function returns a MYSQLi object.

PHP Version

This built-in PHP function was first introduced in PHP version 5 and works in all the later versions.

Example1

The following below is an example which demonstrates the usage of PHP mysqli_init() function (in a procedural style) -

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

Output

When the above code is executed, it will produce the following result -

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

Example2

The following below is another example of this built-in PHP 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 ");
   }
?>

Output

When the above code is executed, it will produce the following result -

Connection Established Successfully

Example3

Try the following example below -

<?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......");
?>

Output

When the above code is executed, it will produce the following result -

Connection Established Successfully......


Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial, we will discuss about the PHP mysqli_insert_id() Function.

Do feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Do follow us on our various social media handles available and also subscribe to our newsletter to get our tutorial posts delivered directly to your emails.

Thanks for reading and bye for now.