如何仅显示要从数据库显示的特定数量的数据 - PHP

时间:2017-10-11 07:54:58

标签: php mysql

有没有办法可以显示数组中特定数量的项目而不是显示数据库中的所有产品?

显示产品的主要代码段     

        //Fetch the data from the database
        $stmt = $DB_con->prepare('SELECT productID, productName, productPrice, productPic, productDescription FROM product_tbl ORDER BY productID DESC');
        $stmt->execute();

        //If the number of products is more than 0 
        if($stmt->rowCount() > 0)
        {
            //Fetch the products from the database table to a row
            while($row=$stmt->fetch(PDO::FETCH_ASSOC))
            {   
                //Extract data to a row
                extract($row);
?>
    <!--Displays the products in a row of arrays-->
    <div class="col-lg-4 col-md-6 mb-4">
        <div class="card h-100">
            <!--Product Image--> 
            <!--Product Name--> 
            <!--Product Price and Details-->
        </div>
  </div>

1 个答案:

答案 0 :(得分:2)

您使用limit and offset作为

select * from tablename order by id desc LIMIT 5 OFFSET 0

了解更多信息,您可以访问 click here

描述LIMIT仅允许在ORDER BY之后显示。