在php中解析json

时间:2017-04-08 05:09:58

标签: php json

我让我在代码运行顺利后面临一个奇怪的错误但是从15天开始它不工作并且没有给我错误。我们在这段代码中遇到了什么问题,还有其他选择吗?我有api,我希望获取所有记录,然后转移到数据库。它的工作正常,但不能正常工作,请检查。

<?php

            $servername = "localhost";
                       $username = "root";
                       $password = "com";
                       $dbname = "sabk";
    $con = mysql_connect($servername,$username,$password) or die('Could not connect: ' . mysql_error());


    mysql_select_db($dbname, $con);
    $jsondata = file_get_contents('http://tools.vcommission.com/api/coupons.php?apikey=e159f64e3dd49fddc3bb21dcda70f10c6670ea91aac30c7cb1d4ed37b20c45b8');
    echo "<script>console.log($jsondata);</script>";
    echo $jsondata;
    $data = json_decode($jsondata, true);


     foreach ($data as $row)
    {
        $promo_id=$row['promo_id'];
        $offer_name=$row['offer_name'];
        $coupon_title=$row['coupon_title'];
        $category=$row['category'];
        $coupon_description=$row['coupon_description'];
        $coupon_type=$row['coupon_type'];
        $coupon_code=$row['coupon_code'];
        $link=$row['link'];
        $coupon_expiry=$row['coupon_expiry'];
        $added=$row['added'];
        $store_image=$row['store_image'];
        $sql = "INSERT INTO Vcom(promo_id,offer_name,coupon_title,category,coupon_description,coupon_type,coupon_code,link,coupon_expiry,added,store_image)
    VALUES('$promo_id' ,'$offer_name','$coupon_title','$category','$coupon_description','$coupon_type','$coupon_code','$link','$coupon_expiry','$added','$store_image')";
    $retval = mysql_query( $sql, $con );

    if(!$retval)
    {
      $sql = "UPDATE Vcom SET offer_name=$offer_name,coupon_title=$coupon_title,category=$category,
      coupon_description=$coupon_description ,coupon_type=$coupon_type,coupon_code=$coupon_code,
      link=$link,coupon_expiry=$coupon_expiry,added=$added
    WHERE promo_id=$promo_id";
     mysql_query( $sql, $con );
            echo "Data Updated ".'<br>';

    }
    else
    {
        echo "Entered data successfully\n";
    }






    }
     mysql_close($con);
?>

1 个答案:

答案 0 :(得分:0)

为了在页面中显示错误添加:

error_reporting(E_ALL);    
ini_set('display_errors', 'On');