Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php循环并写入MySql数据库_Php_Mysql - Fatal编程技术网

Php循环并写入MySql数据库

Php循环并写入MySql数据库,php,mysql,Php,Mysql,我对下面的脚本有问题。 它使用上次传递的值更新状态字段处于活动状态的每个选定字段。我只希望它更新id字段对应的字段 乐。我有多个记录,但只有某些记录处于活动状态 ID 1 state ACTIVE ID 2 state ACTIVE ID 3 state DONE 现在,脚本在状态和速度字段中为ID 1和2写入相同的值。 我希望它只为每个ID写入相应的值 希望这件事能澄清 $result = mysql_query("SELECT id, filename, status, totalsize

我对下面的脚本有问题。 它使用上次传递的值更新状态字段处于活动状态的每个选定字段。我只希望它更新id字段对应的字段

乐。我有多个记录,但只有某些记录处于活动状态

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

错误在$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的状态保持相同。将状态变量跟踪到值的起始位置,并查看是否可以找出它们为何是相同的值。

$procentage=$cfs*'100'/$db_field['totalsize'];与字符串相乘?真的吗?那部分很有效,我不知道我做错了什么!!??错的是字符串不是数字。PHP通常会为您转换内容,但您不应该滥用它-特别是在这种情况下,“100”不应该在其周围加引号,因为它永远不会用作字符串。明白了。谢谢你澄清这一点。知道其余的有什么问题吗?你到底想做什么?编辑问题并添加更多文本,解释您真正想要做的事情。好的,那么您将如何做?告诉我更多关于您想要做的事情,我将相应地更新答案。在更新表格之前,您是否有理由将活动记录带出?对于每个活动记录,我都会进行这些计算,速度在那之后,我想把它写回数据库。但是你想对一个同样活跃的特定ID这样做吗?