1 回答

TA貢獻(xiàn)1843條經(jīng)驗 獲得超7個贊
example.com?pid=10
假設(shè)您在瀏覽器地址欄中輸入,您可以pid
使用$_GET
數(shù)組捕獲該變量,當(dāng)使用查詢字符串調(diào)用頁面時,PHP 會自動為您填充該變量。
使用現(xiàn)有代碼作為起點,您可以
<?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 關(guān)注
- 122 瀏覽
添加回答
舉報