我有一个查询所在的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();
}
}
答案 0 :(得分: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 ) {
$error = false;
} else {
$error = true;
echo mysql_error();
}
}
if(!$error) {
echo "table successful updated"
}
未经测试,但应该可以正常工作。
答案 1 :(得分:1)
$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();
}
}
答案 2 :(得分:1)
$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";
}
答案 3 :(得分:1)
更优化的方法是在循环中创建查询并触发一次而不是反复循环。
通过这种方式,您可以减轻负载并快速执行
<?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';
?>
答案 4 :(得分:-2)
将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';
}