目录

PHP mysqli real_connect() 函数

❮ PHP MySQLi 参考

示例 - 面向对象风格

打开与 MySQL 服务器的新连接,并带有额外的连接选项:

<?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");
?> 

查看底部的程序样式示例。


定义和用法

real_connect() / mysqli_real_connect() 函数打开与 MySQL 服务器的新连接。

该函数不同于连接()通过以下方式:

  • real_connect() 需要一个由以下方法创建的有效对象在里面()
  • real_connect() 可以与 options() 一起使用来设置连接的不同选项
  • real_connect() 有一个标志参数

语法

面向对象风格:

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

程序风格:

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

参数值

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

技术细节

返回值: 成功则为真。失败时为 FALSE
PHP 版本: 5+
PHP 变更日志: PHP 5.6:添加了 MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT 标志

示例 - 程序风格

打开与 MySQL 服务器的新连接,并带有额外的连接选项:

<?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 参考