【发布时间】:2018-10-06 10:38:16
【问题描述】:
大家好
以下代码适用于 Json 脚本的第一部分,但是当我尝试解析整个 Json 脚本时,出现以下错误...
注意:未定义索引:C:\xampp\htdocs\working-scripts-jason\jsontest9.php 第 24 行中的 site_nice
注意:未定义索引:第 25 行 C:\xampp\htdocs\working-scripts-jason\jsontest9.php 中的赔率
工作 PHP 代码如下...解析到 MySQL BD
<?php
$host = "localhost";
$username = "student";
$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) VALUES (?, ?, ?, ?, ?)');
mysqli_stmt_bind_param($st, 'sssss', $sport_nice, $team1, $team2, $commence_time, $home_team);
$filename = 'jsontest9.json';
$json = file_get_contents($filename);
$data = json_decode($json, true);
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'];
mysqli_stmt_execute($st);
}
mysqli_close($con);
?>
以下 PHP 代码不工作...我认为这是因为 Json 文件中的嵌套数组,我无法在“站点”进入数据库后获取数据,经过几天的搜索仍然没有乐趣,我有发现了类似的问题,这些问题帮助我登上了舞台,但停留在这一点上......任何帮助表示赞赏......谢谢......
<?php
$host = "localhost";
$username = "student";
$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, h2h) VALUES (?, ?, ?, ?, ?, ?, ?)');
mysqli_stmt_bind_param($st, 'sssssss', $sport_nice, $team1, $team2, $commence_time, $home_team, $site_nice, $h2h);
$filename = 'jsontest9.json';
$json = file_get_contents($filename);
$data = json_decode($json, true);
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'];
$h2h = $row['sites']['odds']['h2h']['0'];
mysqli_stmt_execute($st);
}
mysqli_close($con);
?>
Json 文件...
[
{
"sport_key": "soccer_epl",
"sport_nice": "EPL",
"teams": [
"Brighton and Hove Albion",
"West Ham United"
],
"commence_time": 1538766000,
"home_team": "Brighton and Hove Albion",
"sites": [
{
"site_key": "unibet",
"site_nice": "Unibet",
"last_update": 1538526493,
"odds": {
"h2h": [
2.55,
2.9,
3.2
]
}
}
],
"sites_count": 9
}
]
【问题讨论】: