我正在尝试找出一种方法,让一个 PHP 页面显示我的所有博客文章,但让 URL 决定从该数据库请求什么文章。有点像这样:localhost/bolg/posts.php?pid=1在我的数据库中,我将其设置为每个帖子都有一个与之关联的 ID。所以我想要的是将 pid=1 并将其放入 MySQL 代码中。这是 post.php 的 PHP 代码
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, title, content, date FROM posts where id =3";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<h1> ". $row["title"]. "</h1>". $row["content"]. "" . $row["date"] . "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
蓝山帝景