The mysqli_real_connect() function opens a new connection to the MySQL server.
PHP mysqli_real_connect() Function has the following syntax.
mysqli_real_connect(connection,host,username,password,dbname,port,socket,flag);
Parameter | Is Required | Description |
---|---|---|
connection | Required. | MySQL connection to use |
host | Optional. | Host name or an IP address |
username | Optional. | Username |
password | Optional. | Password |
dbname | Optional. | Default database to be used |
port | Optional. | Port number to the MySQL server |
socket | Optional. | Socket or named pipe to be used |
flag | Optional. | Connection options. |
Possible values for flag option:
Value | Meaning |
---|---|
MYSQLI_CLIENT_COMPRESS | Use compression protocol |
MYSQLI_CLIENT_FOUND_ROWS | Return number of matched rows |
MYSQLI_CLIENT_IGNORE_SPACE | Allow spaces after function names. Make function names reserved words |
MYSQLI_CLIENT_INTERACTIVE | Allow interactive_timeout seconds of inactivity before closing connection |
MYSQLI_CLIENT_SSL | Use SSL encryption |
It returns TRUE on success and FALSE on failure.
mysqli_real_connect() requires a valid object created by mysqli_init(). It can be used with mysqli_options() to set different options for the connection.
<?php// w w w . j a va 2 s. co m
$con=mysqli_init();
if (!$con){
die("mysqli_init failed");
}
if (!mysqli_real_connect($con,"localhost","my_user","my_password","my_db")){
die("Connect Error: " . mysqli_connect_error());
}
mysqli_close($con);
?>