doulan7166 2015-07-01 14:52
浏览 23
已采纳

设置While循环限制并按日期排序

I am wanting find a way I can set a limit to only show 1 row from a table and also show the 1 that is by the latest created date which is a column on the row also.

<?php

$q = "SELECT * FROM nots";
$r = mysqli_query($dbc, $q);

 while ($row = mysqli_fetch_array($r)) {

     echo '<div style="background-color: red; box-sizing: border-box; padding: 20px; margin: 20px 10px; border-radius: 6px; color: #fff;">' . $row['not_name'] . '<br>'  . $row['not_content'] . '</div>';

 }

?>
  • 写回答

2条回答 默认 最新

  • drfm55597 2015-07-01 14:54
    关注

    Assuming you have an ID or date field of some sort you can use ORDER BY [yourfieldname here] DESC on that field to sort the return by the newest record first and then LIMIT 1 to grab just that record

    $q = "SELECT * 
         FROM nots
         ORDER BY ID DESC
         LIMIT 1";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?