将json文件导入到mysql数据库中_使用php将JSON文件导入MYSQL数据库

我想尝试将JSON文件插入我的MYSQL数据库.

但是每次运行此脚本时只插入第二行.为“为foreach()提供的无效参数”中的一堆错误

我从另一个网页上获得了这个脚本,但是我无法让它顺利运行!

有人怎么能帮帮我?

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

{

$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

{

// 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

//

?>

错误:

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文件:

{

"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=***"

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值