如何将json数据从url发送到数据库并进行更新

时间:2016-04-30 22:09:51

标签: php mysql json

HY,

我无法弄清楚如何让它发挥作用。

如何将主页> name,home>得分发布到数据库(相同的用于),并将状态,回合和日期时间发送到数据库。 但是如果json url中有更改而不是更新数据库,并且如果有新匹配则插入它。

这一切都需要自动完成。

[
   {
  "url":"http://www.gosugamers.net/counterstrike/tournaments/10423-starladder-cis-minor-championship-cologne-2016/2865-playoffs/10426-playoffs/matches/113497-worst-players-vs-team-empire",
  "home":{
     "name":"Worst Players",
     "url":"/counterstrike/teams/15191-worst-players",
     "country":"Russian Federation",
     "rank":57,
     "score":1
  },
  "away":{
     "name":"Team Empire.",
     "url":"/counterstrike/teams/14464-team-empire",
     "country":"Russian Federation",
     "rank":40,
     "score":2
  },
  "status":"Complete",
  "type":"counterstrike",
  "rounds":"Best of 3",
  "valueBet":true,
  "datetime":1462024800
   },
   {
  "url":"http://www.gosugamers.net/counterstrike/tournaments/10296-esports-championship-series-season-1/2810-online-phase/10298-north-american-league/matches/111367-team-liquid-cs-vs-counter-logic-gaming-cs",
  "home":{
     "name":"Team Liquid.CS",
     "url":"/counterstrike/teams/10561-team-liquid-cs",
     "country":"United States",
     "rank":10
  },
  "away":{
     "name":"Counter Logic Gaming.CS",
     "url":"/counterstrike/teams/10569-counter-logic-gaming-cs",
     "country":"United States",
     "rank":39
  },
  "status":"Upcoming",
  "type":"counterstrike",
  "rounds":"Best of 3",
  "valueBet":true,
  "datetime":1462068000
   }
]

我试过这段代码

<?php

include("../../check.php"); 

$urljson = "http://localhost:5000/matches";
$json = file_get_contents($urljson);
$data = json_decode($json, TRUE);
$home = $data[0]['home'];
$away = $data[0]['away'];
$status = $data[0]['status'];
$type = $data[0]['rounds'];
$timestamp = $data[0]['datetime'];


foreach($data as $array) {
    echo $home['name'];
    echo '<br>';
    echo $home['score'];
echo '<br>';
echo $away['name'];
echo '<br>';
echo $away['score'];
echo '<br>';
echo $status;
echo '<br>';
echo $type;
echo '<br>';
echo $timestamp;
echo '<br>';
echo '<br>';
}


?>

但这就是网页给我的东西(它显示了多次相同的结果)

  

ANOX

     

注意:未定义的索引:在第18行的C:\ xampp \ htdocs \ main \ dashboard \ backend \ info.php中得分

     

最差玩家

     

注意:未定义的索引:第22行的C:\ xampp \ htdocs \ main \ dashboard \ backend \ info.php得分

     

待办

     

最好的3

     

1462100400

我希望有人可以帮助我,因为我现在被困4天了。

1 个答案:

答案 0 :(得分:0)

由于您在循环中使用了错误的变量,因此您始终获得相同的数据。在你的情况下使用$ array变量:

foreach($data as $array) {
    echo $array['name'];
    echo '<br>';
    echo $array['score'];
    echo '<br>';
    ... etc.
}

为了更好的可读性,可能最好调用变量$ row或$ match而不是$ array。

您收到“通知:未定义...”,因为有些JSON节点没有得分。在使用之前检查分数

if (isset($array['score'])) {
    // use it inside if:
    echo $array['score']
}

这里是更正后的代码:

<?php

include("../../check.php"); 

$urljson = "http://localhost:5000/matches";
$json = file_get_contents($urljson);
$data = json_decode($json, TRUE);

foreach($data as $match) {
    echo $match['home']['name'];
    echo '<br>';
    if (isset($match['home']['score'])) {
        echo $match['home']['score'];
        echo '<br>';
    }
    echo $match['away']['name'];
    echo '<br>';
    if (isset($match['away']['score'])) {
        echo $match['away']['score'];
        echo '<br>';
    }
    echo $match['status'];
    echo '<br>';
    echo $match['rounds'];
    echo '<br>';
    echo $match['datetime'];
    echo '<br>';
    echo '<br>';
}

?>