• 首页
  • vue
  • TypeScript
  • JavaScript
  • scss
  • css3
  • html5
  • php
  • MySQL
  • redis
  • jQuery
  • mysqli::real_connect()

    (PHP 5, PHP 7)

    建立一个 MySQL 服务器连接

    说明

    面向对象风格
    mysqli::real_connect([string $host[,string $username[,string $passwd[,string $dbname[,int $port[,string $socket[,int $flags]]]]]]]): bool
    过程化风格
    mysqli_real_connect(mysqli$link[,string $host[,string $username[,string $passwd[,string $dbname[,int $port[,string $socket[,int $flags]]]]]]]): bool

    建立一个到 MySQL 服务器的链接。

    与mysqli_connect()的不同点:

    • mysqli_real_connect()需要一个有效的对象,这个对象由mysqli_init()创建。

    • 可以使用mysqli_options()设置各种连接设置。

    • 提供$flags参数。

    参数

    $link

    仅以过程化样式:由mysqli_connect()或mysqli_init()返回的链接标识。

    $host

    可以使用域名、IP 地址。如果传送NULL或者字符串"localhost"那么会使用通道替代 TCP/IP 连接本地服务器。

    $username

    MySQL 登录用户名

    $passwd

    如果设置NULL,那么会使用没有密码验证的方式尝试登录。这样可以为一个用户提供不同的权限,基于他是否提供了密码。

    $dbname

    设置执行查询语句的默认数据库。

    $port

    指定 MySQL 服务器的端口

    $socket

    指定使用的 socket 或者命名通道。

    Note:

    指定$socket参数并不能说明要采用何种方式连接数据库。连接数据的方式由$host设定。

    $flags

    这里可以设置连接参数:

    Supported flags
    NameDescription
    MYSQLI_CLIENT_COMPRESS使用压缩协议
    MYSQLI_CLIENT_FOUND_ROWS返回语句匹配的行数,而不是影响的行数
    MYSQLI_CLIENT_IGNORE_SPACE允许函数名称后有空格,这将使所有的函数名称成为保留字。
    MYSQLI_CLIENT_INTERACTIVE在关闭连接之前允许等待interactive_timeout秒,他替代wait_timeout设定。
    MYSQLI_CLIENT_SSL使用 SSL 加密
    Note:

    从安全角度考虑,在 PHP 中不可以使用MULTI_STATEMENT,若要执行多查询语句,请使用mysqli_multi_query()。

    返回值

    成功时返回TRUE,或者在失败时返回FALSE

    范例

    Example #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::init();
            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
    

    注释

    Note:

    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() 关闭先前打开的数据库连接
    this is because it tries to use sockets with the string localhost this is documented en known
    Notice that when using "localhost" as hostname the port option might be ignored. 
    If you wish to connect thru a different port than default, substitute "127.0.0.1" for localhost.