只有1个json数组使用php进入mysql

83qze16e  于 2021-06-18  发布在  Mysql
关注(0)|答案(0)|浏览(218)

你好。

json文件有13个数组,但是对于这个问题我只显示了2个数组(结构上是一样的)。
php(a)脚本只将2个json数组中的1个解析到mysql数据库中。。。
我有另一个php(b)测试脚本,它“echo的”完全相同的json文件到屏幕上,并显示2个“数组”。。。我知道这是因为我在php(b)中为每个数组编写$variable输出的方式,而不是“foreach”循环(因此直接从url工作并将输出到屏幕)
但我似乎不能将相同的“逻辑”应用到php(a)脚本到db中,你也可以看到一些“注解掉”的脚本我试图应用,但没有工作。。。
那么php(a)脚本的错误在哪里呢。。。感谢您的帮助。。。

json脚本

{
  "success": true,
  "data": [
    {
      "sport_key": "soccer_epl",
      "sport_nice": "EPL",
      "teams": [
        "Arsenal",
        "Fulham"
      ],
      "commence_time": 1538910000,
      "home_team": "Fulham",
      "sites": [
        {
          "site_key": "unibet",
          "site_nice": "Unibet",
          "last_update": 1538860569,
          "odds": {
            "h2h": [
              1.6,
              5.37,
              4.67
            ]
          }
        },
        {
          "site_key": "crownbet",
          "site_nice": "CrownBet",
          "last_update": 1538860546,
          "odds": {
            "h2h": [
              1.59,
              4.9,
              4.4
            ]
          }
        },
        {
          "site_key": "beteasy",
          "site_nice": "Bet Easy",
          "last_update": 1538860572,
          "odds": {
            "h2h": [
              1.59,
              4.9,
              4.4
            ]
          }
        },
        {
          "site_key": "sportsbet",
          "site_nice": "SportsBet",
          "last_update": 1538860570,
          "odds": {
            "h2h": [
              1.57,
              5,
              4.5
            ]
          }
        },
        {
          "site_key": "betfair",
          "site_nice": "Betfair",
          "last_update": 1538860581,
          "odds": {
            "h2h": [
              1.64,
              5.6,
              4.6
            ]
          }
        },
        {
          "site_key": "neds",
          "site_nice": "Neds",
          "last_update": 1538860571,
          "odds": {
            "h2h": [
              1.62,
              5,
              1.62
            ]
          }
        },
        {
          "site_key": "ladbrokes",
          "site_nice": "Ladbrokes",
          "last_update": 1538860575,
          "odds": {
            "h2h": [
              1.6,
              5,
              4.33
            ]
          }
        },
        {
          "site_key": "tab",
          "site_nice": "TAB",
          "last_update": 1538860547,
          "odds": {
            "h2h": [
              1.6,
              5,
              4.2
            ]
          }
        },
        {
          "site_key": "ubet",
          "site_nice": "UBet",
          "last_update": 1538860577,
          "odds": {
            "h2h": [
              1.65,
              4.5,
              4.2
            ]
          }
        }
      ],
      "sites_count": 9
    },
    {
      "sport_key": "soccer_epl",
      "sport_nice": "EPL",
      "teams": [
        "Chelsea",
        "Southampton"
      ],
      "commence_time": 1538918100,
      "home_team": "Southampton",
      "sites": [
        {
          "site_key": "unibet",
          "site_nice": "Unibet",
          "last_update": 1538860569,
          "odds": {
            "h2h": [
              1.65,
              5.9,
              4
            ]
          }
        },
        {
          "site_key": "crownbet",
          "site_nice": "CrownBet",
          "last_update": 1538860546,
          "odds": {
            "h2h": [
              1.62,
              5.5,
              3.85
            ]
          }
        },
        {
          "site_key": "beteasy",
          "site_nice": "Bet Easy",
          "last_update": 1538860572,
          "odds": {
            "h2h": [
              1.62,
              5.5,
              3.85
            ]
          }
        },
        {
          "site_key": "sportsbet",
          "site_nice": "SportsBet",
          "last_update": 1538860570,
          "odds": {
            "h2h": [
              1.57,
              5.5,
              4
            ]
          }
        },
        {
          "site_key": "betfair",
          "site_nice": "Betfair",
          "last_update": 1538860581,
          "odds": {
            "h2h": [
              1.66,
              6.2,
              4.2
            ]
          }
        },
        {
          "site_key": "neds",
          "site_nice": "Neds",
          "last_update": 1538860571,
          "odds": {
            "h2h": [
              1.62,
              5.75,
              1.62
            ]
          }
        },
        {
          "site_key": "ladbrokes",
          "site_nice": "Ladbrokes",
          "last_update": 1538860575,
          "odds": {
            "h2h": [
              1.62,
              5.25,
              3.9
            ]
          }
        },
        {
          "site_key": "tab",
          "site_nice": "TAB",
          "last_update": 1538860547,
          "odds": {
            "h2h": [
              1.6,
              5.5,
              3.8
            ]
          }
        },
        {
          "site_key": "ubet",
          "site_nice": "UBet",
          "last_update": 1538860577,
          "odds": {
            "h2h": [
              1.62,
              5.5,
              3.8
            ]
          }
        }
      ],
      "sites_count": 9
    }
  ]
}

php(a)脚本

<?php

    $host = "localhost";
    $username = "web";
    $password = "";
    $dbname = "football";
    $con = mysqli_connect($host, $username, $password, $dbname) or die('Error in Connecting: ' . mysqli_error($con));

    $st = mysqli_prepare($con, 'INSERT INTO epl_odds(sport_nice, team1, team2, commence_time, home_team, site_nice, home, draw, away) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)');

    mysqli_stmt_bind_param($st, 'sssssssss', $sport_nice, $team1, $team2, $commence_time, $home_team, $site_nice, $home, $draw, $away);

    $filename = 'json.json';
    $json = file_get_contents($filename);

    $data = json_decode($json, true);

    foreach ($data as $row) {
      $sport_nice = $row[0]['sport_nice'];
      $team1 = $row[0]['teams'][0];
      $team2 = $row[0]['teams'][1];
      $commence_time = $row[0]['commence_time'];
      $home_team = $row[0]['home_team'];
      $site_nice = $row[0]['sites'][0]['site_nice'];
      $home = $row[0]['sites'][0]['odds']['h2h'][0];
      $draw = $row[0]['sites'][0]['odds']['h2h'][1];
      $away = $row[0]['sites'][0]['odds']['h2h'][2];

      $sport_nice = $row[1]['sport_nice'];
        $team1 = $row[1]['teams']['0'];
        $team2 = $row[1]['teams']['1'];
        $commence_time = $row[1]['commence_time'];
        $home_team = $row[1]['home_team'];
        $site_nice = $row[1]['sites'][0]['site_nice'];
        $home = $row[1]['sites'][0]['odds']['h2h']['0'];
        $draw = $row[1]['sites'][0]['odds']['h2h']['1'];
        $away = $row[1]['sites'][0]['odds']['h2h']['2'];

              /*foreach ($data as $row) {
              $sport_nice = $row->sport_nice;
              $team1 = $row->teams[0];
              $team2 = $row->teams[1];
              $commence_time = $row->commence_time;
              $home_team = $row->home_team;
              $site_nice = $row->sites->site_nice;
              $home = $row->sites->odds->h2h[0];
              $draw = $row->sites->odds->h2h[1];
              $away = $row->sites->odds->h2h[2];*/

        mysqli_stmt_execute($st);
    }

    mysqli_close($con);
?>

php(b)脚本

<?php

$filename = 'json13m.json';
$strjson = file_get_contents($filename);

$json = json_decode($strjson, true);

echo '<pre>';
var_export($json);
echo '</pre>';

$sport_nice = $json['data'][0]['sport_nice'];
$teamsA = $json['data'][0]['teams'][0];
$teamsB = $json['data'][0]['teams'][1];
$home = $json['data'][0]['sites'][0]['odds']['h2h'][0];
$draw = $json['data'][0]['sites'][0]['odds']['h2h'][1];
$away = $json['data'][0]['sites'][0]['odds']['h2h'][2];
$sport_nice1 = $json['data'][1]['sport_nice'];
$teamsA1 = $json['data'][1]['teams'][0];
$teamsB1 = $json['data'][1]['teams'][1];
$home1 = $json['data'][1]['sites'][0]['odds']['h2h'][0];
$draw1 = $json['data'][1]['sites'][0]['odds']['h2h'][1];
$away1 = $json['data'][1]['sites'][0]['odds']['h2h'][2];
echo $sport_nice .'<br/>'. $teamsA .'<br/>'. $teamsB .'<br/>'. $home .'<br/>'. $draw .'<br/>'. $away;
echo '<p>------------------</p>';
echo $sport_nice1 .'<br/>'. $teamsA1 .'<br/>'. $teamsB1 .'<br/>'. $home1 .'<br/>'. $draw1 .'<br/>'. $away1;

?>

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题