根据PHP变量中的值的数量多次执行mysql查询

时间:2017-12-27 06:20:14

标签: php mysql

我有一个用于捕获事务的mysql表。我试图根据同一个表中的可用类别在多个php / html表中显示数据。我的表中的样本数据如下所示。

Date       Category     Particulars      type        Amount 
-----------------------------------------------------------
01.12.17   Donation     Received from X  Receipt     500.00
10.12.17   Donation     Received from Y  Receipt     200.00  
15.12.17   Medical Aid  Paid to A        Payment     100.00
20.12.17   Expense      Building Rent    Payment      50.00 

是否可以根据类别字段中可用的不同值动态地多次执行以下查询。这是在单个PHP页面中的多个表中填充查询数据。

select * from my_table where category = 'Donation';
select * from my_table where category = 'Medical Aid';
...
...

,预期结果如下......

Date       Category     Particulars      type        Amount 
-----------------------------------------------------------
01.12.17   Donation     Received from X  Receipt     500.00
10.12.17   Donation     Received from Y  Receipt     200.00
-----------------------------------------------------------

-----------------------------------------------------------
Date       Category     Particulars      type        Amount 
-----------------------------------------------------------
15.12.17   Medical Aid  Paid to A        Payment     100.00
-----------------------------------------------------------

-----------------------------------------------------------
Date       Category     Particulars      type        Amount 
-----------------------------------------------------------
20.12.17   Expense      Building Rent    Payment      50.00 

类别表中的值我的更改。我试图在一个单独的html表中显示第一个查询的输出,而在另一个html表中显示第二个查询的输出。

3 个答案:

答案 0 :(得分:2)

您通常应该尽可能少地发出查询,因为这样可以最大限度地减少PHP应用程序中的网络调用和开销。我建议只使用ORDER BY上的category子句的单个查询:

SELECT *
FROM my_table
WHERE category IN ('Donation', 'Medical Aid', ...)
ORDER BY category;

在PHP中迭代结果集时,每个类别的记录将组合在一起,您可以为每个类别创建一个新的HTML表。

$sql = "SELECT * FROM my_table ...";    // your query goes here
$result = $conn->query($sql);
$category = NULL;

if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        if ($category != $row['category']) {
            if ($category != NULL) {
                echo '</table>';
            }
            echo '<table>';
            $category = $row['category'];
        }

        echo '<tr>';
        echo '<td>' . $row['date'] . '</td>';
        echo '<td>' . $row['category'] . '</td>';
        // and other columns in the table
        echo '</tr>';
    }
    echo '</table>';    // close the final table
}

上述脚本只是为了指出正确的方向。你真正想要的HTML可能比这更复杂。

答案 1 :(得分:0)

我只是对每个catigory进行查询,然后在自己的表中输出。

$getCategories = "select category from my_table";
$catResults = $mysqli->query($catResults);
if ($catResults->num_rows > 0){
foreach ($catResults as $catRes){ 
$myQuery= "select * from my_table where category = '".$catRes."'";
$myResults = $mysqli->query($myResults);
if ($myResults->num_rows > 0){
while($row = $myResults->fetch_assoc()){  
$myResults = $row['date'];
$myResults = $row['Donation'];
$myResults = $row['particulars'];
$myResults = $row['type'];
$myResults = $row['amount'];
echo "<table style=\"width:100%\">"
echo "<tr>";
echo "<th>".$catRes."</th>";
echo "</tr>";
echo "<tr>";
echo "<td>".$row['time']."</td>";    
echo "<td>".$row['Donation']."</td>";  
echo "<td>".$row['particulars']."</td>";     
echo "<td>".$row['type']."</td>";   
echo "<td>".$row['amount']."</td>"; 
echo "</tr>"; }}}}

答案 2 :(得分:-1)

首先获取所有类别列表

select Category from my_table

将其存储在数组中。然后使用Foreach语句执行所有查询。