PHP mysqli_connect() Function
Definition
The mysqli_connect() function opens a new connection to the MySQL server.
Syntax
mysqli_connect(host,username,password,dbname,port,socket);
Parameter
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 |
Return
It returns an object representing the connection to the MySQL server.
Example
The following code opens a new connection to the MySQL server.
<?php// w w w.j a v a 2 s.c o m
$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();
}
?>