0
我有一个分析,并与INSERT OR IGNORE
加上UPDATE
查询进入我的数据库JSON FILE一份准备好的声明。该JSON被分成“守门员”,“向前”和“防守队员”,所以我落得这样做的:展望凝聚与多个foreach循环(PHP,SQLite的)
try {
$db = new PDO('connection stuff');
$db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
} catch (Exception $e) {
echo "Error: Could not connect to database. Please try again later.";
exit;
}
$json = file_get_contents('http://nhlwc.cdnak.neulion.com/fs1/nhl/league/teamroster/SJS/iphone/clubroster.json');
$json = json_decode($json, TRUE);
$goalie = $json['goalie'];
$defensemen = $json['defensemen'];
$forwards = $json['forwards'];
$query = "INSERT OR IGNORE INTO roster2015_2016 ('position','id','weight','height','imageURL','birthplace','age','name','birthdate','number')
VALUES (:position, :id, :weight, :height , :imageURL, :birthplace , :age , :name , :birthdate , :number)";
$prepare = $db->prepare($query);
$query2 = "UPDATE roster2015_2016
SET position= :position, id= :id, weight= :weight, height= :height, imageURL= :imageURL, birthplace= :birthplace, age= :age, name= :name,
birthdate= :birthdate, number= :number
WHERE id= :id";
$prepare2 = $db->prepare($query2);
foreach ($goalie as $pd) {
$a = array (':position' => $position = $pd['position'],
':id' => $id = $pd['id'],
':weight' => $weight = $pd['weight'],
':height' => $height = $pd['height'],
':imageURL' => $imageURL = $pd['imageUrl'],
':birthplace' =>$birthplace = $pd['birthplace'],
':age' => $age = $pd['age'],
':name' => $name = $pd['name'],
':birthdate' => $birthdate = $pd['birthdate'],
':number' => $number = $pd['number']);
$prepare->execute($a);
$prepare2->execute($a);
}
foreach ($defensemen as $pd) {
$a = array (':position' => $position = $pd['position'],
':id' => $id = $pd['id'],
':weight' => $weight = $pd['weight'],
':height' => $height = $pd['height'],
':imageURL' => $imageURL = $pd['imageUrl'],
':birthplace' =>$birthplace = $pd['birthplace'],
':age' => $age = $pd['age'],
':name' => $name = $pd['name'],
':birthdate' => $birthdate = $pd['birthdate'],
':number' => $number = $pd['number']);
$prepare->execute($a);
$prepare2->execute($a);
}
foreach ($forwards as $pd) {
$a = array (':position' => $position = $pd['position'],
':id' => $id = $pd['id'],
':weight' => $weight = $pd['weight'],
':height' => $height = $pd['height'],
':imageURL' => $imageURL = $pd['imageUrl'],
':birthplace' =>$birthplace = $pd['birthplace'],
':age' => $age = $pd['age'],
':name' => $name = $pd['name'],
':birthdate' => $birthdate = $pd['birthdate'],
':number' => $number = $pd['number']);
$prepare->execute($a);
$prepare2->execute($a);
}
正如你所看到的,我做同样的事情,3次在一排(1进攻,防守和守门员)。我试图使用一个&&
运营商,并只做一个foreach
循环,但我想你是不允许这样做的。那次失败的尝试是有人可以将我链接到一个资源,让我快速了解该做什么?我尝试了几次Google和Stack Overflow搜索,但找不到我在找的内容。 Here is a link to my roster output。不要担心滚动到查询的权利,这是工作得很好。
工程。我希望找到一个更简单的解决方案。我从来没有想过将数组追加到数组中。谢谢。我很可能会接受这个答案。 –
请接受答案。 – Ninju
你付出了努力,它的工作,所以我会。我不介意评论(提示)的能力。 –