简体   繁体   English

PHP循环并写入MySql数据库

[英]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. 我只希望它更新与id字段相对应的字段。

LE. 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. 现在,脚本在状态和速度字段中为ID 1和2写入相同的值。我希望它只为每个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. 错误在于$ 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. 因此,这意味着必须存在一些使两个ID的状态保持不变的东西。 Trace the status variable to where the value originates and see if you can figure out why they're the same value. 将状态变量跟踪到该值的起源,并查看是否可以弄清楚它们为什么是相同的值。

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

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