mysqli_connect

(PHP 5, PHP 7, PHP 8)

mysqli_connect别名 mysqli::__construct()

说明

此函数是该函数的别名: mysqli::__construct()

虽然说在 mysqli::__construct() 的文档 对 mysqli_connect() 函数也进行了详细的说明, 这里依然给出一个简单的示例:

范例

Example #1 mysqli_connect() 例程

<?php
$link 
mysqli_connect("127.0.0.1""my_user""my_password""my_db");

if (!
$link) {
    echo 
"Error: Unable to connect to MySQL." PHP_EOL;
    echo 
"Debugging errno: " mysqli_connect_errno() . PHP_EOL;
    echo 
"Debugging error: " mysqli_connect_error() . PHP_EOL;
    exit;
}

echo 
"Success: A proper connection to MySQL was made! The my_db database is great." PHP_EOL;
echo 
"Host information: " mysqli_get_host_info($link) . PHP_EOL;

mysqli_close($link);
?>

以上例程的输出类似于:

Success: A proper connection to MySQL was made! The my_db database is great.
Host information: localhost via TCP/IP

User Contributed Notes

mparsa1372 at gmail dot com 12-Mar-2021 02:58
Example (MySQLi Object-Oriented)

<?php
$servername
= "localhost";
$username = "username";
$password = "password";

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
  die(
"Connection failed: " . $conn->connect_error);
}
echo
"Connected successfully";
?>