用php实现JSON到数据库
所以我有这个JSON的例子用php实现JSON到数据库,php,mysql,json,database,Php,Mysql,Json,Database,所以我有这个JSON的例子 { "top" : [ { "info" : { "ID" : 0, "TID" : 1 }, "geo" : { "poins" : [ [ [ -5.9, 57.1 ], [ -5.9
{
"top" : [
{
"info" : {
"ID" : 0,
"TID" : 1
},
"geo" : {
"poins" : [
[
[
-5.9,
57.1
],
[
-5.99,
57.0
]
]
]
}
},
{
"info" : {
"ID" : 1,
"TID" : 2
},
"geo" : {
"points" : [
[
[
-5.4,
57.0
],
[
-5.9,
57.0
]
]
]
}
}
]
}
我需要用php将这些信息放到数据库中
所以我在DB中有一个称为points的列,它需要里面的数据看起来像:
[-5.4, 57.0],[-5.9, 57.0]
我有一个带有ID的columb,所以我只需要为每个ID放置JSON中的点
我的php应该像这样运行:
与数据库的连接
$str = file_get_contents(the JSON);
$json = json_decode($str, true);
foreach ($json['top'] as $field) {
query='UPDATE poins_table
SET points='$field['geo']['points']'
WHERE ID='$field['info']['ID']' '
}
代码似乎不起作用。我错过了什么…任何暗示都会有帮助。谢谢您可以尝试以下内容:
<?php
$str = file_get_contents('the JSON URL');
$json = json_decode($str, true);
try{
$db = new PDO("dbtype:host=yourhost;dbname=yourdbname;charset=utf8","username","password");
$query = $db->prepare('UPDATE poins_table SET points=? WHERE ID=?');
foreach ($json['top'] as $field) {
$query->execute(array(json_encode($field['geo']['points']), $field['info']['ID']));
}
} catch(PDOException $e) {
echo "Error: ". $e;
}
?>
您的数据库连接在哪里?什么样的数据库?你尝试过什么吗?我正在使用MySQL,这不是连接问题。。我所需要的就是从JSON的id中获取每个点的信息,这样foreach就应该有了。其余的就不那么重要了…如果这不是解决方案,它应该是什么样子的。。。如果你只知道你有这个JSON,你需要把它放在数据库中……你的代码看起来会是什么样子的@ГББППППППааСаааааааааа107。