在循环时控制 PHP 中的输出


Control Output Within PHP While Loop

我有一个查询所在的while循环,MySQL查询工作正常。但是,当我尝试输出我的文本说查询成功时,我得到:

"表成功更新表成功

更新表成功更新"

我知道我正在发生这种情况,因为我正在编辑 3 个对象,所以我想知道有没有办法输出字符串而不让它重复我更改多少对象?

这是循环:

while ($r3 = mysql_fetch_assoc($result3)) 
{
    $part_num = $r3['part_num'];
    $desc = $r3['desc'];
    $bom_qty = $r3['qty'];
    $need_qty = $qty * $bom_qty;
    //insert the part_num into the temp parts table and the qty
    $resut4 = mysql_query("INSERT INTO `temp_grabtec_parts`(`temp_id`,`part_num`,`desc`,`qty`) VALUES ('$temp_id','$part_num','$desc','$need_qty')");
    if ($resut4) {
        echo 'Table Succesfully Updated';
    } else {
        echo mysql_error();
    }
}
while ($r3 = mysql_fetch_assoc($result3)) {
$part_num = $r3['part_num']; $desc = $r3['desc']; $bom_qty = $r3['qty']; $need_qty = $qty*$bom_qty;
   //insert the part_num into the temp parts table and the qty
$resut4 = mysql_query("INSERT INTO `temp_grabtec_parts`(`temp_id`,`part_num`,`desc`,`qty`) VALUES ('$temp_id','$part_num','$desc','$need_qty')");
    if ( $resut4 ) {
        $error = false;
    } else {
            $error = true;
            echo mysql_error();
    }
}
if(!$error) {
   echo "table successful updated"
}

未经测试,但应该可以正常工作。

    $bool =false;
    while ($r3 = mysql_fetch_assoc($result3)) {
        $part_num = $r3['part_num']; $desc = $r3['desc']; $bom_qty = $r3['qty']; $need_qty = $qty*$bom_qty;
           //insert the part_num into the temp parts table and the qty
        $resut4 = mysql_query("INSERT INTO `temp_grabtec_parts`(`temp_id`,`part_num`,`desc`,`qty`) VALUES ('$temp_id','$part_num','$desc','$need_qty')");
            if ( $resut4 && $bool==false) {
                echo 'Table Succesfully Updated';
              $bool=true;
            } else if(!$result) {
                    echo mysql_error();
            }
    }
$check=true;
while ($r3 = mysql_fetch_assoc($result3)) {
$part_num = $r3['part_num']; $desc = $r3['desc']; $bom_qty = $r3['qty']; $need_qty = $qty*$bom_qty;   
$resut4 = mysql_query("INSERT INTO `temp_grabtec_parts`(`temp_id`,`part_num`,`desc`,`qty`) VALUES ('$temp_id','$part_num','$desc','$need_qty')");
    if (!$resut4 ) {
      $check=false;
      echo mysql_error();
    }
}
if($check)
{
echo "table successful updated";
}

更优化的方法是在循环中创建查询并触发它一次,而不是一次又一次地在循环内触发。

这样您可以减少负载并且执行速度很快

<?php
$update_count = 0;
$query_part = "INSERT INTO `temp_grabtec_parts`(`temp_id`,`part_num`,`desc`,`qty`) VALUES ";
while ($r3 = mysql_fetch_assoc($result3)) 
{
    $part_num = $r3['part_num'];
    $desc = $r3['desc'];
    $bom_qty = $r3['qty'];
    $need_qty = $qty * $bom_qty;
    //create the query part here in the loop 
    $query_part .= "('$temp_id','$part_num','$desc','$need_qty'),";
}
$query_final = substr($query_part, 0,-1);//remove the extra comma at the end
$resut4 = mysql_query($query_final);
if ($resut4) 
{
    echo 'Table(s) Succesfully Updated';
} 
else
{
    echo mysql_error();
}
?>

以下是您的方法的答案

您可以使用以下代码在最后打印一次消息,并显示确切的计数

<?php
$update_count = 0;
while ($r3 = mysql_fetch_assoc($result3)) 
{
    $part_num = $r3['part_num'];
    $desc = $r3['desc'];
    $bom_qty = $r3['qty'];
    $need_qty = $qty * $bom_qty;
    //insert the part_num into the temp parts table and the qty
    $resut4 = mysql_query("INSERT INTO `temp_grabtec_parts`(`temp_id`,`part_num`,`desc`,`qty`) VALUES ('$temp_id','$part_num','$desc','$need_qty')");
    if ($resut4) 
    {
        $update_count++;
    } 
    else
    {
        echo mysql_error();
    }
}
echo $update_count.' Table(s) Succesfully Updated';
?>

将 if-else 部分放在 while 循环之外。

  while ($r3 = mysql_fetch_assoc($result3)) {
      $part_num = $r3['part_num']; $desc = $r3['desc']; $bom_qty = $r3['qty']; $need_qty = $qty*$bom_qty;
      //insert the part_num into the temp parts table and the qty
      $resut4 = mysql_query("INSERT INTO `temp_grabtec_parts`(`temp_id`,`part_num`,`desc`,`qty`) VALUES ('$temp_id','$part_num','$desc','$need_qty')") or die(mysql_error());
       }
      if ( $resut4 ) {
      echo 'Table Succesfully Updated';
      } else {
      echo 'Table Update Fail';
      }