我正在尝试使用while循环从另一个表中提取的结果更新MySQL表,但它只更新了最后一个结果集。
这是代码
$qa=$db->query("SELECT *, (acq_subudget.RemBal-order_items.total_cost) AS rama FROM order_items, acq_subudget WHERE invoice_num='$ordnumber_update' AND acq_subudget.id=order_items.disc");
while($qa_data=$qa->fetch(PDO::FETCH_ASSOC))
{
set_time_limit(0);
$account_remaining=$qa_data['rama'];
$account_name=$qa_data['acq_subudget.disc'];
$account_id=$qa_data['id'];
$qa_update=$db->exec("UPDATE `acq_subudget` SET RemBal='$account_remaining' WHERE id='$account_id'");
}
我正在使用pdo
答案 0 :(得分:0)
您使用fetch而不是fetchAll,fetch只从结果集中获取下一行
答案 1 :(得分:0)
顺便说一句,您可以在单个语句中执行此操作而无需循环: -
UPDATE acq_subudget
INNER JOIN order_items
ON acq_subudget.id = order_items.disc
SET acq_subudget.RemBal = acq_subudget.RemBal-order_items.total_cost
WHERE acq_subudget.invoice_num='$ordnumber_update'