Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php中解析json_Php_Json - Fatal编程技术网

在php中解析json

在php中解析json,php,json,Php,Json,我遇到了一个奇怪的错误,代码运行平稳,但由于15天不工作,没有给我错误。这段代码有什么问题,还有其他选项吗。我有一个api,我想通过它读取所有记录,然后传输到数据库中。由于工作正常,但不工作,请检查 <?php $servername = "localhost"; $username = "root"; $password = "com";

我遇到了一个奇怪的错误,代码运行平稳,但由于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);
?>

要在页面中显示错误,请添加:

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

我们需要知道你得到的错误。我没有得到任何错误,它的空白检查输出你应该使用
INSERT。。。在重复密钥更新时,当插入失败时,不执行单独的
更新
查询。您的
UPDATE
查询中缺少大量引号。您还应该停止使用过时的mysql扩展,切换到PDO或mysqli,并使用带参数的准备语句,而不是将字符串替换到SQL中。@Barmar不会在服务器日志中收到任何错误