我需要将php编码数组的元素插入到数据库中,并且我完全卡住了。我首先使用json编码来使用SQL查询从数据库中获取数据(我成功了),但现在我需要能够做相反的事情。如果有人可以帮助,我会非常感激。这是我第二天的工作,我做得不太好。下面是我的代码:插入php json编码到数据库中的数组
$UserCoords_Query = "Select Accuracy, Longitude, Latitude, Timestamp
FROM UserDetails
WHERE UserId =" . $UserID;
$UserCoords_result = mysql_query($UserCoords_Query);
if (mysql_num_rows($UserCoords_result) == 0) {
echo "There are no users with an id of ". $UserID;
}
else {
$EmptyArray=array();
while ($row = mysql_fetch_array($UserCoords_result)) {
$Accuracy=$row['Accuracy'];
$Longitude= $row['Longitude'];
$Latitude=$row['Latitude'];
$Timestamp= $row['Timestamp'];
$Queue= array('Accuracy:' => $Accuracy, 'Latitude' => $Latitude, 'Longitude' => $Longitude, 'Timestamp' => $Timestamp);
array_unshift($EmptyArray,$Queue);
}
$ObjectResponse = array('Coords' => $EmptyArray);
echo json_encode($ObjectResponse);
$Json_Encoded= json_encode($ObjectResponse);
$Json_Decoded= json_decode($Json_Encoded, true);
$Update_Query= "INSERT INTO UserDetails (UserId, Accuracy, Latitude, Longitude, Timestamp)
VALUES ('".$UserID."','".$Json_Decoded[0] ['Accuracy']."','".$Json_Decoded[0]['Latitude']."',
'".$Json_Decoded[0]['Longitude']."','".$Json_Decoded[0]['Timestamp']."')";
mysql_query($Update_Query) or die(mysql_error());
你得到的错误是什么? – 2013-05-08 16:04:42
你不应该将json文本插入到数据库中,除非你打算永远不要使用sql访问json文本中的任何单独的子数据。 json的传输格式,而不是存储格式。 – 2013-05-08 16:05:29
@ Marc B嗯,我需要爆炸字符串来获取元素并将它们添加到数据库中。我认为这是什么JSON解码功能呢? – user2363025 2013-05-08 16:24:25