简体   繁体   English

MYSQLI-每次运行增加查询的WHERE值

[英]MYSQLI - Increment query WHERE value per run

I am looking to see if it is possible to increment $tid by one each time, 我想看看是否可以将$tid每次增加$tid
so it is $tid = 1, 2, 3, 4, 5, etc... 所以它是$tid = 1, 2, 3, 4, 5, etc...

This $tid would count the rows of the table, so $tid=1 would echo row 1 info, $tid=2 would echo row 2 info and so on. $tid将计算表的行,因此$tid=1将回显第1行的信息, $tid=2将回显第2行的信息,依此类推。 While I may feel this isn't the best solution, if anyone can suggest a more preferred alternative to how this can be done, that would be great. 虽然我可能觉得这不是最好的解决方案,但是如果有人可以提出一种更好的替代方法,那就太好了。

TL:DR : I would simply like to find a method where I can cycle through all the rows where s_id = $tid using the code example below 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>';

};

In your code, you are doing a MySQL query outside the loop. 在您的代码中,您正在循环外执行MySQL查询。 It will be executed once and then you loop over the result. 它将执行一次,然后循环遍历结果。 If you want to change your query (which I assume as $tid is a parameter of your query) and want to get the output for the changed query, you have to execute the query a second time. 如果要更改查询(我假设$tid是查询的参数),并且想要获取更改后的查询的输出,则必须再次执行查询。 If you know all possible values of $tid , it could work the following way: 如果您知道$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>';
}

But this is not good, as you have to execute the query multiple times. 但这不好,因为您必须多次执行查询。 So just use IN : 因此,只需使用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>';
}

use this function to get num of row by this 使用此功能来获取行数

 $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);

now count has count of result 现在计数有结果计数

to find get count value from query: 从查询中找到获取计数值:

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

$count = $row[0];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM