从数据库中检索数据,保存在数组中,并在php的where子句中使用该数组

时间:2018-12-01 07:18:11

标签: php sql

我将产品表中的product_name存储在$product_array中,并在以后的SQL查询的where子句中使用该数组来从购买表中检索与该数组值匹配的数据。但是我只得到与第一个product_name相关的结果,而不是全部。请帮忙

<table class="table table-borderless table-data3">
                                    <h3>Stock Table</h3>
                                    <br>
                                    <thead>
                                        <tr>
                                            <th>SL NO</th>
                                            <th>Product Name</th>
                                            <th>Total Ream</th>
                                            <th>Toatl MT</th>
                                            <!-- <th colspan="2" style="text-align: center;">Action</th> -->                               
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php
                                        $product_array=array();
                                        $query="select * from product";
                                        $query_run=mysqli_query($connect , $query);
                                        while($row=mysqli_fetch_array($query_run)){
                                            $product_array[]= $row['product_name'];
                                            //retrieving data matching the array ($product_array) in purchase table
                                            $query="select * from purchase where product_name in ('".implode("','",$product_array)."')";
                                            $counter=0;
                                            $query_run2=mysqli_query($connect , $query);
                                            while($row2=mysqli_fetch_array($query_run2)){
                                                $counter++;
                                                ?>
                                                <tr>
                                                    <td><?php echo $counter; ?></td>
                                                    <td><?php echo $row2['product_name']; ?></td>
                                                    <td><?php echo $row2['ream']; ?></td>
                                                    <td><?php echo $row2['mt']; ?></td>                                         
                                                </tr>
                                                <?php
                                            }
                                        }
                                        ?>
                                    </tbody>
                                </table>

1 个答案:

答案 0 :(得分:0)

您必须使用group by product_name来对所有具有相同名称的产品数量求和。

        <table class="table table-borderless table-data3">
                                    <h3>Stock Table</h3>
                                    <br>
                                    <thead>
                                        <tr>
                                            <th>Product Name</th>
                                            <th>Total MT</th>
                                            <th>Toatl Ream</th>
                                            <!-- <th colspan="2" style="text-align: center;">Action</th> -->                               
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php

                                        $query2="select product_name,sum(mt), 
                                      sum(ream) from purchase group by product_name";
                                        $query2_run=mysqli_query($connect, $query2);
                                        while($row2=mysqli_fetch_array($query2_run)){
                                        ?>

                                                <tr>
                                                <td><?php echo $row2['product_name'] ?></td>
                                                    <td><?php echo $row2['sum(mt)']; ?></td>
                                                    <td><?php echo $row2['sum(ream)']; ?></td>

                                                </tr>
                                            <?php } ?>
                                        </tr>
                                    </tbody>
                                </table>