简体   繁体   中英

How to loop through PHP code to insert JSON data into MySQL database

Here is my current PHP code - this only brings in one row of data and I need it to loop through the JSON which is dynamic and changes - it could be 5 rows or 10 rows at different times.

I have tried multiple different ways to foreach() loop through the data and none have worked.

$jsonCont = file_get_contents('nba.json');

$data = json_decode($jsonCont, true);

    $tip = $data['data'][0]['commence_time'];
    $teama = $data['data'][0]['teams'][0];
    $linea = $data['data'][0]['sites'][0]['odds']['spreads']['points'][0];
    $teamb = $data['data'][0]['teams'][1];
    $lineb = $data['data'][0]['sites'][0]['odds']['spreads']['points'][1];

$sql = "INSERT INTO nbagames(tip, teama, linea, teamb, lineb)
VALUES('$tip', '$teama', '$linea', '$teamb', '$lineb')";
if(!mysqli_query($conn,$sql))
{
    die('Error : ' . mysql_error());
}

What do I need to add to this code so it will loop through and grab all the data and not just 1 row?

EDIT: In reply to adding the JSON data - here is an example - I need to get every game - but I don't need multiple sites (if there are more than 1 site with the odds)

data: [
{
sport_key: "basketball_nba",
sport_nice: "NBA",
teams: [
"Charlotte Hornets",
"Washington Wizards"
],
commence_time: 1546128000,
home_team: "Washington Wizards",
sites: [
{
site_key: "mybookieag",
site_nice: "MyBookie.ag",
last_update: 1546136064,
odds: {
spreads: {
odds: [
1.8,
1.901
],
points: [
"3.5",
"-3.5"
]
}
}
}
],
sites_count: 1
},
{
sport_key: "basketball_nba",
sport_nice: "NBA",
teams: [
"Houston Rockets",
"New Orleans Pelicans"
],
commence_time: 1546128600,
home_team: "New Orleans Pelicans",
sites: [
{
site_key: "mybookieag",
site_nice: "MyBookie.ag",
last_update: 1546136064,
odds: {
spreads: {
odds: [
1.855,
1.855
],
points: [
"-6.5",
"6.5"
]
}
}
}
],
sites_count: 1
},
{
sport_key: "basketball_nba",
sport_nice: "NBA",
teams: [
"Atlanta Hawks",
"Cleveland Cavaliers"
],
commence_time: 1546130400,
home_team: "Atlanta Hawks",
sites: [
{
site_key: "mybookieag",
site_nice: "MyBookie.ag",
last_update: 1546136064,
odds: {
spreads: {
odds: [
1.855,
1.855
],
points: [
"-7.5",
"7.5"
]
}
}
}
],
sites_count: 1
},
{
sport_key: "basketball_nba",
sport_nice: "NBA",
teams: [
"Boston Celtics",
"Memphis Grizzlies"
],
commence_time: 1546132200,
home_team: "Memphis Grizzlies",
sites: [
{
site_key: "mybookieag",
site_nice: "MyBookie.ag",
last_update: 1546136064,
odds: {
spreads: {
odds: [
1.901,
1.855
],
points: [
"10.5",
"-10.5"
]
}    
}
},
{
site_key: "bovada",
site_nice: "Bovada",
last_update: 1546136030,
odds: {
spreads: {
odds: [
1.952381,
1.87
],
points: [
"6.5",
"-6.5"
]
}

You can use foreach:

$data = json_decode($jsonCont, true);

foreach($data['data'] as $line){
    $tip = $line['commence_time'];
    $teama = $line['teams'][0];
    $linea = $line['sites'][0]['odds']['spreads']['points'][0];
    $teamb = $line['teams'][1];
    $lineb = $line['sites'][0]['odds']['spreads']['points'][1];

    $sql = "INSERT INTO nbagames(tip, teama, linea, teamb, lineb)
    VALUES('$tip', '$teama', '$linea', '$teamb', '$lineb')";
    if(!mysqli_query($conn,$sql))
    {
        die('Error : ' . mysql_error());
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM