Web Design Laboratory
建立資料庫連線
執行 SQL 查詢
處理查詢結果
關閉連線
完整範例
<?php
$servername = "your_servername";
$username = "your_username";
$password = "your_password";
$dbname = "your_dbname";
// 建立連線
$conn = new mysqli($servername, $username, $password, $dbname);
// 檢查連線是否成功
if ($conn->connect_error) {
die("連線失敗: " . $conn->connect_error);
}
$sql = "SELECT * FROM YourTable";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// 输出数据
echo "<table><tr><th>ID</th><th>Name</th><th>Email</th></tr>";
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["id"]. "</td><td>" . $row["name"]. "</td><td>" . $row["email"]. "</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?>