mysqli_connect() in PHP Example

The mysqli_connect() function opens a new connection to the MySQL server.

mysqli_connect() in PHP Example

<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */

$host= "localhost";
$username= "root";
$password = "";

$db_name = "test_db";

// MySQLi, Procedural way
$mysql_connection = mysqli_connect($host, $username, $password, $db_name);

// Check connection
if($mysql_connection === false){
   die("ERROR: Could not connect. " . mysqli_connect_error());
}
 
// Print host information
echo "Connect Successfully. Host info: " . mysqli_get_host_info($mysql_connection);

// Close connection
mysqli_close($link);
?>

Note that we have used mysqli_connect() function:

// MySQLi, Procedural way
$mysql_connection = mysqli_connect($host, $username, $password, $db_name);

Closing the MySQL server connection:

// Close connection
mysqli_close($link);




Comments