Current Location: Home> Function Categories> mysqli::real_connect

mysqli::real_connect

(mysqli_real_connect) Open the connection to the mysql server
Name:mysqli::real_connect
Category:MySQLi
Programming Language:php
One-line Description:Open a new MySQL server connection.

Definition and usage

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

The difference between this function and connect() is that:

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

Example

Example 1 - Object-Oriented Style

Open a new MySQL server connection and set additional connection options:

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

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

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

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

Example 2 - Procedural Style

Open a new MySQL server connection and set additional connection options:

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

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

// Specify the option to read from named files instead of my.cnf
mysqli_options ( $con , MYSQLI_READ_DEFAULT_FILE , "myfile.cnf" ) ;

mysqli_real_connect ( $con , "localhost" , "my_user" , "my_password" , "my_db" ) ;
?>
Similar Functions
Popular Articles