MySQL和PHP - 从数据库中获取最新评论
问题描述:
I have this code:
$sql = "SELECT * FROM deptcomment WHERE deptPostID ='$postID' ORDER BY deptCommentRegDate ASC, deptCommentRegTime ASC LIMIT 4";
It is working but it only gets the first 4 comments of a particular post. i need this code to get the last 4 comments in ASC order. Is this possible.
Any help appreciated.
我有这段代码: p>
$ sql =“SELECT * FROM deptcomment WHERE deptPostID ='$ postID'ORDER BY deptCommentRegDate ASC,deptCommentRegTime ASC LIMIT 4“;
code> pre>
它正在运行,但它只获得前4条评论 一个特定的帖子。 我需要此代码以ASC顺序获取最后4条评论。 这是可能的。 p>
感谢任何帮助。 p>
div>
答
Try
$sql = "SELECT * FROM deptcomment WHERE deptPostID ='$postID' ORDER BY deptCommentRegDate ASC, deptCommentRegTime DESC LIMIT 4";
... and then run array_reverse();
on the result.
While I have you, I notice that you're constructing your query in such as way that suggests you intend on passing it on to a mysql_*
type function. Please keep in mind that these types of functions have been deprecated, and that you are advised to use a library such as PDO to communicate with the database instead.