1 回答

TA貢獻1843條經驗 獲得超7個贊
example.com?pid=10
假設您在瀏覽器地址欄中輸入,您可以pid
使用$_GET
數組捕獲該變量,當使用查詢字符串調用頁面時,PHP 會自動為您填充該變量。
使用現有代碼作為起點,您可以
<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
if (isset($_GET['pid'])) {
? ? // Create connection
? ? $conn = new mysqli($servername, $username, $password, $dbname);
? ??
? ? $sql = "SELECT id, title, content, date FROM posts where id = ?";
? ? $stmt = $conn->prepare($sql);
? ? $stmt->bind_param('i', $_GET['pid']);
? ? $stmt->execute();
? ? $result = $stmt->get_result();
? ? if ($result->num_rows > 0) {
? ? ? ? // output data of each row
? ? ? ? // while looop is not necessary, you are only returning one row
? ? ? ? $row = $result->fetch_assoc();
? ? ? ? echo "<h1> ". $row["title"]. "</h1>". $row["content"]. "" . $row["date"] . "<br>";
? ? }
? ? $conn->close();
} else {
? ? echo "0 results";
}
- 1 回答
- 0 關注
- 117 瀏覽
添加回答
舉報