编程小白

编程小白

php如何操作数据库

默认分类 0 评

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

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

// 检查连接是否成功
if (!$conn) {

die("连接失败: " . mysqli_connect_error());

}

// 执行查询
$sql = "SELECT * FROM your_table";
$result = mysqli_query($conn, $sql);

// 检查查询是否成功
if (mysqli_num_rows($result) > 0) {

while($row = mysqli_fetch_assoc($result)) {
    echo "ID: " . $row["id"]. " - Name: " . $row["name"]. "<br>";
}

} else {

echo "0 结果";

}

// 关闭连接
mysqli_close($conn);