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

时间:2015-06-12 12:17:05

标签: php database mysqli sql-update auto-update

我们想要从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();
?>

1 个答案:

答案 0 :(得分:0)

您可以执行以下操作:

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