gpt4 book ai didi

PHP 不会解析完整的 JSON 文件以获得所有结果

转载 作者:行者123 更新时间:2023-11-29 11:57:44 26 4
gpt4 key购买 nike

php 脚本可能只会解析 1 到 2 个结果,但不会解析完整的文件。 JSON 文件包含大约 200 个结果。

这是 php 文件

$url = 'http://ironcentral.org/carnivore/api/nation_data/iron_nations';
$content = file_get_contents($url);
$json = json_decode($content, true);
$con = mysqli_connect("localhost", "user", "pass", "iron");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

foreach($json as $item) {
$sql = "INSERT INTO nations (nationid, ruler, nation, gov, religion, tech, infra, land, mode, resource1, resource2, strength, defcon, soldiers, tanks, cruise, nukes, slots) VALUES ('$item[nationid]','$item[ruler]','$item[nation]','$item[gov]','$item[religion]','$item[tech]','$item[infra]','$item[land]','$item[mode]','$item[resource1]','$item[resource2]','$item[strength]','$item[defcon]','$item[soldiers]','$item[tanks]','$item[cruise]','$item[nukes]','$item[slots]')";

}
mysqli_query($con, $sql) or die(mysqli_error($con));
mysqli_close($con);

最佳答案

您对 mysqli_query() 的调用位于循环之外,因此它仅在循环执行完毕后运行。将其移至循环内部:

foreach($json as $item) {
$sql = "INSERT INTO nations (nationid, ruler, nation, gov, religion, tech, infra, land, mode, resource1, resource2, strength, defcon, soldiers, tanks, cruise, nukes, slots) VALUES ('$item[nationid]','$item[ruler]','$item[nation]','$item[gov]','$item[religion]','$item[tech]','$item[infra]','$item[land]','$item[mode]','$item[resource1]','$item[resource2]','$item[strength]','$item[defcon]','$item[soldiers]','$item[tanks]','$item[cruise]','$item[nukes]','$item[slots]')";
mysqli_query($con, $sql) or die(mysqli_error($con));
}

您还可以将其设置为一个查询并在一个语句中执行所有操作:

$sql = "INSERT INTO nations (nationid, ruler, nation, gov, religion, tech, infra, land, mode, resource1, resource2, strength, defcon, soldiers, tanks, cruise, nukes, slots) VALUES ";
foreach($json as $item) {
$sql .= "('$item[nationid]','$item[ruler]','$item[nation]','$item[gov]','$item[religion]','$item[tech]','$item[infra]','$item[land]','$item[mode]','$item[resource1]','$item[resource2]','$item[strength]','$item[defcon]','$item[soldiers]','$item[tanks]','$item[cruise]','$item[nukes]','$item[slots]'), ";
}
$sql = rtrim($sql, ',');
mysqli_query($con, $sql) or die(mysqli_error($con));

$sql = "INSERT INTO nations (nationid, ruler, nation, gov, religion, tech, infra, land, mode, resource1, resource2, strength, defcon, soldiers, tanks, cruise, nukes, slots) VALUES ";
$inserts = [];
foreach($json as $item) {
$inserts[] = "('$item[nationid]','$item[ruler]','$item[nation]','$item[gov]','$item[religion]','$item[tech]','$item[infra]','$item[land]','$item[mode]','$item[resource1]','$item[resource2]','$item[strength]','$item[defcon]','$item[soldiers]','$item[tanks]','$item[cruise]','$item[nukes]','$item[slots]') ";
}
$sql .= implode(',', $inserts);
mysqli_query($con, $sql) or die(mysqli_error($con));

关于PHP 不会解析完整的 JSON 文件以获得所有结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32938629/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com