简体   繁体   中英

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.

Right now it stores the same $random and $pid number for every value of $db_row.

Thanks, Cristian.

As you're updating all items using the in clause, the last one will update them all with your last random number,

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

in your update clause will only update a single row at a time

(obviously you have add id to your SELECT statement as well)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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