MYSQLI - 增加查询 WHERE 值每 运行

MYSQLI - Increment query WHERE value per run

我想看看是否可以增加 $tid每次减一,
所以是$tid = 1, 2, 3, 4, 5, etc...

$tid 将计算 table 的行数,因此 $tid=1 将回显第 1 行信息,$tid=2 将回显第 2 行信息,依此类推。虽然我可能觉得这不是最好的解决方案,但如果有人能提出一个更可取的替代方案来完成这项工作,那就太好了。

TL:DR:我只想找到一种方法,我可以使用下面的代码示例

循环遍历 s_id = $tid 的所有行]
$tid = 1;

$query2 = "SELECT * FROM `SALE` 
 LEFT JOIN `SALE_ROW` 
   ON `SALE_ROW`.s_id=`SALE`.s_id 
 WHERE `SALE_ROW`.s_id = {$tid}";
$results2 = mysqli_query($dbconnection, $query2);
while ($row = mysqli_fetch_array($results2)) {

    echo '<div class="pphhold">';

    echo '<div class="pcdtstlye">Previous Details</div>';

    echo  '<div> s_id: ' . $row['s_id'] .'</div>';
    echo  '<div> p_id: ' . $row['p_id'] .'</div>';
    echo  '<div> sr_qty: ' . $row['sr_qty'] .'</div>';
    echo  '<div> s_total: ' . $row['s_total'] . '</div>';
    echo  '<div> s_date: ' . $row['s_date'] . '</div>';

    echo '</div>';

};

使用此函数通过此

获取行数
 $query2 = "SELECT * FROM `SALE` LEFT JOIN `SALE_ROW` ON `SALE_ROW`.s_id=`SALE`.s_id WHERE `SALE_ROW`.s_id = {$tid}";

$results2 = mysqli_query($dbconnection, $query2);
$count=mysqli_num_rows($results2);

现在计数有结果计数

从查询中查找获取计数值:

$result = mysqli_query($dbconnection,"select count(1) FROM table");
$row = mysqli_fetch_array($result);

$count = $row[0];

在您的代码中,您正在循环外执行 MySQL 查询。它将执行一次,然后循环遍历结果。如果你想改变你的查询(我假设 $tid 是你的查询的一个参数)并且想要得到改变后的查询的输出,你必须再次执行查询。如果您知道 $tid 的所有可能值,它可以按以下方式工作:

$tids = [1, 2, 3];
foreach($tids as $tid) {
  $query2 = "SELECT * FROM `SALE` LEFT JOIN `SALE_ROW` ON `SALE_ROW`.s_id=`SALE`.s_id WHERE `SALE_ROW`.s_id = {$tid}";
  $results2 = mysqli_query($dbconnection, $query2);
  $row = mysqli_fetch_array($results2);

  echo '<div class="pphhold">';

  echo '<div class="pcdtstlye">Previous Details</div>';

  echo  '<div> s_id: ' . $row['s_id'] .'</div>';
  echo  '<div> p_id: ' . $row['p_id'] .'</div>';
  echo  '<div> sr_qty: ' . $row['sr_qty'] .'</div>';
  echo  '<div> s_total: ' . $row['s_total'] . '</div>';
  echo  '<div> s_date: ' . $row['s_date'] . '</div>';

  echo '</div>';
}

但这并不好,因为您必须多次执行查询。所以只需使用 IN:

$tids = [1, 2, 3];
$query2 = "SELECT * FROM `SALE` LEFT JOIN `SALE_ROW` ON `SALE_ROW`.s_id=`SALE`.s_id WHERE `SALE_ROW`.s_id IN(" . implode(',', $tids) . ")";
$results2 = mysqli_query($dbconnection, $query2);
while($row = mysqli_fetch_array($results2)) {
  echo '<div class="pphhold">';

  echo '<div class="pcdtstlye">Previous Details</div>';

  echo  '<div> s_id: ' . $row['s_id'] .'</div>';
  echo  '<div> p_id: ' . $row['p_id'] .'</div>';
  echo  '<div> sr_qty: ' . $row['sr_qty'] .'</div>';
  echo  '<div> s_total: ' . $row['s_total'] . '</div>';
  echo  '<div> s_date: ' . $row['s_date'] . '</div>';

  echo '</div>';
}