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 关注
- 88 浏览
添加回答
举报