PHP 连接 MySQL

<?php
$servername = "localhost:8889";
$username = "root";
$password = "root";

// 创建连接
$conn = mysqli_connect($servername, $username, $password);

// 检测连接
if (!$conn) {
    die("连接失败: " . $conn->connect_error);
}
echo "连接成功";
?>

面向对象的操作

<?php
$servername = "localhost:8889";
$username = "root";
$password = "root";

// 创建连接
$conn = new mysqli($servername, $username, $password);

// 检测连接
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}
echo "连接成功";
?>
$conn->close();