JSON 到 MySql 使用 PHP

JSON to MySql using PHP

你好,我需要你的帮助

我正在为 class 做一个项目,我希望从 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");
}

我没有触发任何错误消息出现,但数据库中没有数据。我究竟做错了什么?是 php 代码正确读取了 json 字符串吗???

通过仔细观察你的问题,我发现问题出在foreach() Loop里面。应该是

foreach($arr as $item){
    echo  $aq1 = $item[0]['aq1']; # note that [0] here 
    echo  $aq3 = $item[0]['aq3'];
    #...........Rest of Loop
}

并重命名像这样的变量 $aqf/_source$aqf_source ,依此类推。

我成功了。

foreach ($phpArray['results'] as $key => $value) {

// This is to make the data to be saved into the database

foreach ($value as $k => $v) {
        $aq1 = $value['aq1'];
        $aq3 = $value['aq3'];
        $aq2 = $value['aq2'];
        $ateam_source = $value['ateam/_source'];
        $aq4 =$value['aq4'];
        $hqf_source =$value['hqf/_source'];
        $hq2 =$value['aq2'];
        $hq1 =$value['aq1'];
        $hq4 =$value['aq4'];
        $hq3 =$value['aq3'];
        $aqf_source =$value['aqf/_source'];
        $ateam_text =$value['ateam/_text'];
        $aq3_source =$value['aq3/_source'];
        $hq4_source =$value['hq4/_source'];
        $hteam =$value['hteam'];
        $aq2_source =$value['aq2/_source'];
        $hteam_text =$value['hteam/_text'];
        $aq1_source =$value['aq1/_source'];
        $hteam_source =$value['hteam/_source'];
        $ateam =$value['ateam'];
        $aqf =$value['aqf'];
        $hq1_source =$value['hq1/_source'];
        $hq3_source =$value['hq3/_source'];
        $hq2_source =$value['hq2/_source'];
        $hqf =$value['hqf'];
        $aq4_source =$value['hq4/_source'];
}