2015-10-04 110 views
0

PHP脚本只会解析,也许1〜2的结果,但不完整的文件。 JSON文件中有大约200个结果。PHP不会完全解析JSON文件的所有结果

这里是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); 

回答

1

您的来电mysqli_query()是你的循环之外,所以只能通过后,循环执行完毕NCE运行。它移动到内环路:

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

你也可以把它只是一个查询,并在一个statemnt执行这一切:

$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)); 
+0

做当选项3 [太阳17年10月4日:42:34.694326 2015] [:错误] [4468 PID] [客户端69.120.197.63:46043] PHP解析错误:语法错误,在第16行的/var/www/html/login/nations.php中出现意外的'$ inserts'(T_VARIABLE) 选项1有效,但后来我得到 你的SQL语法;检查对应于你的MySQL服务器版本的手册正确的语法使用近“我的信仰”,“17,121.50”,“8,999.99”,“1,100.001”,“对战模式”,“小麦”,“铁”,“123,530.4”在线1 – Ryahn

+0

我的不好。我在那里留下了一个错误的逗号。我显然不能测试这个代码,所以有时候会偷偷溜走。 –

+0

选项3不通过,除非通过 '解析错误:语法错误,意外'$插入'(T_VARIABLE)在/var/www/html/login/nations.php在线16' – Ryahn

0

bind_param是另一种选择逃跑。这个答案假设了很多东西: 1. .json中的所有列都按您需要插入的顺序排列。 (必须是真实的,否则你会不得不做一些东西来得到这个工作) 2.所有的列是字符串(八九不离十容易解决。在"ssssssssssssssssss"s都对应到要插入一列。更换任何si如果该列是一个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; 
} 
?>