Php循环问题并写入MySql Db

时间:2011-03-13 15:11:10

标签: php mysql loops

也许有人可以帮忙解决这个问题因为我疯了:

$action = $_GET['action'];
$items = rtrim($_POST['items'],",");
$sql = mysql_query("SELECT url, pid, log_no FROM plow WHERE id IN ($items)") or die ('Error: ' . mysql_error());

    if ($action == 'start') {
        while ($db_row = mysql_fetch_assoc($sql)) { 
            //random number for the log file name
            $random = mt_rand(1,500000);
            //log file name
            $out_file = $init_loc.'/Logs/log'.$random;
            // the command
            $command = exec("($path" . " nohup plowdown --temp-directory='$init_loc' -o '$init_loc' " . "'".$db_row['url']."' 2> " . "'$out_file' > /dev/null &);" . "echo $$;", $out); 
            exec($command, $out);
            mysql_query("UPDATE plow SET status = 'Pending...', state = 'Active', pid = '".$out[0]."', log_no = '$random' WHERE id IN ($items)") or die ('Error: ' . mysql_error());
        }
    }

基本上,当给出启动命令时,我希望能够从$ db_row执行每个值的操作,为日志生成一个随机数,并将它们分别存储在MySql DB中各自的位置

现在它为$ db_row的每个值存储相同的$ random和$ pid编号。

谢谢, 克里斯蒂安。

1 个答案:

答案 0 :(得分:1)

当您使用in子句更新所有项目时,最后一项将使用您的上一个随机数更新它们,

' ... where id='.$row['id']

update子句中的

一次只会更新一行

(显然你也在你的SELECT声明中添加了id)