PHP mysqli_real_connect() Function

PHP mysqli_real_connect() function is used to open a connection to a mysql server. It differs from mysqli_connect() function. Establish a connection to a MySQL database engine.


bool mysqli_real_connect(connection,host,username,password,db,port,socket,flag);

mysqli_real_connect() Function Parameter

connection :Required parameter. The MySQL connection to be used
host :Required parameter. Can be either a host name or an IP address.
username :Required parameter. The MySQL user name.
password :Required parameter. The MySQL password.
db :Optional parameter. The default database to be used
port :Optional parameter. Mention port number to attempt to connect to the MySQL server.
socket :Optional parameter. Mention socket or named pipe that should be used.
flag :Optional parameter. With the parameter flags you can set different connection options:

  • MYSQLI_CLIENT_COMPRESS - Use compression protocol.
  • MYSQLI_CLIENT_FOUND_ROWS - return number of matched rows, not the number of affected rows.
  • MYSQLI_CLIENT_IGNORE_SPACE - Allow spaces after function names. Makes all function names reserved words.
  • MYSQLI_CLIENT_INTERACTIVE - Allow interactive_timeout seconds of inactivity before closing the connection.
  • MYSQLI_CLIENT_SSL - Use SSL (encryption)
  • MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT - Like MYSQLI_CLIENT_SSL, but disables validation of the provided SSL certificate.

mysqli_real_connect() Function Return Value

Return Values :Returns TRUE on success or FALSE on failure.

mysqli_real_connect() Function Example

$con = mysqli_init();

if (!$con)
 die("init failed");

if (!mysqli_real_connect($con,"localhost","user","password","db"))
 die("Connect Error: " . mysqli_connect_error());