Php 如何从url获取帖子id?
blog.php 网址:Php 如何从url获取帖子id?,php,url,Php,Url,blog.php 网址: 我想从url获取帖子id。但我不知道怎么做? 如果你能帮助我,我会很高兴的 read.php <?php $sql_query = "SELECT * FROM posts WHERE post_id = ?"; $select_all_posts = mysqli_query($conn, $sql_query); while ($row = mys
我想从url获取帖子id。但我不知道怎么做?
如果你能帮助我,我会很高兴的
read.php
<?php
$sql_query = "SELECT * FROM posts WHERE post_id = ?";
$select_all_posts = mysqli_query($conn, $sql_query);
while ($row = mysqli_fetch_assoc($select_all_posts)) {
$post_date = $row["post_date"];
$date = strtotime($post_date);
$newdate = date("d/m/Y", $date);
$post_title = $row["post_title"];
$post_text = $row["post_text"];
$post_image = $row["post_image"];
}
?>
我可以建议下一个解决方案:
<?php
// get page URI and split it by '/'
$uri_arr = explode('/', $_SERVER['REQUEST_URI']);
// get post_id and prevent sql injection
$post_id = mysqli_real_escape_string($conn, $uri_arr[2]);
// run query
$sql_query = "SELECT * FROM posts WHERE post_id = $post_id";
$select_all_posts = mysqli_query($conn, $sql_query);
while ($row = mysqli_fetch_assoc($select_all_posts)) {
$post_date = $row["post_date"];
$date = strtotime($post_date);
$newdate = date("d/m/Y", $date);
$post_title = $row["post_title"];
$post_text = $row["post_text"];
$post_image = $row["post_image"];
}
?>
我可以建议下一个解决方案:
<?php
// get page URI and split it by '/'
$uri_arr = explode('/', $_SERVER['REQUEST_URI']);
// get post_id and prevent sql injection
$post_id = mysqli_real_escape_string($conn, $uri_arr[2]);
// run query
$sql_query = "SELECT * FROM posts WHERE post_id = $post_id";
$select_all_posts = mysqli_query($conn, $sql_query);
while ($row = mysqli_fetch_assoc($select_all_posts)) {
$post_date = $row["post_date"];
$date = strtotime($post_date);
$newdate = date("d/m/Y", $date);
$post_title = $row["post_title"];
$post_text = $row["post_text"];
$post_image = $row["post_image"];
}
?>
它应该在$\u GET
数组中。好的,谢谢,但是怎么做?代码应该是怎样的?我无法为您编写代码,因为我不知道您希望如何使用代码。您可以通过写入print\r($\u GET)来检查查询字符串数组
查看数组中的内容,并根据您的使用情况从中选择。它应该位于$\u GET
数组中。好的,谢谢,但是如何操作?代码应该是怎样的?我无法为您编写代码,因为我不知道您希望如何使用代码。您可以通过写入print\r($\u GET)来检查查询字符串数组
查看阵列中的内容,并根据您的使用情况从阵列中转到阵列。感谢您提供的代码。但它不起作用。我得到了这个错误:警告:mysqli_fetch_assoc()期望参数1是mysqli_result,bool在第51行/51行的C:\xampp\htdocs\Son\blog-icerik.php中给出:while($row=mysqli_fetch_assoc($select_all_posts))您得到了什么错误?你能发布什么返回$\u服务器['REQUEST\u URI']吗?请看这里的示例:错误:mysqli_fetch_assoc()期望参数1是mysqli_结果,第51行给出bool:while($row=mysqli_fetch_assoc($select_all_posts))数组([0]=>1]=>son[2]=>read.php[3]=>lorem ipsum自1500年代以来一直是行业标准的虚拟文本[4]=>42)post\u id:read.phpSo您需要获得$uri\u arr[4]
它将获得42
作为post\u id谢谢您的代码。但它不起作用。我得到了这个错误:警告:mysqli_fetch_assoc()期望参数1是mysqli_result,bool在第51行/51行的C:\xampp\htdocs\Son\blog-icerik.php中给出:while($row=mysqli_fetch_assoc($select_all_posts))您得到了什么错误?你能发布什么返回$\u服务器['REQUEST\u URI']吗?请看这里的示例:错误:mysqli_fetch_assoc()期望参数1是mysqli_结果,第51行给出bool:while($row=mysqli_fetch_assoc($select_all_posts))数组([0]=>1]=>son[2]=>read.php[3]=>lorem ipsum自1500年代以来一直是行业标准的虚拟文本[4]=>42)post\u id:read.phpSo您需要获取$uri\u arr[4]
它将获取42
作为post\u id