Open In App

PHP mysqli_connect() Function

The mysqli_connect() function in PHP is used to connect you to the database. In the previous version of the connection, the mysql_connect() was used for connection and then there comes mysqli_connect() function where i denotes an improved version of the connection and is more secure than mysql_connect().

Syntax: 

mysqli_connect ( "host", "username", "password", "database_name" );

Parameters Used: 

Return Value:

Program: Below is the implementation of mysqli_connect() function.






<?php
mysqli_connect("localhost", "root", "", "GFG");
 
if (mysqli_connect_error()) {
    echo "Connection Error.";
} else {
    echo "Database Connection Successfully.";
}
?>

Output:

Database Connection Successfully.

We can also use the die() method to terminate the program with a message if the connection fails.



Program: Below is the implementation of the die() method with the mysqli_connect() method.




<?php
mysqli_connect("localhost", "root", "", "GFG") or
    die("Connection Unsuccessfull");
 
// If the connection is successfull then
// the program does not terminates
// and the following message is displayed.
echo "Connection Successfull";
?>

Output:

Database Connection Successfully.

Article Tags :