目录

PHP mysqli connect() 函数

❮ PHP MySQLi 参考

示例 - 面向对象风格

打开与 MySQL 服务器的新连接:

<?php
$mysqli = new mysqli("localhost","my_user","my_password","my_db");

// Check connection
if ($mysqli -> connect_errno) {
  echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
  exit();
}
?>

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


定义和用法

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


语法

面向对象风格:

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

程序风格:

mysqli_connect( host, username, password, dbname, port, socket)

参数值

Parameter Description
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

技术细节

返回值: 返回一个表示与 MySQL 服务器的连接的对象
PHP 版本: 5+

示例 - 程序风格

打开与 MySQL 服务器的新连接:

<?php
$con = mysqli_connect("localhost","my_user","my_password","my_db");

// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  exit();
}
?>


❮ PHP MySQLi 参考