打开与 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();
}
?>
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!