Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/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脚本在循环期间暂停_Php_Loops - Fatal编程技术网

PHP脚本在循环期间暂停

PHP脚本在循环期间暂停,php,loops,Php,Loops,下面的脚本在循环过程中似乎无缘无故地暂停。有6700个结果需要循环,但它会在375-460个处理结果之间暂停。没有给出或记录错误 <?php /* Example usage of the Amazon Product Advertising API */ include("amazon_api_class.php"); ignore_user_abort(true); set_time_limit(0); ini_set('memory_limit','256M'); in

下面的脚本在循环过程中似乎无缘无故地暂停。有6700个结果需要循环,但它会在375-460个处理结果之间暂停。没有给出或记录错误

    <?php

/* Example usage of the Amazon Product Advertising API */
include("amazon_api_class.php");


ignore_user_abort(true);
set_time_limit(0);
ini_set('memory_limit','256M');
ini_set("display_errors", 1);
ini_set("track_errors", 1);
ini_set("html_errors", 1);
error_reporting(E_ALL);



// define variables
$username = "xxxxxxx";
$password = "xxxxxxx";
$hostname = "localhost"; 

//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password) 
    or die("Unable to connect to MySQL");
    //echo "Connected to MySQL<br>";

//select a database to work with
mysql_select_db("xxxxxxxx",$dbhandle) 
    or die("Could not select database");

//execute the SQL query and return records
$result = mysql_query("SELECT * FROM input ORDER BY mpn");
if (!$result) { // add this check.
die('Invalid query: ' . mysql_error());
}
//clear results table before processing
mysql_query("TRUNCATE TABLE results");

mysql_close($dbhandle);


//loop
while($row = mysql_fetch_array($result)) {

$mfg = $row['mfg'];
$mpn = $row['mpn'];

$keyword = $mfg." ".$mpn;


    $obj = new AmazonProductAPI();

try{


    $data = $obj->getItemByKeyword($keyword);




$asin = $data->Items->Item->ASIN;
$weight = $data->Items->Item->ItemAttributes->PackageDimensions->Weight;
$height = $data->Items->Item->ItemAttributes->PackageDimensions->Height;
$length = $data->Items->Item->ItemAttributes->PackageDimensions->Length;
$width = $data->Items->Item->ItemAttributes->PackageDimensions->Width;
$manufacturer = $data->Items->Item->ItemAttributes->Manufacturer;
$brand = $data->Items->Item->ItemAttributes->Brand;
$partnumber = $data->Items->Item->ItemAttributes->MPN;
$title = $data->Items->Item->ItemAttributes->Title;
$upc = $data->Items->Item->ItemAttributes->UPC;
$ean = $data->Items->Item->ItemAttributes->EAN;
$pricenew = $data->Items->Item->OfferSummary->LowestNewPrice->FormattedPrice;
$priceused = $data->Items->Item->OfferSummary->LowestUsedPrice->FormattedPrice;
$description = $data->Items->Item->ItemAttributes->Feature;
$title = $data->Items->Item->ItemAttributes->Title;

    echo '  processing  '.$counter++;



//connection to the database
$dbhandle1 = mysql_connect($hostname, $username, $password) 
    or die("Unable to connect to MySQL");


//select a database to work with
mysql_select_db("xxxxxxxx",$dbhandle1)
    or die("Could not select db");


//execute the SQL query and return records
mysql_query('INSERT INTO `results`(`mpn`, `mfg`, `brand`, `asin`, `ean`, `upc`, `description`, `low_price_new`, `low_price_used`, `length`, `width`, `height`, `net_weight`, `gross_weight`, `shipped_weight`, `title`) VALUES ("'.$partnumber.'","'.$manufacturer.'","'.$brand.'","'.$asin.'","'.$ean.'","'.$upc.'","'.$description.'","'.$pricenew.'","'.$priceused.'","'.$length.'","'.$width.'","'.$height.'","'.$weight.'","'.$weight.'","'.$weight.'","'.$title.'")');
mysql_error();


 mysql_close($dbhandle1);

 //unset variables
 unset($asin);
 unset($weight);
 unset($height);
 unset($length);
 unset($width);
 unset($manufacturer);
 unset($brand);
 unset($partnumber);
 unset($packagequantity);
 unset($title);
 unset($upc);
 unset($data);
 unset($keyword);

 //flush
 flush();


 //force garbage collection
 gc_enable();
 gc_collect_cycles();






usleep(50000);
}
 catch(Exception $e)

{
//echo $e->getMessage();  
}
    //print_r($result);
}
echo "<br><h3>Job Completed</h3><br><br><h1><a href='csv_amz.php'>Download  Results As CSV File</a></h1><br><br><h1><a href='index.php'>Upload New File</a></h1>";
?>

过去(回到今天)PHP有一个执行时间设置,任何经过该设置的脚本都会立即被删除。我不确定情况是否仍然如此,但是否值得一查?另外:mysql方法应该被mysqli方法所取代-你会得到很多这样的回应。而且用//flushI注释
flush()
,我猜可能有一个产品名称(或类似名称)包含一个来自亚马逊产品API的撇号,这打破了你(未初始化的)SQL查询。。。但是这里还有其他问题(包括前面提到的不推荐使用的mysql_uu扩展)——我认为您需要找到一个PHP教程,它比拿破仑战争时期编写的要晚。哦,计数器不能通过原始PHP“实时”更新-服务器端脚本在浏览器读取之前完成。根据服务器的设置,我在组合
flush()
ob\u flush()时取得了不同的成功
获取实时进度消息。@LokiSinclair已经提到了超时时间-如果您非常一致地接近30秒或60秒计时器,则这可能是问题的根源。最近,我有一个长时间运行的进程,所以我跟踪计时器,当我大约10秒后,我会重定向回同一页,并用一个参数告诉我从哪里获取进程。YMMV。