PHP mysqli_real_connect()Functions

PHP MySQLi 参考手册PHP MySQLi Reference Manual

Examples

Opens a new connection to the MySQL server:

<?php
$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);
?>

Definition and Usage

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

The mysqli_real_connect() function and themysqli_connect()() function differ in the following aspects:

  • mysqli_real_connect() requires a valid object created by themysqli_init()() function.
  • mysqli_real_connect() can be used with mysqli_options() to set different connection options.
  • mysqli_real_connect() has a flag parameter.

Syntax

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

Parameters Description
connection Required. Specifies the MySQL connection to use.
host Optional. Specifies the hostname or IP address.
username Optional. Specifies the MySQL username.
password Optional. Specifies the MySQL password.
dbname Optional. Specifies the default database to use.
port Optional. Specifies the port number to attempt to connect to the MySQL server.
socket Optional. Specifies the socket or named pipe to use.
flag Optional. Specifies different connection options. Possible values:
  • MYSQLI_CLIENT_COMPRESS - Use compressed protocol
  • MYSQLI_CLIENT_FOUND_ROWS - Return the number of matched rows (not the number of affected rows)
  • MYSQLI_CLIENT_IGNORE_SPACE - Allow spaces after function names, making function names reserved words
  • MYSQLI_CLIENT_INTERACTIVE - Allow interactive_timeout seconds of inactivity before closing the connection
  • MYSQLI_CLIENT_SSL - Use SSL encryption

Technical Details

Return value: Returns TRUE on success, FALSE on failure.
PHP version: 5+


PHP MySQLi 参考手册PHP MySQLi Reference Manual Other Extensions