PHP mysqli real_connect() Function

❮ PHP MySQLi Reference

Example - Object Oriented style

Open a new connection to the MySQL server, with extra connect options:

<?php
$mysqli = mysqli_init();
if (!$mysqli) {
  die("mysqli_init failed");
}

// Specify connection timeout
$con -> options(MYSQLI_OPT_CONNECT_TIMEOUT, 10);

// Specify read options from named file instead of my.cnf
$con -> options(MYSQLI_READ_DEFAULT_FILE, "myfile.cnf");

$con -> real_connect("localhost","my_user","my_password","my_db");
?> 

Look at example of procedural style at the bottom.


Definition and Usage

The real_connect() / mysqli_real_connect() function opens a new connection to the MySQL server.

This function differs from connect() in the following ways:

  • real_connect() requires a valid object created by init()
  • real_connect() can be used with options() to set different options for the connection
  • real_connect() has a flag parameter

Syntax

Object oriented style:

$mysqli -> real_connect(host, username, password, dbname, port, socket, flag)

Procedural style:

mysqli_real_connect(connection, host, username, password, dbname, port, socket, flag)

Parameter Values

Parameter Description
connection Required. Specifies the MySQL connection to use
host Optional. Specifies a host name or an IP address
username Optional. Specifies the MySQL username
password Optional. Specifies the MySQL password
dbname Optional. Specifies the default database to be used
port Optional. Specifies the port number to attempt to connect to the MySQL server
socket Optional. Specifies the socket or named pipe to be used
flag Optional. Specifies different connection options. Possible values:
  • MYSQLI_CLIENT_COMPRESS - Use compression protocol
  • MYSQLI_CLIENT_FOUND_ROWS - Return number of matched rows (not affected 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
  • MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT

Technical Details

Return Value: TRUE on success. FALSE on failure
PHP Version: 5+
PHP Changelog: PHP 5.6: Added MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT flag

Example - Procedural style

Open a new connection to the MySQL server, with extra connect options:

<?php
$con = mysqli_init();
if (!$con) {
  die("mysqli_init failed");
}

// Specify connection timeout
mysqli_options($con, MYSQLI_OPT_CONNECT_TIMEOUT, 10);

// Specify read options from named file instead of my.cnf
mysqli_options($con, MYSQLI_READ_DEFAULT_FILE, "myfile.cnf");

mysqli_real_connect($con,"localhost","my_user","my_password","my_db");
?> 


❮ PHP MySQLi Reference
Copyright 1999-2023 by Refsnes Data. All Rights Reserved.