Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP和MySQL:按最新日期订购,限制为10_Php_Mysql_Sql Order By_Limit_Querying - Fatal编程技术网

PHP和MySQL:按最新日期订购,限制为10

PHP和MySQL:按最新日期订购,限制为10,php,mysql,sql-order-by,limit,querying,Php,Mysql,Sql Order By,Limit,Querying,我正在我的网站上构建一个notes系统,我已经到了一个阶段,用户可以使用PHP将notes发布到MySQL数据库中,然后PHP将其打印到页面上。但是,当它们打印/回显时,最旧的会首先出现,但我希望最新的会首先出现。我也希望他们被限制在10个,所以只有10个出现在页面上。这是我的PHP代码,非常感谢您的帮助: // initialize some variables $notedisplaylist = ""; $myObject = ""; $result = mysql_query("SELE

我正在我的网站上构建一个notes系统,我已经到了一个阶段,用户可以使用PHP将notes发布到MySQL数据库中,然后PHP将其打印到页面上。但是,当它们打印/回显时,最旧的会首先出现,但我希望最新的会首先出现。我也希望他们被限制在10个,所以只有10个出现在页面上。这是我的PHP代码,非常感谢您的帮助:

// 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 />';
}
//初始化一些变量
$notedisplaylist=“”;
$myObject=“”;
$result=mysql\u query(“从notes中选择*,其中notes\u author\u id='$u\u id'按日期和时间排序”);
while($row=mysql\u fetch\u数组($result)){
$note_title=$row[“note_title”];
$note_body=$row[“note_body”];
$date=$row[“日期和时间”];
$notedisplaylist.='.$note\u title.$note\u body.


注:'.$date.'



; }
试试看

有关
订单
限额
的更详细说明,请访问MySQL文档中有关和基本的文章(查找描述
限额
)的项目符号。

给出类似的说明

 ORDER BY date_time DESC
否则,您将按升序对它们进行排序。。这就是为什么年长者优先考虑的原因这应该做到:

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

DESC:降序(从最新到最旧) 限制10:找到前10条记录。

执行此操作

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

如果您希望限制为变量,我将其命名为$LIMIT:

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