将JSON数据输入MySQL

时间:2016-01-23 05:55:53

标签: php mysql sql json

我正在尝试获取JSON数据并将其输入我的数据库。

这是我目前的代码:

while($row= mysqli_fetch_assoc($query)){
    $id = $row["id"];
    $domain = $row["domain"];
    $time = date('Y-m-d H:i:s');

    $ch = curl_init();
    $url = "https://www.example.com/";
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, "apikey=123&test=$domain");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $output = curl_exec ($ch);
    print $output;

    // CODE SHOULD BE ADDED HERE I THINK

    curl_close ($ch);

    $sql_to_update = "UPDATE monitoring SET socials='$socials', update_time='$time' WHERE id='$id'";
    mysqli_query($conn,$sql_to_update);
}

以下是我得到的JSON结果示例:

{
   "resource":"random data",
   "tests":100,
   "total":250,
   "networks":{
      "FaceBook":{
         "detected":true,
         "result":"ignore"
      },
      "Twitter Inc":{
         "detected":false,
         "result":"ignore"
      },
      "MySpace":{
         "detected":true,
         "result":"ignore"
      },
      "Pinterest":{
         "detected":true,
         "result":"ignore"
      },
      "Instagram":{
         "detected":false,
         "result":"ignore"
      }
   }
}

我需要做的就是将networks detected设置为true并使用变量$socials将其插入我的数据库。

例如,对于上面列出的JSON示例,我希望将以下内容输入数据库的socials列:FaceBook, MySpace, Pinterest

如果networks没有detected设置为true,那么我想将NONE FOUND输入该列。

任何人都知道我如何使用JSON输出将其导入我的数据库?

2 个答案:

答案 0 :(得分:1)

使其成为一个数组,以便更好地处理json_decode($json, true)

echo '<pre>';
print_r(json_decode($json, true));
echo '</pre>';

你会得到像这样的stdClass对象

stdClass Object
(
    [resource] => random data
    [tests] => 100
    [total] => 250
    [networks] => stdClass Object
        (
            [FaceBook] => stdClass Object
                (
                    [detected] => 1
                    [result] => ignore
                )

            [Twitter Inc] => stdClass Object
                (
                    [detected] => 
                    [result] => ignore
                )

            [MySpace] => stdClass Object
                (
                    [detected] => 1
                    [result] => ignore
                )

            [Pinterest] => stdClass Object
                (
                    [detected] => 1
                    [result] => ignore
                )

            [Instagram] => stdClass Object
                (
                    [detected] => 
                    [result] => ignore
                )

        )

)
然后

在数组上执行循环。我希望你知道其余的事情。你现在应该怎么做

答案 1 :(得分:1)

Check the code to get the desired data that you want to update:-

<?php
$link = '{
   "resource":"random data",
   "tests":100,
   "total":250,
   "networks":{
      "FaceBook":{
         "detected":true,
         "result":"ignore"
      },
      "Twitter Inc":{
         "detected":false,
         "result":"ignore"
      },
      "MySpace":{
         "detected":true,
         "result":"ignore"
      },
      "Pinterest":{
         "detected":true,
         "result":"ignore"
      },
      "Instagram":{
         "detected":false,
         "result":"ignore"
      }
   }
}'; // suppose json variable is $link
$dataArr = json_decode($link);
echo "<pre/>";print_r($dataArr);// print the variable to see how json data looks when it converted to php array
$socials = '';
foreach($dataArr->networks as $key=>$dataA){
    if($dataA->detected == 1){
        $socials .= $key.',';
    }

}
$socials = trim($socials,',');
echo $socials;
?>

输出: - https://eval.in/506693

现在把你的更新代码放在那里。