【问题标题】:Entering JSON data into MySQL将 JSON 数据输入 MySQL
【发布时间】:2016-04-29 20:20:29
【问题描述】:

我正在尝试获取 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"
      }
   }
}

我需要做的就是将networksdetected 设置为true,然后使用变量$socials 将它们插入我的数据库中。

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

如果没有将detected 设置为truenetworks,那么我想在该列中输入NONE FOUND

有谁知道我如何使用 JSON 输出将其输入我的数据库?

【问题讨论】:

标签: php mysql sql json


【解决方案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
                )

        )

)

然后对数组执行一个循环。我希望你知道其余的。你现在应该怎么做。

【讨论】:

  • 将其设为数组以便更好地处理json_decode($json, true)
【解决方案2】:
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

现在输入你的更新代码就可以了。

【讨论】:

  • @user10848.很高兴为您提供帮助。
猜你喜欢
  • 2011-01-30
  • 1970-01-01
  • 2013-03-25
  • 2015-01-09
  • 2020-12-07
  • 2019-04-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-31
相关资源
最近更新 更多