Php 是否有方法检查结果是否为表中的最后一个(最高ID)?
我有一个用来显示博客文章的页面。在页面的开头,为了获取数据,我使用以下SQL:Php 是否有方法检查结果是否为表中的最后一个(最高ID)?,php,mysql,Php,Mysql,我有一个用来显示博客文章的页面。在页面的开头,为了获取数据,我使用以下SQL: SELECT posts.*, count(comments.post_id) as number_of_comments from posts left join comments on (posts.post_id = comments.post_id) WHERE post_url LIKE '$post_url' group by posts.post_id 我在页面末尾添加了一个“阅读下一篇文章”
SELECT posts.*, count(comments.post_id) as number_of_comments from posts left join comments on (posts.post_id = comments.post_id) WHERE post_url LIKE '$post_url' group by posts.post_id
我在页面末尾添加了一个“阅读下一篇文章”按钮,按下该按钮时,会找到当前显示的文章的ID,然后从db获取下一篇文章:
$(".readNext").click(function(){
var result = "";
var postID = "postID=" + $(".post").attr("id");
$.get("/retrieveNextPost", postID, function (data) {
//SomeFunction(data);
window.location = '/blog/' + data;
});
});
<?php
include('dbconnect.php');
if (isset($_GET['postID']))
{
$post_id = $_GET['postID'];
}
$SQL = "SELECT posts.*, count(comments.post_id) as number_of_comments from posts left join comments on (posts.post_id = comments.post_id) WHERE posts.post_id > $post_id group by posts.post_id ORDER BY post_id ASC LIMIT 0, 1";
$result = mysqli_query($link, $SQL);
$row = mysqli_fetch_array($result);
echo $row['post_url'];
?>
$(“.readNext”)。单击(函数(){
var结果=”;
var postID=“postID=”+$(“.post”).attr(“id”);
$.get(“/retrieveNextPost”,postID,函数(数据){
//函数(数据);
window.location='/blog/'+数据;
});
});
这非常有效-直到你看到最后一篇(最新/最高ID)的博客文章。在这一点上-我想从页面上完全删除按钮。因为显示它没有意义
有没有办法修改页面顶部的SQL以标记某种Y/N标志,如果为true,我可以使用该标志隐藏按钮
i、 e.如果当前博客帖子记录是表中最后一个/最高ID,请将标志设置为true。您可以向包含最大帖子ID的帖子添加另一个左连接。如果此连接匹配(不为null),则您位于最高帖子 不经意间:
SELECT posts.*,
count(comments.post_id) as number_of_comments,
-- if post_id matches then is highest
case when maxPosts.post_id is not null then 1 else 0 end as IsLast
from posts
left join comments on (posts.post_id = comments.post_id)
left join (
-- get max post_id and join on current post_id
select post_id from posts order by post_id desc Limit 1
) maxPosts on post.post_id = maxPosts.post_id
WHERE post_url LIKE '$post_url' group by posts.post_id
您可以向包含最大post_id的post添加另一个左联接。如果此项匹配(不为空),则您处于最高post 不经意间:
SELECT posts.*,
count(comments.post_id) as number_of_comments,
-- if post_id matches then is highest
case when maxPosts.post_id is not null then 1 else 0 end as IsLast
from posts
left join comments on (posts.post_id = comments.post_id)
left join (
-- get max post_id and join on current post_id
select post_id from posts order by post_id desc Limit 1
) maxPosts on post.post_id = maxPosts.post_id
WHERE post_url LIKE '$post_url' group by posts.post_id
您可以在mysql查询中使用限制和降序
SELECT * FROM yourtable ORDER BY id DESC LIMIT 1
将“限制”设置为1将允许您仅获得1个结果。使用desc从最高到最低对id进行排序您可以在mysql查询中使用limit和descending
SELECT * FROM yourtable ORDER BY id DESC LIMIT 1
将“限制”设置为1将允许您仅获得1个结果。并使用desc将id从最高到最低排序您可以向select查询中添加一个MAX(posts.post\u id)
,并检查当前帖子是否是最后一篇
SELECT
posts.*, MAX(posts.post_id) as last_post,
count(comments.post_id) as number_of_comments
FROM posts
LEFT JOIN comments on (posts.post_id = comments.post_id)
WHERE post_url LIKE '$post_url'
GROUP BY posts.post_id
然后在php
中,将输出返回为json
$row = mysqli_fetch_array($result);
if ($row['post_id'] == $row['last_post']) $last = true;
else $last = false;
header('Content-Type: application/json');
echo json_encode( array(
'url'=>$row['post_url'],
'isLast' => $last
)
);
使用javascript部分中的json
,测试是否已到达最后一篇文章,如果为true,只需删除按钮即可
$(".readNext").click(function(){
var result = "";
var postID = "postID=" + $(".post").attr("id");
$.get("/retrieveNextPost", postID, function (data) {
if (data.isLast == true) $(this).remove();
else window.location = '/blog/' + data.url;
});
});
您可以在select查询中添加MAX(posts.post\u id)
,并检查当前帖子是否是最后一篇
SELECT
posts.*, MAX(posts.post_id) as last_post,
count(comments.post_id) as number_of_comments
FROM posts
LEFT JOIN comments on (posts.post_id = comments.post_id)
WHERE post_url LIKE '$post_url'
GROUP BY posts.post_id
然后在php
中,将输出返回为json
$row = mysqli_fetch_array($result);
if ($row['post_id'] == $row['last_post']) $last = true;
else $last = false;
header('Content-Type: application/json');
echo json_encode( array(
'url'=>$row['post_url'],
'isLast' => $last
)
);
使用javascript部分中的json
,测试是否已到达最后一篇文章,如果为true,只需删除按钮即可
$(".readNext").click(function(){
var result = "";
var postID = "postID=" + $(".post").attr("id");
$.get("/retrieveNextPost", postID, function (data) {
if (data.isLast == true) $(this).remove();
else window.location = '/blog/' + data.url;
});
});
在上面允许您在查询中使用LIMIT&DESC获取最新行的帖子之上,还有其他方法可以简化它 例如: PDO:您可以访问lastInsertId();函数,该函数返回行的最新id
MySQLi:在您的情况下,我相信您正在使用MySQLi,因此您可以使用MySQLi_insert_id()函数,该函数的工作方式与上面的PDO函数类似。在上面的文章中,允许您在查询中使用LIMIT&DESC获取最新的行,还有其他方法可以简化它 例如: PDO:您可以访问lastInsertId();函数,该函数返回行的最新id
MySQLi:在您的情况下,我相信您正在使用MySQLi,因此您可以使用MySQLi_insert_id()函数,该函数的工作方式与上面的PDO函数类似。我认为您可以通过
SELECT*从某物中按天顺序进行选择代码>并按日期加载帖子。因此,您有一个循环来输出结果。将计数器连接到回路上。当计数器小于数组中的项目数时,先做一些事情,然后再做其他事情。我想你可以通过SELECT*FROM something ORDER by day\u of_ORDER代码>并按日期加载帖子。因此,您有一个循环来输出结果。将计数器连接到回路上。当计数器小于数组的项数时,请先执行某项操作,然后再执行其他操作。这些函数仅在当前连接会话中执行插入时起作用,如果在未执行插入的页面上使用,它们将返回NULL。在这种情况下,这些函数不是很有用,因为OP没有向数据库写入任何内容。这些函数仅在当前连接会话中执行插入时起作用,如果在没有插入的页面上使用,它们将返回NULL。在这种情况下,它们不是很有用,因为OP没有向数据库写入任何内容。这非常完美。关于简单使用orderby/limit的其他建议忽略了我最初的查询,它们是搜索特定的记录。通过实现这个SQL,我现在能够在我的PHP中执行以下检查:if($row['IsLast']!=1),如果这是真的,我将回显按钮的HTML代码。如果为false,我什么也不做——并且按钮永远不会为用户输出到屏幕上。非常感谢你的帮助。这太完美了。关于简单使用orderby/limit的其他建议忽略了我最初的查询,它们是搜索特定的记录。通过实现这个SQL,我现在能够在我的PHP中执行以下检查:if($row['IsLast']!=1),如果这是真的,我将回显按钮的HTML代码。如果为false,我什么也不做——并且按钮永远不会为用户输出到屏幕上。非常感谢你的帮助。