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

4.3k Views Asked by At

We want to update database from API data, we have almost 100000 Records that all records are update day by day, i need to update with new data by using API.

Script Working Details We are fetching $productId From store and from this $productId we will fetch updated data from API Script, after getting updated data we will update that updated data in to the store table.

is it possible that after update one data its automatic click on one button and its going for update for next $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();
?>
1

There are 1 best solutions below

1
On

You can do as follows :

$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;
        }
    }
}