根据条件更新表(循环时) [英] Update table based on condition (While Loop)
本文介绍了根据条件更新表(循环时)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我试图根据一个单参数来更新我的表:
So I am trying to update my table based on a singe parameter:
dateEntered字段必须为空白.
The dateEntered field must be blank.
我想随机选择50行,并将空白的ownerID字段更新为测试器"
And I want to randomly select 50 rows, and update the blank ownerID fields to "Tester"
这就是我所拥有的:
<?php
include("includes/constants.php");
include("includes/opendb.php");
$query = "SELECT * FROM contacts WHERE dateEntered='' ORDER BY RAND() LIMIT 50";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_assoc($result)){
$firstid = $row['id'];
$query2 = mysql_query("UPDATE contacts
SET ownerID = 'Tester'
WHERE id = '$firstid'");
$result2 = mysql_query($query2) or die(mysql_error());
}
?>
它将更新一条记录,然后退出并给我:
It will update a single record, then quit and give me:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1' at line 1
选择记录的第一部分工作正常,其query2不会更新所有50条记录,只是更新一条记录.也许我写错了.
The first part that selects the records works fine, its query2 that won't update all 50 records, just one. Maybe I am writing this wrong.
推荐答案
mysql_query只需要一次
mysql_query needs only one time
$query2 = mysql_query("UPDATE contacts
SET ownerID = 'Tester'
WHERE id = '$firstid'");
$result2 = mysql_query($query2) or die(mysql_error());
到
$result2 = mysql_query("UPDATE contacts
SET ownerID = 'Tester'
WHERE id = '$firstid'");
这篇关于根据条件更新表(循环时)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文