使用php将JSON文件导入MYSQL数据库 [英] Import JSON file into MYSQL database with php
本文介绍了使用php将JSON文件导入MYSQL数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
但每次运行此脚本时,只插入第二行。为为foreach()提供的无效参数中的一堆错误
我从其他网页获得此脚本但我无法顺利工作!
有人怎么能帮帮我?
php脚本:
<?php
$ con = mysql_connect(localhost,***,***);
if(!$ con)
{
die('无法连接:'。mysql_error());
}
mysql_select_db(***,$ con);
$ f = file_get_contents('http://.../data.json');
$ arr = explode('},',$ f); //准备json_decode但是最后}缺少
$ global_arr = array(); //包含每个解码的json(TABLE ROW)
$ global_keys = array(); //包含SQL
的列if(!function_exists('json_decode'))die('你的主机不支持json');
for($ i = 0; $ i< count($ arr); $ i ++)
{
$ decoding = json_decode($ arr [$ i]。'}',true) ; // Reappend last}或者它将返回NULL
$ global_arr [] = $ decoding;
foreach($解码为$ key => $ value)
{
$ global_keys [$ key] ='';
}
}
//迭代$ global_arr
for($ i = 0; $ i< count($ global_arr); $ i ++)//这是比foreach更快
{
//现在使用ardav建议
foreach($ global_arr [$ i]作为$ key => $ value){
$ sql [] = (is_numeric($ value))? `$ key` = $ value:`$ key` ='。 mysql_real_escape_string($ value)。 ;
}
$ sqlclause = implode(,,$ sql);
$ rs = mysql_query(INSERT INTO temp_table SET $ sqlclause);
} // for i
//
?>
错误:
警告:为第21行***。php中的foreach()提供的参数无效
警告:在第21行的***。php中为foreach()提供的参数无效
警告:为第31行***。php中的foreach()提供的参数无效
警告:implode()[function.implode]:传入的参数无效* **。第34行的PHP
警告:为第31行***。php中的foreach()提供的参数无效
Json文件:
{
data:[
{
name:name freiend 1,
id:friend id 1
},
{
name: name freiend 2,
id:friend id 2
},
{
name:name freiend 3,
id :朋友ID 3
}
],
分页:{
next:https://graph.facebook.com/100002295143005/friends? access_toke n = ***
}
}
解决方案
尝试解析整个JSON文件而不是一个部分,并使用更少的循环:
$ f = file_get_contents(的 'http://.../data.json');
if(!function_exists('json_decode'))die('你的主机不支持json');
$ feed = json_decode($ f);
for($ i = 0; $ i< count($ feed ['data']); $ i ++)
{
$ sql = array();
foreach($ feed ['data'] [$ i] as $ key => $ value){
$ sql [] =(is_numeric($ value))? `$ key` = $ value:`$ key` ='。 mysql_real_escape_string($ value)。 ;
}
$ sqlclause = implode(,,$ sql);
$ rs = mysql_query(INSERT INTO temp_table SET $ sqlclause);
}
i'am trying to insert a JSON file into my MYSQL Database. But only the second line is inserted every time i run this script. along whit a bunch of errors for "Invalid argument supplied for foreach()"
I got this script from another webpage but i can't get it work smooth! Someone how can help me out?
php script:
<?php
$con = mysql_connect("localhost","***","***");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("***", $con);
$f = file_get_contents('http://.../data.json');
$arr = explode('},',$f); // Prepare for json_decode BUT last } missing
$global_arr = array(); // Contains each decoded json (TABLE ROW)
$global_keys = array(); // Contains columns for SQL
if(!function_exists('json_decode')) die('Your host does not support json');
for($i=0; $i<count($arr); $i++)
{
$decoded = json_decode($arr[$i].'}',true); // Reappend last } or it will return NULL
$global_arr[] = $decoded;
foreach($decoded as $key=> $value)
{
$global_keys[$key] = '';
}
}
// iterate $global_arr
for($i=0; $i<count($global_arr); $i++) // this is faster than foreach
{
// NOW use what ardav suggested
foreach($global_arr[$i] as $key => $value){
$sql[] = (is_numeric($value)) ? "`$key` = $value" : "`$key` = '" . mysql_real_escape_string($value) . "'";
}
$sqlclause = implode(",",$sql);
$rs = mysql_query("INSERT INTO temp_table SET $sqlclause");
} // for i
//
?>
the errors:
Warning: Invalid argument supplied for foreach() in ***.php on line 21
Warning: Invalid argument supplied for foreach() in ***.php on line 21
Warning: Invalid argument supplied for foreach() in ***.php on line 31
Warning: implode() [function.implode]: Invalid arguments passed in ***.php on line 34
Warning: Invalid argument supplied for foreach() in ***.php on line 31
Json file:
{
"data": [
{
"name": "name freiend 1",
"id": "friend id 1"
},
{
"name": "name freiend 2",
"id": "friend id 2"
},
{
"name": "name freiend 3",
"id": "friend id 3"
}
],
"paging": {
"next": "https://graph.facebook.com/100002295143005/friends?access_token=***"
}
}
解决方案
Try parsing the entire JSON file instead of one part, and with fewer loops:
$f = file_get_contents('http://.../data.json');
if(!function_exists('json_decode')) die('Your host does not support json');
$feed = json_decode($f);
for($i=0; $i<count($feed['data']); $i++)
{
$sql = array();
foreach($feed['data'][$i] as $key => $value){
$sql[] = (is_numeric($value)) ? "`$key` = $value" : "`$key` = '" . mysql_real_escape_string($value) . "'";
}
$sqlclause = implode(",",$sql);
$rs = mysql_query("INSERT INTO temp_table SET $sqlclause");
}
这篇关于使用php将JSON文件导入MYSQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文