Latest web development tutorials

PHP mysqli_real_connect () function

PHP MySQLi Reference Manual PHP MySQLi Reference Manual

Examples

Open a new connection to a 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

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

mysqli_real_connect () function and mysqli_connect () functions differ in the following aspects:

  • mysqli_real_connect () a claim by the () mysqli_init valid object created.
  • mysqli_real_connect () can mysqli_options () with the use of different options to set up the connection.
  • mysqli_real_connect () has a flag parameter.

grammar

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

参数 描述
connection 必需。规定要使用的 MySQL 连接。
host 可选。规定主机名或 IP 地址。
username 可选。规定 MySQL 用户名。
password 可选。规定 MySQL 密码。
dbname 可选。规定要使用的默认数据库。
port 可选。规定尝试连接到 MySQL 服务器的端口号。
socket 可选。规定 socket 或要使用的已命名 pipe。
flag 可选。规定不同的连接选项。可能的值:
  • MYSQLI_CLIENT_COMPRESS - 使用压缩协议
  • MYSQLI_CLIENT_FOUND_ROWS - 返回匹配的行数(不是受影响的行数)
  • MYSQLI_CLIENT_IGNORE_SPACE - 在函数名后允许空格,使函数名保留字
  • MYSQLI_CLIENT_INTERACTIVE - 在关闭连接之前允许不活动的 interactive_timeout 秒
  • MYSQLI_CLIENT_SSL - 使用 SSL 加密

technical details

return value: If successful it returns TRUE, on failure returns FALSE.
PHP version: 5+


PHP MySQLi Reference Manual PHP MySQLi Reference Manual