2015-10-04 51 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; 
} 
?>