mysqli::real_connect
mysqli_real_connect
(PHP 5, PHP 7, PHP 8)
mysqli::real_connect -- mysqli_real_connect — 建立一个 MySQL 服务器连接
说明
面向对象风格
?string
$hostname = null,?string
$username = null,#[\SensitiveParameter] ?string
$password = null,?string
$database = null,?int
$port = null,?string
$socket = null,int
$flags = 0): bool
过程化风格
mysqli
$mysql,?string
$hostname = null,?string
$username = null,#[\SensitiveParameter] ?string
$password = null,?string
$database = null,?int
$port = null,?string
$socket = null,int
$flags = 0): bool
建立一个到 MySQL 服务器的链接。
与 mysqli_connect() 的不同点:
- 
    
mysqli_real_connect() 需要一个由 mysqli_init() 创建的有效对象。
 - 
    
可以使用 mysqli_options() 设置各种连接设置。
 - 
    
提供
flags参数。 
参数
- 
mysql 仅以过程化样式:由 mysqli_connect() 或 mysqli_init() 返回的 mysqli 对象。
hostname- 
      
可以使用域名、IP 地址。如果传递
null,值将从 mysqli.default_host 中检索。如果可能,将使用管道代替 TCP/IP 协议。如果同时提供主机名和端口号,则使用 TCP/IP 协议,例如localhost:3308。 username- 
      
MySQL 登录用户名,为
null时则假设是基于 mysqli.default_user ini 选项的用户名。 password- 
      
MySQL 密码,为
null时则假设是基于 mysqli.default_pw ini 选项的密码。 database- 
      
执行查询语句的默认数据库或为
null。 port- 
      
MySQL 服务器的端口,为
null时则假设是基于 mysqli.default_port ini 选项的端口。 socket- 
      
指定使用的 socket 或者命名通道,为
null时则假设是基于 mysqli.default_socket ini 选项的套接字。注意:
指定
socket参数并不能说明要采用何种方式连接数据库。 连接数据的方式由hostname设定。 flags- 
      
这里可以设置连接参数:
Supported flags Name Description MYSQLI_CLIENT_COMPRESS使用压缩协议 MYSQLI_CLIENT_FOUND_ROWS返回语句匹配的行数,而不是影响的行数 MYSQLI_CLIENT_IGNORE_SPACE允许函数名称后有空格,这将使所有的函数名称成为保留字。 MYSQLI_CLIENT_INTERACTIVE在关闭连接之前允许等待 interactive_timeout秒, 他替代wait_timeout设定。MYSQLI_CLIENT_SSL使用 SSL 加密 MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT跟 MYSQLI_CLIENT_SSL类似,但禁用对提供的 SSL 证书的验证。这仅适用于使用 MySQL Native Driver 和 MySQL 5.6 及其后续版本的安装。注意:
从安全角度考虑,在 PHP 中不可以使用
MULTI_STATEMENT, 若要执行多查询语句,请使用 mysqli_multi_query()。 
错误/异常
If mysqli error reporting is enabled (MYSQLI_REPORT_ERROR) and the requested operation fails,
a warning is generated. If, in addition, the mode is set to MYSQLI_REPORT_STRICT,
a mysqli_sql_exception is thrown instead.
更新日志
| 版本 | 说明 | 
|---|---|
| 7.4.0 | 所有的参数都可为 null。 | 
示例
示例 #1 mysqli::real_connect() 示例
面向对象风格
<?php
$mysqli = mysqli_init();
if (!$mysqli) {
    die('mysqli_init failed');
}
if (!$mysqli->options(MYSQLI_INIT_COMMAND, 'SET AUTOCOMMIT = 0')) {
    die('Setting MYSQLI_INIT_COMMAND failed');
}
if (!$mysqli->options(MYSQLI_OPT_CONNECT_TIMEOUT, 5)) {
    die('Setting MYSQLI_OPT_CONNECT_TIMEOUT failed');
}
if (!$mysqli->real_connect('localhost', 'my_user', 'my_password', 'my_db')) {
    die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
echo 'Success... ' . $mysqli->host_info . "\n";
$mysqli->close();
?>面向对象风格 when extending mysqli class
<?php
class foo_mysqli extends mysqli {
    public function __construct($host, $user, $pass, $db) {
parent::__construct();
        if (!parent::options(MYSQLI_INIT_COMMAND, 'SET AUTOCOMMIT = 0')) {
            die('Setting MYSQLI_INIT_COMMAND failed');
        }
        if (!parent::options(MYSQLI_OPT_CONNECT_TIMEOUT, 5)) {
            die('Setting MYSQLI_OPT_CONNECT_TIMEOUT failed');
        }
        if (!parent::real_connect($host, $user, $pass, $db)) {
            die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
        }
    }
}
$db = new foo_mysqli('localhost', 'my_user', 'my_password', 'my_db');
echo 'Success... ' . $db->host_info . "\n";
$db->close();
?>过程化风格
<?php
$link = mysqli_init();
if (!$link) {
    die('mysqli_init failed');
}
if (!mysqli_options($link, MYSQLI_INIT_COMMAND, 'SET AUTOCOMMIT = 0')) {
    die('Setting MYSQLI_INIT_COMMAND failed');
}
if (!mysqli_options($link, MYSQLI_OPT_CONNECT_TIMEOUT, 5)) {
    die('Setting MYSQLI_OPT_CONNECT_TIMEOUT failed');
}
if (!mysqli_real_connect($link, 'localhost', 'my_user', 'my_password', 'my_db')) {
    die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
echo 'Success... ' . mysqli_get_host_info($link) . "\n";
mysqli_close($link);
?>以上示例会输出:
Success... MySQL host info: localhost via TCP/IP
注释
注意:
MySQLnd 总是使用服务器的默认字符集。此字符集在连接握手/认证时发送,并被 mysqlnd 使用。
Libmysqlclient 使用 my.cnf 中的默认字符集或者由在调用 mysqli_init() 之后,mysqli_real_connect() 之前先调用 mysqli_options() 来指定。
参见
- mysqli_connect() - 别名 mysqli::__construct
 - mysqli_init() - 初始化 MySQLi 并返回用于 mysqli_real_connect() 的对象
 - mysqli_options() - 设置选项
 - mysqli_ssl_set() - 用于使用 SSL 建立安全连接
 - mysqli_close() - 关闭先前打开的数据库连接