从数据库中获取数据并使用 API 的新数据更新它


fetch data from database and update its with API's new data

我们想从API数据更新数据库,我们有近100000条记录,所有记录每天都在更新,我需要使用API更新新数据。

脚本工作详细信息我们正在从商店获取$productId,从这个$productId我们将从 API 脚本获取更新的数据,在获得更新的数据后,我们会将更新的数据更新到存储表中。

是否有可能在更新一个数据后自动单击一个按钮并为下一个$productId进行更新?

<?php
// Mysql Connection //
$table_name= "store";
$result = $mysqli->query( "SELECT productId FROM $table_name" );
while ( $rows =  $result->fetch_assoc() ) {
$pid = $rows['productId']; // Product code here
}
// API Script Here For - Getting Data from Api by fetching $pid code every, API Script is revert only single $pid details //
include "../extra/clusterdev.flipkart-api.php";
$flipkart = new 'clusterdev'Flipkart("xxxxxx", "xxxxxxxxxxxxxxxx", "json");
$url = 'https://affiliate-api.flipkart.net/affiliate/product/json?id=' .$pid;
$details = $flipkart->call_url($url);
if(!$details){
    echo 'Error: Could not retrieve products list.';
    exit();
}
$details = json_decode($details, TRUE);
$mrp = $details['productBaseInfo']['productAttributes']['maximumRetailPrice']['amount'];
$newPrice = $details['productBaseInfo']['productAttributes']['sellingPrice']['amount'];
$newInStock = (int) $details['productBaseInfo']['productAttributes']['inStock'];
$discountPercentage = $details['productBaseInfo']['productAttributes']['discountPercentage'];
if ($newInStock == 1)
{ $newInStock= 'true'; } else { $newInStock = 'false'; }
echo $newInStock;
echo '<br />';
echo $newPrice;
echo '<br />';
// Mysql Connection //
$result = $mysqli->query( "SELECT price,inStock FROM $table_name WHERE productId = '$pid' ") ;
while ( $rows =  $result->fetch_assoc() ) {
$price = $rows['price'];
$inStock = $rows['inStock'];
}
if ($newPrice != $price || $newInStock != $inStock)
  {
$results = $mysqli->query("UPDATE $table_name SET price='$newPrice', inStock='$newInStock' WHERE productId='$pid' ");
if($results){
    print 'Success! record updated'; 
}else{
    print 'Error : ('. $mysqli->errno .') '. $mysqli->error;
}
  }
// close connection
$mysqli->close();
?>

您可以执行以下操作:

$result = $mysqli->query( "SELECT productId,price,inStock FROM $table_name ") ;
while ($rows = $result->fetch_assoc()){
    $pid = $rows['productId'];
    $price = $rows['price'];
    $inStock = $rows['inStock'];
    if ($newPrice != $price || $newInStock != $inStock){
        $results = $mysqli->query("UPDATE $table_name SET price='$newPrice', inStock='$newInStock' WHERE productId='$pid' ");
        if($results){
            print 'Success! record updated'; 
        }else{
            print 'Error : ('. $mysqli->errno .') '. $mysqli->error;
        }
    }
}