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 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来