简体   繁体   中英

Php loop and writing to MySql db

I'm having problems with the following script. It updates every selected field where the "state" field is ACTIVE with the values from the last pass. I only want it to update the field corresponding to the id field.

LE. I have multiple records, but only certain of them are ACTIVE.

ID 1 state ACTIVE
ID 2 state ACTIVE
ID 3 state DONE

right now the script writes in the status and speed field the same value for ID 1 and 2. I want it to write only the corresponding values for each ID.

Hope this clears it.

$result = mysql_query("SELECT id, filename, status, totalsize, procent, pid, log_no FROM plow WHERE state = 'Active'");
while (($db_field = mysql_fetch_array($result)) != false) {
        $cfs = filesize($init_loc."/".$db_field['filename']);
        $procentage= ($cfs * '100')/$db_field['totalsize'];

        $out1="2";
        $pid2=$out1 + $db_field['pid'];
        $command = exec("ps ax | grep -v grep | grep -c ".$pid2, $out); 
        exec($command, $out);

        if ($out[0] == 1 && $procentage <= 99 ) {

            $fp = fopen($init_loc."/Logs/log".$db_field['log_no'], 'r');
            $cursor = -1;

            fseek($fp, $cursor, SEEK_END);
            $char = fgetc($fp);

            while ($char === "\n" || $char === "\r") {
                fseek($fp, $cursor--, SEEK_END);
                $char = fgetc($fp);
            }

            while ($char !== false && $char !== "\n" && $char !== "\r") {
                $line = $char . $line;
                fseek($fp, $cursor--, SEEK_END);
                $char = fgetc($fp);
            }

            $av_speed=ereg_replace("[^0-9]", "", substr($line,-6));
            mysql_query("UPDATE plow SET currentsize = '$cfs', procent = '$procentage', av_speed = '$av_speed' WHERE id = '".$db_field['id']."'") or die ('Error: ' . mysql_error());

            $needle1='Waiting';
            $needle2='failed';
            $needle3='no module';
            $needle4='retry after a safety wait';
            $search1=strpos($line, $needle1);
            $search2=strpos($line, $needle2);
            $search3=strpos($line, $needle3);
            $search4=strpos($line, $needle4);

                if($search1 !== false) 
                {
                    $status=ereg_replace("[^0-9]", "", $line);
                    mysql_query("UPDATE plow SET status = '$status' WHERE id = '".$db_field['id']."'") or die ('Error: ' . mysql_error());
                }

                elseif ($search2 !== false) 
                {
                    $status="2";
                    mysql_query("UPDATE plow SET status = '$status' WHERE id = '".$db_field['id']."'") or die ('Error: ' . mysql_error());
                }

                elseif ($search3 !== false) 
                {
                    $status="2";
                    mysql_query("UPDATE plow SET status = '$status' WHERE id = '".$db_field['id']."'") or die ('Error: ' . mysql_error());
                }
                elseif ($search4 !== false) 
                {
                    $status="Retrying ...";
                    mysql_query("UPDATE plow SET status = '$status' WHERE id = '".$db_field['id']."'") or die ('Error: ' . mysql_error());
                }
                else 
                {
                    $status="3";
                    mysql_query("UPDATE plow SET status = '$status' WHERE id = '".$db_field['id']."'") or die ('Error: ' . mysql_error());
                }   

            unset($search1);
            unset($search2);
            unset($search3);
            unset($search4);
            unset($av_speed);
            unset($status);
        }
        else if ($out[0] == 0 && $procentage == 100 ) {
            mysql_query("UPDATE plow SET currentsize = '$cfs', procent = '$procentage', status= '1', state = 'Done' WHERE id = '".$db_field['id']."'") or die ('Error: ' . mysql_error());
        unset($status);
        }
        else {
            $status="Unknown error";
            mysql_query("UPDATE plow SET status= '$status' WHERE id = '".$db_field['id']."'") or die ('Error: ' . mysql_error());
        }
}

mysql_close();

The error is in the value for $status. I did a simple script using your original calculations and it updates the other values as expected.

//Define the SQL
$query = "SELECT id, filename, totalsize FROM plow WHERE state = 'ACTIVE'";
$result = mysql_query($query);

if (mysql_affected_rows() > 0) {
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
        //Do some simple calculation before updating the database
        $id = $row['id']; // Set the id
        $cfs = filesize($init_loc."/".$row['filename']); //Get file size
        $procentage= ($cfs * 100)/$row['totalsize']; // Percentage
        echo 'Filesize: ' . $cfs . '<br />' . 'Percentage: '. $procentage . '<br />';
        $update = "UPDATE plow SET procent = '$procentage' WHERE id = '$id'";
        $updateResult = mysql_query($update);
    }
} //End check for affected rows

So it means there must be something that makes the status remain the same for both ids. Trace the status variable to where the value originates and see if you can figure out why they're the same value.

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