我正在尝试使用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
这是我无法弄清楚的部分:
它试图以数组形式输入,这显然不起作用 - 它只是在networks
列下插入文本“数组”。如果detected
设置为true
,我只想插入数据库。如果不是,我不希望数据库中列出该社交网络。
如果某个对象的社交网络都没有设置为true
,我根本不希望输入该对象。这就是为什么你看到我上面的例子中缺少某些网络以及第二行不存在的原因。
答案 0 :(得分:1)
在将值插入数据库之前,请对$ netwrok的值进行一些检查。只有在验证了值时才插入
foreach ($data as $row) {
$url = $row['url'];
$identifier = $row['identifier'];
$current_date = $row['current_date'];
$results = $row['results'];
$networks = $row['networks'];
//**insert you checks here ***//
mysqli_stmt_execute($st);
}
答案 1 :(得分:1)
$data = json_decode($json, true);
foreach ($data as $row) {
$url = $row['url'];
$identifier = $row['identifier'];
$current_date = $row['current_date'];
$results = $row['results'];
$network_row = $row['networks'];
$networks = '';
foreach($network_row as $key => $val) {
if ($val->detected == true) {
$networks .= $key . ',';
}
}
if (mb_strlen($networks, 'utf-8') > 0) {
$networks = substr($networks, 0, mb_strlen($networks, 'utf-8')-1);
mysqli_stmt_execute($st);
}
}
答案 2 :(得分:1)
使用implode()函数作为插入的一部分。 然后,您可以在检索时使用explode()函数。
答案 3 :(得分:1)
而不是$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;