PHP不会显示表中的所有行

时间:2018-02-23 07:58:57

标签: php mysql

我有一个名为cart的表,我想通过PHP返回它的所有行。

所以我编码了这个:

$get_add = "SELECT * FROM cart WHERE ip_add = '1'";
$run_add = mysqli_query($con,$get_add);

$cart_items = [];
while($row_results = mysqli_fetch_array($run_add))
{
    $item = array(
        'table_id' => $row_results['table_id'],
        'pro_id' => $row_results['product_id'],
        'pro_title' => $row_results['product_title'],
        'pro_price' => number_format($row_results['product_price'], '0', '', ','),
        'pro_supplier'  => $row_results['product_supplier'],
        'pro_img' => $row_results['product_image'],
    );
    $cart_items[] = $item;

    foreach($cart_items as $cart){
        echo $cart['pro_title']
    }
 }

所以这很好但唯一的问题是它只显示一个结果。但是,有几行将ip_add设置为1

所以我请求你让我知道我的错是什么,以及显示所有表行的正确方法是什么?

提前致谢!

2 个答案:

答案 0 :(得分:1)

将内部foreach循环移到while之外,如下所示:

while($row_results = mysqli_fetch_array($run_add))
{
    $item = array(
        'table_id' => $row_results['table_id'],
        'pro_id' => $row_results['product_id'],
        'pro_title' => $row_results['product_title'],
        'pro_price' => number_format($row_results['product_price'], '0', '', ','),
        'pro_supplier'  => $row_results['product_supplier'],
        'pro_img' => $row_results['product_image'],
    );
    $cart_items[] = $item;
}

foreach($cart_items as $cart){
    echo $cart['pro_title'];
}

基本上,你正在做的事情将总是返回数组$cart_itmes的最后一项,因为它在while循环内部。因此,请将其更改为我在上述代码段中完成的方式。

答案 1 :(得分:1)

删除 Light.aggregate([ {"$match":{"STATUS":"LIGHTS OFF"}}, {"$group":{ "_id":"$SWITCHID", "TOTALLIGHTSOFFRECEVEDDATE":{"$push":"$RECEIVEDDATE"}, }} ], Light.aggregate([ {"$match":{"STATUS":"LIGHTS ON"}}, {"$group":{ "_id":"$SWITCHID", "TOTALLIGHTSONRECEVEDDATE":{"$push":"$RECEIVEDDATE"}, }} ], output is: { "_id" : "Z5-W58-58-1A/12/5", "TOTALLIGHTSOFFRECIVEDDATE" : [ "2018-02-09-06.00.04.846000", "2018-02-09-06.15.36.520000", "2018-02-09-06.44.44.950000", "2018-02-09-07.14.49.457000", "2018-02-09-07.44.54.994000", "2018-02-09-08.14.56.890000", ] } { "_id" : "Z5-W63-199A/6", "TOTALLIGHTSOFFRECIVEDDATE" : [ "2018-02-09-05.51.37.082000", "2018-02-09-06.27.34.869000", "2018-02-09-06.56.45.592000", "2018-02-09-07.26.49.401000", "2018-02-09-07.56.53.023000", "2018-02-09-08.26.57.172000", ] } { "_id" : "Z5-W58-58-1A/12/5", "TOTALLIGHTSONRECIVEDDATE" : [ "2018-02-09-00.58.57.801000", "2018-02-09-00.28.52.478000", "2018-02-09-01.29.21.811000", "2018-02-09-01.59.05.749000", "2018-02-09-02.29.10.715000", "2018-02-09-02.59.13.542000", "2018-02-09-03.29.18.677000", "2018-02-09-03.59.23.692000", ] } { "_id" : "Z5-W63-199A/6", "TOTALLIGHTSONRECIVEDDATE" : [ "2018-02-09-00.50.54.233000", "2018-02-09-00.20.48.378000", "2018-02-09-01.20.57.603000", "2018-02-09-01.51.39.090000", "2018-02-09-02.21.06.288000", "2018-02-09-02.51.11.159000", "2018-02-09-03.21.15.296000", ] } 的{​​{1}}循环,并在foreach循环后添加它。因为您要在$cart_items中附加值,所以您循环while,所以第一次获得单个值,然后是两个值,然后是三个值......另外还有{{}} 1}}在$cart_items行的末尾。

$cart_items

最后添加;将获得echo $cart['pro_title']

中的所有记录