使用PHP将JSON信息发送到数据库?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了使用PHP将JSON信息发送到数据库?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3170字,纯文字阅读大概需要5分钟。
内容图文
![使用PHP将JSON信息发送到数据库?](/upload/InfoBanner/zyjiaocheng/907/2afd2904f1d6492b9791f82de33577a7.jpg)
我正在尝试使用PHP将数据从JSON文件发送到MySQL数据库.
我有99%的工作,但遇到了一个我无法弄清楚的小问题.这是我的代码:
$con = mysqli_connect($host, $username, $password, $dbname) or die('Error in Connecting: ' . mysqli_error($con));
$st = mysqli_prepare($con, 'INSERT INTO url_feed(url, results, current_date, networks, identifier) VALUES (?, ?, ?, ?, ?)');
mysqli_stmt_bind_param($st, 'sssss', $url, $results, $current_date, $networks, $identifier);
$filename = 'https://www.example.com/random.json';
$json = file_get_contents($filename);
$data = json_decode($json, true);
foreach ($data as $row) {
$url = $row['url'];
$identifier = $row['identifier'];
$current_date = $row['current_date'];
$results = $row['results'];
$networks = $row['networks'];
mysqli_stmt_execute($st);
}
mysqli_close($con);
这是一个包含3个对象的JSON的副本:
[
{
"url":"http://example1.com",
"identifier":495755330,
"current_date":"2015-12-30 17:05:45",
"results":3,
"networks":{
"FaceBook":{"detected":true,"result":"no-result"},
"Twitter Inc":{"detected":false,"result":"no-result"},
"Pinterest.com":{"detected":true,"result":"no-result"},
"Other Sites":{"detected":true,"result":"some-result"}
}
},
{
"url":"http://example2.com",
"identifier":495755331,
"current_date":"2015-12-30 17:05:46",
"results":0,
"networks":{
"FaceBook":{"detected":false,"result":"what-result"},
"Twitter Inc":{"detected":false,"result":"some-result"},
"Pinterest.com":{"detected":false,"result":"some-result"},
"Other Sites":{"detected":false,"result":"what-result"}
}
},
{
"url":"http://example3.com",
"identifier":495755332,
"current_date":"2015-12-30 17:05:47",
"results":1,
"networks":{
"FaceBook":{"detected":false,"result":"some-result"},
"Twitter Inc":{"detected":true,"result":"some-result"},
"Pinterest.com":{"detected":false,"result":"some-result"},
"Other Sites":{"detected":false,"result":"some-result"}
}
}
]
现在如果我运行脚本,它会将它插入到数据库中,如下所示:
id | url | results | current_date | networks | identifier | status
1 | http://example1.com | 3 | 2015-12-30 17:05:45 | Array | 495755330 | queued
2 | http://example2.com | 0 | 2015-12-30 17:05:46 | Array | 495755331 | queued
3 | http://example3.com | 1 | 2015-12-30 17:05:47 | Array | 495755332 | queued
但这就是我想要输入的方式:
id | url | results | current_date | networks | identifier | status
1 | http://example1.com | 3 | 2015-12-30 17:05:45 | FaceBook,Pinterest.com,Other Sites | 495755330 | queued
2 | http://example3.com | 1 | 2015-12-30 17:05:47 | Twitter Inc | 495755332 | queued
这是我无法弄清楚的部分:
它试图作为一个数组输入,这显然不起作用 – 它只是在网络列下插入文本“数组”.如果检测到设置为true,我只想插入数据库.如果不是,我不希望数据库中列出的社交网络.
如果某个对象的社交网络都没有设置为true,我根本不希望输入该对象.这就是为什么你看到我上面的例子中缺少某些网络以及第二行不存在的原因.
解决方法:
而不是$networks = $row [‘networks’];用这个:
$a = json_decode($row['networks'], true);
$b = array_filter($a, function($el) {
if ($el['detected'] == true) {
return true;
}
});
$c = implode(', ', array_keys($b));
$networks = $c;
内容总结
以上是互联网集市为您收集整理的使用PHP将JSON信息发送到数据库?全部内容,希望文章能够帮你解决使用PHP将JSON信息发送到数据库?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。