The script below seems to stall for no reason during the loops. There are 6700 results to loop through, but it stalls between 375-460 results processed. No errors given or logged.
<?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>";
?>
My primary concern is to get this script to run through the entire list. When testing with 50-100 results, works without any problem. As a secondary question, within the loop I have an echo with a counter. I would like to get that to display on screen in real time, instead of all at once when script finishes.