【问题标题】:PHP - nested recursive SQL query doesn't retrieve valuesPHP - 嵌套递归 SQL 查询不检索值
【发布时间】:2017-01-11 22:24:28
【问题描述】:

我要做的是打印一个表,并在每一行打印,对于数据库中的每个不同值、值、数据库中具有该值的行数以及值的总和。抱歉这个解释得不好,直接上代码比较容易

$query_voucher1="SELECT * FROM voucher WHERE consegnato= 0 AND IDprestazione=0 ORDER BY codice";
$risultato_query_voucher1=@mysql_query($query_voucher1);
echo "<table class='table table-striped' style='margin-top:70px; width: 60%;'>
      <caption>Riassunto Voucher Liberi</caption>
       <tr>
         <th>Codice di controllo</th>
         <th>Quantità (solo liberi)</th>
         <th>Data Emissione</th>
         <th>Valore Pacchetto (solo liberi)</th>
        </tr>";

$prevcodice= NULL;
$curcodice= 10;
while ($row_voucher1=mysql_fetch_row($risultato_query_voucher1)) {
                if ($curcodice!=$prevcodice){
                  $array_temp_query_value=array();
                  if ($modifica==true){
                  $temp_query_value=mysql_query("SELECT valorelordo FROM voucher WHERE codice='$row_voucher1[2]' AND consegnato=0 ");
                  }else{
                    $temp_query_value=mysql_query("SELECT valorelordo FROM voucher WHERE codice='$row_voucher1[2]' AND consegnato=0 AND IDprestazione=0");
                  }
                  while(mysql_fetch_row($temp_query_value)){
                    array_push($array_temp_query_value, $temp_query_value[0]);
                  }
                  echo "<tr>
                          <td>" . $row_voucher1[2] . "</td>
                          <td>" . count($array_temp_query_value) . "</td>
                          <td>" . print_r($array_temp_query_value). "</td>
                        </tr>";
                  $prevcodice=$row_voucher1[2];

                }
              $curcodice=$row_voucher1[2];
              }

              echo "</table>";

假设我有一个带有codice 字段的数据库,并且该字段中的许多值都重复了。我想做的是打印一次值,一次打印codice 的行数相同,一次打印codice 的值的总和相同。

这个解释也不好,所以这里是JSFiddle

$array_temp_query_value 返回正确数量的值,因此我可以计算它们以填充第二个字段,但数组中的所有值都是空的,因此我无法对它们求和。查询看起来不错,所以我真的不知道。

【问题讨论】:

  • 为什么不在查询中使用 groupBy 呢?
  • 警告:如果您只是学习 PHP,请不要使用mysql_query 接口。它是如此可怕和危险,以至于在 PHP 7 中被删除。像 PDO is not hard to learn 这样的替代品和像 PHP The Right Way 这样的指南解释了最佳实践。你的用户参数不是properly escaped,有SQL injection bugs可以被利用。
  • 警告:在使用 @ 运算符调用方法时不要抑制错误。如果出现问题,您想了解它并需要采取纠正措施,为用户显示有用的消息,记录问题,等等。如果您忽略可以提醒您注意问题的错误,也会使此类调试问题变得更加复杂。

标签: php mysql arrays loops


【解决方案1】:

你不需要自己计算这个值,只需使用count()sum()group by,例如

select codice, count(*), sum(codice)
from voucher
group by codice

这将为您提供表中的所有现有值、每个值的行数和总和。


预先,不要使用 mysql_* 函数,因为它们是 deprecated 并在 PHP 7 中被删除。

使用mysqli,取自mysqli_fetch_row 的在线手册,类似这些内容(未经测试)

$result = mysqli_query($conn, 'select codice, count(*), sum(codice) from voucher group by codice');
while ($row = mysqli_fetch_row($result)) {
    echo '<tr><td>' . $row[0] . '</td><td>' . $row[1] . '</td><td>' . $row[2] . '</td></tr>';
}

【讨论】:

  • 我无法在我的代码中应用此解决方案。你能提供一份工作代码的草稿吗?
猜你喜欢
  • 1970-01-01
  • 2012-02-16
  • 2019-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-24
  • 2020-03-09
  • 2014-02-22
相关资源
最近更新 更多