The mysqli_connect() function opens a new connection to the MySQL server.
mysqli_connect(host,username,password,dbname,port,socket);
Parameter | Is Required | Description |
---|---|---|
host | Optional. | Host name or an IP address |
username | Optional. | MySQL username |
password | Optional. | MySQL password |
dbname | Optional. | Default database to be used |
port | Optional. | Port number to attempt to connect to the MySQL server |
socket | Optional. | Socket or named pipe to be used |
It returns an object representing the connection to the MySQL server.
The following code opens a new connection to the MySQL server.
<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno($con)){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>