PHP不会为所有结果解析完整的JSON文件

时间:2015-10-04 21:31:41

标签: php mysql json mysqli sql-insert

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);

2 个答案:

答案 0 :(得分:1)

你对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));

答案 1 :(得分:0)

bind_param是转义的另一种选择。这个答案假设了很多东西:  1. .json中的所有列都按照您需要插入的顺序排列。 (必须是真的,否则你必须做一些事情才能让这个工作)  2.所有列都是字符串(sorta easy fix。s中的"ssssssssssssssssss"都对应于您要插入的列。如果该列替换s i是一个int)

$insertColumns = array("nationid", "ruler", "nation", "gov", "religion", "tech", "infra", "land", "mode", "resource1", "resource2", "strength", "defcon", "soldiers", "tanks", "cruise", "nukes", "slots"); 
$sql = "INSERT INTO skill (" . implode(",",$insertColumns) . ") VALUES (" . implode(",",array_fill(0,count($insertColumns),"?")) . ")";

if($stmt = mysqli_prepare($con,$sql)) {
    foreach($json  as $item) {
        call_user_func_array("mysqli_stmt_bind_param", refValues(array_merge(array($stmt, "ssssssssssssssssss"),array_values($item))));

        mysqli_stmt_execute($stmt);
    }
}

// shameless borrowing http://stackoverflow.com/a/16120923/5051310
function refValues($arr) {
    if (strnatcmp(phpversion(),'5.3') >= 0) //Reference is required for PHP 5.3+
    {
        $refs = array();
        foreach($arr as $key => $value)
            $refs[$key] = &$arr[$key];
        return $refs;
    }
    return $arr;
}
?>