MySQL输出CSV并从CSV更新

时间:2012-01-12 23:18:57

标签: php mysql csv

我在下面有一个查询,我从数据库中选择一个ID,然后用1更新该ID的字段,表示该记录已被处理。我现在需要执行相同的过程但是选择50个ID并以CSV格式输出它们,并再次用1更新每个记录以指示这些记录已被处理。任何帮助表示赞赏,我不确定最有效的方法。

$result = mysql_query("SELECT `id` FROM `t_ids` WHERE `f_fetched` IS null LIMIT 1");
while($row = mysql_fetch_array($result))
$f_id = $row['id'];
mysql_query("UPDATE t_ids SET f_fetched = '1' WHERE id = '$f_id'");

2 个答案:

答案 0 :(得分:0)

为什么不直接使用:

mysql_query("UPDATE t_ids SET f_fetched = '1' WHERE  `f_fetched` IS null");

答案 1 :(得分:0)

你可以使用这样的东西

$result = mysql_query("SELECT `id` FROM `t_ids` WHERE `f_fetched` IS null LIMIT 50");
$processed_ids = array();
while($row = mysql_fetch_array($result))
{
    //do whatever processing you need to with that id

    //add the id to the $processed_ids array
    $processed_ids[] = $row['id'];
}

$ids = implode(",", $processed_ids);  //create a comma-delimited string of ids.

//update all rows in 1 query
mysql_query("UPDATE t_ids SET f_fetched = '1' WHERE id IN ($ids)");