PHP将嵌套json插入mySQl
我正在尝试将嵌套json从URL插入mySQL 根据这里的其他问题/答案,我尝试了一些foreach,但似乎无法使其发挥作用。我给了它一个for循环,它正在插入,但是它只插入了一条记录,这是最后一条记录 有3个一级数组元素(formguide、teams、players),其中我只需要formguide的值 从URL返回的JSON示例:PHP将嵌套json插入mySQl,php,mysql,json,Php,Mysql,Json,我正在尝试将嵌套json从URL插入mySQL 根据这里的其他问题/答案,我尝试了一些foreach,但似乎无法使其发挥作用。我给了它一个for循环,它正在插入,但是它只插入了一条记录,这是最后一条记录 有3个一级数组元素(formguide、teams、players),其中我只需要formguide的值 从URL返回的JSON示例: { "formguide": [ { "SUSPENSION": null, "WEEKPOINTS": "7", "TEAMCOD
{
"formguide": [
{
"SUSPENSION": null,
"WEEKPOINTS": "7",
"TEAMCODE": "LIV",
"VALUE": "4.5",
"POINTS": "215",
"PLAYERNAME": "Salah, M",
"TEAMNAME": "Liverpool",
"SIXWEEKPOINTS": "58",
"INJURY": null,
"PLAYERID": "3324",
"POS": "MID"
},
{
"SUSPENSION": null,
"WEEKPOINTS": "8",
"TEAMCODE": "TOT",
"VALUE": "7.0",
"POINTS": "209",
"PLAYERNAME": "Kane, H",
"TEAMNAME": "Tottenham Hotspur",
"SIXWEEKPOINTS": "49",
"INJURY": null,
"PLAYERID": "4002",
"POS": "STR"
},
我的代码:
<?php
$servername = "localhost";
$username = "root";
$password = "pass";
$dbname = "my_db";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
include('simple_html_dom.php');
$url = file_get_contents('https://insertjsonURL.com');
$array = json_decode($url, true);
$length = count($array['formguide']);
//assign array keys to variables for sql insert query
for ($i = 0; $i < $length; $i++) {
$sus = $array['formguide'][$i]['SUSPENSION'];
$wpoints = $array['formguide'][$i]['WEEKPOINTS'];
$tcode = $array['formguide'][$i]['TEAMCODE'];
$val = $array['formguide'][$i]['VALUE'];
$points = $array['formguide'][$i]['POINTS'];
$pname = $array['formguide'][$i]['PLAYERNAME'];
$tname = $array['formguide'][$i]['TEAMNAME'];
$sixwpoints = $array['formguide'][$i]['SIXWEEKPOINTS'];
$injury = $array['formguide'][$i]['INJURY'];
$playerid = $array['formguide'][$i]['PLAYERID'];
$pos = $array['formguide'][$i]['POS'];
}
$sql = "INSERT INTO formguide (suspension, weekpoints, teamcode, value, points, playername, teamname, sixweekpoints, injury, playerid, pos)
VALUES ('$sus', '$wpoints', '$tcode','$val','$points','$pname','$tname','$sixwpoints','$injury','$playerid','$pos')";
//output message if successful or not
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
问题很简单。insert语句超出了
for
语句的范围,这导致insert语句只插入数组的最后一个元素,因此将其修复为:
for ($i = 0; $i < $length; $i++) {
$sus = $array['formguide'][$i]['SUSPENSION'];
$wpoints = $array['formguide'][$i]['WEEKPOINTS'];
$tcode = $array['formguide'][$i]['TEAMCODE'];
$val = $array['formguide'][$i]['VALUE'];
$points = $array['formguide'][$i]['POINTS'];
$pname = $array['formguide'][$i]['PLAYERNAME'];
$tname = $array['formguide'][$i]['TEAMNAME'];
$sixwpoints = $array['formguide'][$i]['SIXWEEKPOINTS'];
$injury = $array['formguide'][$i]['INJURY'];
$playerid = $array['formguide'][$i]['PLAYERID'];
$pos = $array['formguide'][$i]['POS'];
$sql = "INSERT INTO formguide (suspension, weekpoints, teamcode,
value, points, playername, teamname,
sixweekpoints, injury, playerid, pos)
VALUES ('$sus', '$wpoints', '$tcode','$val','$points','$pname','$tname',
'$sixwpoints','$injury','$playerid','$pos')";
//output message if successful or not
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
}
for($i=0;$i<$length;$i++){
$sus=$array['formguide'][$i]['SUSPENSION'];
$wpoints=$array['formguide'][$i]['WEEKPOINTS'];
$tcode=$array['formguide'][$i]['TEAMCODE'];
$val=$array['formguide'][$i]['VALUE'];
$points=$array['formguide'][$i]['points'];
$pname=$array['formguide'][$i]['PLAYERNAME'];
$tname=$array['formguide'][$i]['TEAMNAME'];
$sixwpoints=$array['formguide'][$i]['SIXWEEKPOINTS'];
$injury=$array['formguide'][$i]['injury'];
$playerid=$array['formguide'][$i]['playerid'];
$pos=$array['formguide'][$i]['pos'];
$sql=“插入formguide(暂停、周点、组码、,
价值、积分、玩家名称、团队名称、,
六周积分、受伤、球员ID、位置)
值(“$sus”、“$wpoints”、“$tcode”、“$val”、“$points”、“$pname”、“$tname”,
“$sixwpoints”、“$injury”、“$playerid”、“$pos”);
//如果成功或失败,则输出消息
if(mysqli_查询($conn,$sql)){
echo“新记录创建成功”;
}否则{
echo“Error:”.$sql.
“.mysqli_Error($conn);
}
}
如果我理解正确,它只是插入数组中的最后一个对象,因为只有一个sql语句正在执行,它位于for
语句之后。因此,变量$sus
,$wpoints
等被设置为数组中最后一个对象的值-在您的示例中,它将只添加“凯恩,H”播放器的数据对于每个对象,您需要创建一条SQL语句,以便将其添加到数据库中。要执行此操作,请将
$sql
变量移动到for
语句中,并使用,以便在$sql
变量中为每个元素设置一条sql语句
$sql = "INSERT INTO formguide (suspension, weekpoints /* ... the field names*/) VALUES";
for ($i = 0; $i < $length; $i++) {
$sus = $array['formguide'][$i]['SUSPENSION'];
$wpoints = $array['formguide'][$i]['WEEKPOINTS'];
// The other variables . . .
$sql .= "('$sus', '$wpoints' /* The other variables*/),";
}
然而,我会选择的答案,因为它需要较少的处理资源,但有另一个想法 什么是
var\u转储($array)代码>显示您是否将其添加到您的
的之前?谢谢@Jorge。我尝试了之前的方法,但得到了一个错误:错误:在formguide(暂停、周点、组码、值、点、球员姓名、组名、六周点、受伤、球员ID、pos)值(“”、“”、“”、“”、“”、“”、“”、“”、“”)中插入不正确的整数值:“”对于第1行的“周点”列,请澄清,您是否在for语句中添加了变量赋值?执行此操作时,我得到了相同的结果,即最后一个对象是addedAh,我的if语句在for之外,我想我缺少了}。现在一切都好了。谢谢你的多点提示!如果处理不当,则在运行sql时会中断,因为语句末尾将有一个备用逗号。是,这会导致错误。删除它会打印出我所期望的所有对象,但最后会得到一个我似乎无法理解的错误:您的SQL语法中有一个错误;检查与您的MySQL服务器版本对应的手册,以获得正确的语法,以便在第1行的“(”、“8”、“TOT”、“7.0”、“209”、“Kane、H”、“Tottenham Hotspur”、“49”、“4002”、“STR”)附近使用
$sql = "INSERT INTO formguide (suspension, weekpoints /* ... the field names*/) VALUES";
for ($i = 0; $i < $length; $i++) {
$sus = $array['formguide'][$i]['SUSPENSION'];
$wpoints = $array['formguide'][$i]['WEEKPOINTS'];
// The other variables . . .
$sql .= "('$sus', '$wpoints' /* The other variables*/),";
}
$pos = strrpos($sql, ","); // Last occurrence of ',' in $sql
$sql = substr($sql, 0, $pos);// Remove the last ','