dqnek0079 2018-03-19 03:23
浏览 61
已采纳

mysql_fetch_array()TOP(10)的问题[重复]

trying to make a compliments area of my test site

$result = mysql_query("SELECT message, username FROM compliments order by date DESC");
while($r = mysql_fetch_array($result))    
{
    $Name = $r["username"];
    $Message = $r["message"];
}

this however, gets all the messages,

i want to limit to only the most recent 10

so i tried this..

$result = mysql_query("SELECT TOP(10) message, username FROM compliments order by date ASC");
while($r = mysql_fetch_array($result))    
{
    $Name = $r["username"];
    $Message = $r["message"];
}

throws an error

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /var/www/vhosts/localhost/httpdocs/readcompliments.php on line 52

how can i do this successfuly?

EDIT:

Now using

SELECT  message, username FROM compliments order by date ASC LIMIT 10

and it works! but the problem is, that its getting the oldest 10! how do i make it so it gets the most recent 10?

</div>
  • 写回答

3条回答 默认 最新

  • donglanying3855 2018-03-19 03:26
    关注

    IF you are using Mysql

    TOP is for sqlServer,LIMIT for Mysql

    SELECT  message, username FROM compliments order by date ASC LIMIT 10
    

    Instead of

    SELECT TOP(10) message, username FROM compliments order by date ASC
    

    EDIT

    If you want to get most recent 10,You can use order by date DESC

    SELECT  message, username FROM compliments order by date DESC LIMIT 10
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历