English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Basic PHP Tutorial

Advanced PHP Tutorial

PHP & MySQL

PHP Reference Manual

Usage and examples of PHP mysqli_init() function

PHP MySQLi Reference Manual

The mysqli_init() function initializes MySQLi and returns a resource type value, which can be used as an argument for the mysqli_real_connect() function

Definition and Usage

mysqli_init()Function is used to initialize the mysqli object. The result of this function can be passed as one of the parameters tomysqli_real_connect()Function.

Syntax

mysqli_init($con);

Parameter

NumberParameters and Description
1

con(Required)

This is an object representing the connection with the MySQL Server.

Return Value

This function returns a mysqli object.

PHP Version

This function was first introduced in PHP version5introduced and can be used in all higher versions.

Online Example

The following examples demonstratemysqli_init()Usage of the Function (Procedural Style)-

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

Output Result

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

Online Example

Here is another example of this function $ minus;

<?php
   $db = mysqli_init();
   //Establish Connection
   $con = mysqli_real_connect($db, "localhost","root","password","test");
   if($con){
      print("Connection established successfully");
   }
      print("Connection failed ");
   }
?>

Output Result

Connection established successfully

Online Example

Example of using the mysqli_init() function:

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

Output Result

Connection established successfully.....

PHP MySQLi Reference Manual