Php循环并写入MySql db

时间:2011-03-20 20:11:39

标签: php mysql

我遇到以下脚本问题。 它使用上一次传递的值更新“state”字段为ACTIVE的每个选定字段。我只希望它更新对应于id字段的字段。

LE。我有多条记录,但只有某些记录是ACTIVE。

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

现在,脚本在状态和速度字段中写入ID 1和2的相同值。 我希望它只为每个ID写入相应的值。

希望这可以解决它。

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

1 个答案:

答案 0 :(得分:0)

错误是$ status的值。我使用您的原始计算执行了一个简单的脚本,并按预期更新其他值。

//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

所以这意味着必须有一些东西使两个ID的状态保持不变。将状态变量跟踪到值的来源,看看是否可以找出它们为什么是相同的值。