【发布时间】:2015-03-11 08:32:42
【问题描述】:
你好,我需要你的帮助
我正在做一个课程项目,我希望从 JSON 写入 MySql 数据库。
JSON 看起来像这样
{
"offset": 0,
"results": [
{
"aq1": 22,
"aq3": 27,
"aq2": 27,
"ateam/_source": "/nba/teams/page/NO/new-orleans-pelicans",
"aq4": 24,
"hqf/_source": "108",
"hq2": 27,
"hq1": 22,
"hq4": 36,
"hq3": 23,
"aqf/_source": "100",
"ateam/_text": "New Orleans",
"aq3/_source": "27",
"hq4/_source": "36",
"hteam": "http://www.cbssports.com/nba/teams/page/BOS/boston-celtics",
"aq2/_source": "27",
"hteam/_text": "Boston",
"aq1/_source": "22",
"hteam/_source": "/nba/teams/page/BOS/boston-celtics",
"ateam": "http://www.cbssports.com/nba/teams/page/NO/new-orleans-pelicans",
"aqf": 100,
"hq1/_source": "22",
"hq3/_source": "23",
"hq2/_source": "27",
"hqf": 108,
"aq4/_source": "24"
},
],
"cookies": [],
"connectorVersionGuid": "dgsadjgofsjkgkfsmghjfhf",
"connectorGuid": "fbcjsdbgfjdjgkf",
"pageUrl": "http://xsftbfbjjjsfaf.com/"
}
我的 php 是这样的
<?php
$server = "localhost";
$username = "lies";
$password = "madeup";
$database = "dgjsfdgjv";
mysql_connect($server,$username,$password) or die("Failed");
mysql_select_db($database) or die("Database Failed");
$url = "https://api.import.io/store/data/jjjjj-ec0f-4553-bda5-def61ca1756c/_query?input/webpage/url=http%3A%2F%2Fwww.jjjjj.com%2Fnba%2Fscoreboard%2F20150111&_user=f1508a78-05c5-4487-9219-51a990391329&_apikey=aLZu2wmRCvUlBwopi%2F6Kj%2F4wnscvZRh7DYQf80LE2e3hL22rtAp0nJ3lujy10cyx9JC9Ed73xb3zGp3aArhYDQ%3D%3D";
$string = file_get_contents($url);
$arr = json_decode($string, true);
foreach($arr as $item){
$aq1 = $item['aq1'];
$aq3 = $item['aq3'];
$aq2 = $item['aq2'];
$ateam/_source = $item['aqeam/_source'];
$aq4 =$item['aq4'];
$hqf/_source =$item['aqf/_source'];
$hq2 =$item['aq2'];
$hq1 =$item['aq1'];
$hq4 =$item['aq4'];
$hq3 =$item['aq3'];
$aqf/_source =$item['aqf/_source'];
$ateam/_text =$item['aqeam/_text'];
$aq3/_source =$item['aq3/_source'];
$hq4/_source =$item['aq4/_source'];
$hteam =$item['aqeam'];
$aq2/_source =$item['aq2/_source'];
$hteam/_text =$item['aqeam/_text'];
$aq1/_source =$item['aq1/_source'];
$hteam/_source =$item['aqeam/_source'];
$ateam =$item['aqeam'];
$aqf =$item['aqf'];
$hq1/_source =$item['aq1/_source'];
$hq3/_source =$item['aq3/_source'];
$hq2/_source =$item['aq2/_source'];
$hqf =$item['aqf'];
$aq4/_source =$item['aq4/_source'];
mysql_query("INSERT INTO createdb (aq1,aq3,aq2,ateam/_source,aq4,hqf/_source,hq2,hq1,hq4,hq3,aqf/_source,ateam/_text,aq3/_source,hq4/_source,hteam,aq2/_source, hteam/_text,aq1/_source,hteam/_source,ateam,aqf,hq1/_source,hq3/_source,hq2/_source,hqf,aq4/_source) VALUES('$aq1','$aq3','$aq2','$ateam/_source','$aq4','$hqf/_source','$hq2','$hq1','$hq4','$hq3','$aqf/_source','$ateam/_text', '$aq3/_source','$hq4/_source','$hteam','$aq2/_source','$hteam/_text','$aq1/_source','$hteam/_source','$ateam','$aqf','$hq1/_source','$hq3/_source','$hq2/_source','$hqf','$aq4/_source')") or die ("Failed");
}
我没有触发任何错误消息出现,但数据库中没有数据。我究竟做错了什么?是正确读取json字符串的php代码吗???
【问题讨论】:
-
Mysql_* 函数已正式弃用,请查看 PDO 或 Prepared Statements。
-
这些$hqf/_source、$aq4/_source是什么
-
而不是
$aq1 = $item['aq1']在 foreach 循环中试试这个$aq1 = $item['results']['aq1'];。 -
您的 JSON 有问题,我尝试使用 json 数据运行 json_decode 失败。
Syntax error, malformed JSON -
当我转到您的 apiUrl 时,它给了我一个错误的答案。也许你应该 var_dump() $string 并确保它是一个好的 json。