通过desc获取最近5位用户数据

时间:2015-01-15 23:11:08

标签: php mysql

<? 
require('../../members/inc/config.php');
require('../../lib/framework.php');

 $sql = "SELECT * FROM members ";

 $query = $db->prepare( $sql );
 $query->execute();
 $results = $query->fetchAll( PDO::FETCH_ASSOC );
?>

 <table class="table">
   <tr>
     <th>ID</th>
     <th>Username</th>
     <th>Email</th>

   </tr>
   <?php foreach( $results as $row ){
   echo "<tr><td>";
     echo $row['memberID'];
     echo "</td><td>";
     echo $row['username'];
     echo "</td><td>";
     echo $row['email'];
     echo "</td>";
   echo "</tr>";
   }
 ?>
 </table>

我们,我想获得我桌子的最新5位用户。我已经尝试过这样做:

$sql = "SELECT * FROM members ORDER BY username DESC LIMIT5";

但是..不检索任何结果..

2 个答案:

答案 0 :(得分:2)

查询应为

$sql = "SELECT * FROM members ORDER BY username DESC LIMIT 5";

LIMIT和5之间需要空格

答案 1 :(得分:0)

如果你想要最后5个订单应该是id / creation_time

$sql = "SELECT * FROM members ORDER BY id DESC LIMIT 5";
$sql = "SELECT * FROM members ORDER BY creation_time DESC LIMIT 5";