如何使用mysql限制数据库中的所有列

时间:2017-09-28 17:12:00

标签: php mysql

我正在开发图片库系统,我想知道如何限制/显示上传到图库/数据库表中的所有图片。我通常不知道如何做到这一点所以我通常使用一个大于数据库中列数的数字来显示我的所有列。我很想知道如何改变我的方法来改变我的方法。

以下是我的代码;

<?php
  $image_id = $the_image = $image_des = $time_uploaded = "";
  $gallery = mysqli_query($connect, "SELECT * FROM gallery ORDER BY time_uploaded DESC LIMIT 9000");
  while ($pick = mysqli_fetch_array($gallery, MYSQLI_BOTH)) {
    $image_id = $pick['id'];
    $the_image = $pick['image'];
    $image_des = $pick['description'];
    $time_uploaded = $pick['time_uploaded'];
    echo '<div class="nicdark_margin100">';
      echo '<a href="../img/gallery/'.$the_image.'" title="'.$image_des.'"><img alt="" class="nicdark_radius nicdark_opacity" src="../img/gallery/'.$the_image.'" width="100" height="100" /></a>&nbsp;&nbsp;&nbsp;&nbsp;';
    echo ' </div>';                    
  }
?>

1 个答案:

答案 0 :(得分:1)

MySQL在查询中不需要LIMIT字段。所以要获得所有结果

SELECT * FROM gallery ORDER BY time_uploaded DESC

但是,如果您尝试进行分页,则可以组合使用LIMIT和OFFSET

## First Page of 25 images
SELECT * FROM gallery ORDER BY time_uploaded DESC LIMIT 25 OFFSET 0
## Second Page of 25 images (offset by 25)
SELECT * FROM gallery ORDER BY time_uploaded DESC LIMIT 25 OFFSET 25

有关详细信息,请参阅

https://www.w3schools.com/php/php_mysql_select_limit.asp