gpt4 book ai didi

php - 将 JSON 数据输入 MySQL

转载 作者:行者123 更新时间:2023-11-29 02:18:50 25 4
gpt4 key购买 nike

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

如果没有networksdetected设置为 true , 那么我要输入 NONE FOUND改为进入该列。

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

最佳答案

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

现在输入您的更新代码即可。

关于php - 将 JSON 数据输入 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34960161/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com