简体   繁体   English

PHP问题与循环并写入MySql Db

[英]Php Problem with a loop and writing to MySql Db

Maybe someone can help with this because I am going crazy: 也许有人可以帮忙,因为我快疯了:

$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());
        }
    }

Basically, when the start command is given, I want to be able to execute for every value from $db_row the action there, generate a random number for the log and store each on of them in their respective place in the MySql DB. 基本上,当发出启动命令时,我希望能够对$ db_row中的每个值执行操作,为日志生成一个随机数,并将它们分别存储在MySql DB中。

Right now it stores the same $random and $pid number for every value of $db_row. 现在,它为$ db_row的每个值存储相同的$ random和$ pid号。

Thanks, Cristian. 谢谢,克里斯蒂安。

As you're updating all items using the in clause, the last one will update them all with your last random number, 当您使用in子句更新所有项时,最后一项将使用您的最后一个随机数更新所有项,

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

in your update clause will only update a single row at a time 在您的update子句中一次只会更新一行

(obviously you have add id to your SELECT statement as well) (显然,您也已将ID添加到SELECT语句中)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM