PHP and MySQL: Order by most recent date and limit 10

故事扮演 提交于 2019-11-28 01:50:26

This should do it :

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

use:

SELECT * FROM notes WHERE note_author_id='$u_id' ORDER BY date_time DESC LIMIT 10

DESC : descending order ( from newest to oldest ) LIMIT 10: first 10 records found.

Try

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

For a more detailed explanation on ORDER and LIMIT, visit the MySQL doc's articles about sorting rows and the basic select syntax (look for a bullet describing LIMIT).

give like

 ORDER BY date_time DESC

otherwise you are sorting them in ascending order.. thats why older ones come first

Do this

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

If in case you want your LIMIT to be a variable, here I named it $limit:

"SELECT * FROM tbl ORDER BY input_date DESC LIMIT 0, $limit";
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!