PHP和MySQL:按最新日期排序,限制为10 [英] PHP and MySQL: Order by most recent date and limit 10

查看:69
本文介绍了PHP和MySQL:按最新日期排序,限制为10的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在网站上构建一个便笺系统,现在用户可以使用PHP将便笺发布到MySQL数据库,然后PHP将其打印在页面上.但是,当他们打印/回显时,最早的显示在最前面,但我要显示最新的在列.我也希望将它们限制为10个,因此页面上仅显示10个.这是我的PHP代码,非常感谢您的帮助:

I am building a notes system on my site and I've got to the stage where users can post notes into the MySQL database using PHP and then PHP prints them out on a page. However, when they print/echo out, the oldest one appears first but I want the most recent first. I also want them to be limited to 10, so only 10 appear on the page. Here is my PHP code, your help will be much appreciated:

// initialize some variables
$notedisplaylist = "";
$myObject = "";
$result = mysql_query("SELECT * FROM notes WHERE note_author_id='$u_id' ORDER BY         date_time");

while($row = mysql_fetch_array($result)){
  $note_title = $row["note_title"];
  $note_body = $row["note_body"];
  $date = $row["date_time"];
  $notedisplaylist .= '<h2>' . $note_title . '</h2><br /><p>' . $note_body . '</p><hr /><p>Noted: ' . $date . '</p><hr /><br />';
}

推荐答案

这应该做到:

$result = mysql_query("SELECT * FROM notes WHERE note_author_id='$u_id' ORDER BY date_time DESC LIMIT 0, 10");

这篇关于PHP和MySQL:按最新日期排序,限制为10的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆