0

我正在运行一个php脚本,它有一个while循环和文件写入,这使得它需要一些时间。问题出现在它似乎没有在运行时打印错误时它在完全执行脚本后打印它们,有时会出错处于循环中,因此每次都循环重复,这花费了我很多时间。我正在使用 Wamp Server 2.2 版。
这是样本code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Algorithm" content="text/html; charset=utf-8" />
<title>Delta Compression</title>
</head>
<body>

<?php
//DB Connection
$dbuser = "root";
$dbpass = "";
$dbname = "delta_compression";
$dbhost = "localhost";

//Database Connection
$con = mysql_connect($dbhost,$dbuser,$dbpass);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db($dbname, $con);
echo "Database Connected !";

//Variable declaration 
$file="flag_track.txt";
$result=mysql_query("select * from d_5");
$total_rows=mysql_num_rows($result);
$limit=256;

while($i < $total_rows){
    //increasing time limit 
    set_time_limit(0);
    //getting the value from the row of reading
    $reading=mysql_result($result,$i,'reading');
    //if first flag is set 
        if(!(firstFlag())) {
            delta();
            }
    //convert the decimal to binary and store in db 
    if(saveDelta(decbin($diffrence))== flase){
        echo"Error saving the data in DB";

        }   

}
function delta(){

        $delta=$flag-$reading;
        file_put_contents ($file, $flag."<br>".$delta, FILE_APPEND)."<br>";             
        if( $delta > $limit){
                $flag=$reading;
                file_put_contents ($file, $flag."<br/>".$delta,     FILE_APPEND)."<br>";

                }
}

function firstFlag(){
if($flag == null){
        //set the flag
        $flag=$reading;
        //saving the flag 
        file_put_contents ($file, $flag, FILE_APPEND)."<br>";
        return true;
        }
        else {

return false;
}
}


function saveDelta($dif) {

if(mysql_query("INSERT INTO e_x_1 (delta_of_reading) VALUES ('b" . $dif . "')")){
return true; //on success
} else {
return false; //on failure
}

}
?>

</body>
</html>
4

1 回答 1

0

错误在此语句中

mysql_query("INSERT INTO e_x_1 (delta_of_reading) VALUES (b'$dif')");

它应该是这样的:

mysql_query("INSERT INTO e_x_1 (delta_of_reading) VALUES ('b" . $dif . "')"); 
//i don't know if you want `b` there or it is an error

并且由于该函数saveDelta($dif)是在 while 循环中调用的,因此它将多次打印错误。您可以返回错误代码并检查查询是否成功执行。

function saveDelta($dif) {

  if(   mysql_query("INSERT INTO e_x_1 (delta_of_reading) VALUES ('b" . $dif . "')") ){
    return true; //on success
  } else {
    return false; //on failure
  }

}

在你的while循环中检查为:

if( saveDelta(decbin($diffrence) === false ){
  //print error and return or do whatever you like to do
} 
于 2013-07-31T05:16:11.353 回答