显示来自查询的 5 行数据 [英] Displaying 5 rows of data from query

查看:38
本文介绍了显示来自查询的 5 行数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含客户订单的简单表 (mgap_orders).多个具有相同的 id (mgap_ska_id),我只想从表中提取 5 条记录并显示所有 5 条记录.

I have a simple table (mgap_orders) with customer orders in it. Multiple have the the same id (mgap_ska_id) and I simply want to pull 5 records from the table and display all five.

我可以使用以下查询和 PDO 轻松获取一条记录,但如何显示 5 行而不是仅一行?

I can easily get one record with the following query and PDO, but how can I display 5 rows instead of only one row?

  $result_cat_item = "SELECT * FROM mgap_orders WHERE mgap_ska_id = '$id' GROUP BY mgap_ska_id";

    while($row_cat_sub = $stmt_cat_item->fetch(PDO::FETCH_ASSOC))
    {
    $item=$row_cat_sub['mgap_item_description'];
    $item_num=$row_cat_sub['mgap_item_number'];
    $item_type=$row_cat_sub['mgap_item_type'];
    $item_cat=$row_cat_sub['mgap_item_catalog_number'];
    $item_ven=$row_cat_sub['mgap_item_vendor'];
    $item_pur=$row_cat_sub['mgap_item_percent_purchased'];
    $item_sales=$row_cat_sub['mgap_item_sales'];
    }

推荐答案

使用 limit 5 然后把结果放到一个数组中,像这样:

Use limit 5 then put the results in an array, like this:

$result_cat_item = "SELECT * FROM mgap_orders WHERE mgap_ska_id = '$id' GROUP BY mgap_ska_id LIMIT 5";

$items = array();

while($row_cat_sub = $stmt_cat_item->fetch(PDO::FETCH_ASSOC))
{
    $items['item']       = $row_cat_sub['mgap_item_description'];
    $items['item_num']   = $row_cat_sub['mgap_item_number'];
    $items['item_type']  = $row_cat_sub['mgap_item_type'];
    $items['item_cat']   = $row_cat_sub['mgap_item_catalog_number'];
    $items['item_ven']   = $row_cat_sub['mgap_item_vendor'];
    $items['item_pur']   = $row_cat_sub['mgap_item_percent_purchased'];
    $items['item_sales'] = $row_cat_sub['mgap_item_sales'];
}

然后你可以这样做:

foreach($items as $item) {
    // echo $item['item'] or whatever
}

或者您可以跳过将它们放入数组中,而只需使用 while() 来执行您需要对数据执行的操作.

Or you can skip putting them in the array and just use the while() to do what you need to do with the data.

这篇关于显示来自查询的 5 行数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆