わかりました.. 外部の JSON ページからローカルの MySQL データベースに (xampp 経由で) 情報を取得しようとしています。それは動作していません..これが私のコードです。
JSONページ
{
"id": 219,
"first_name": "Dimitar",
"second_name": "Berbatov",
"season_history": [
["2006/07", 2715, 12, 11, 0, 45, 0, 0, 0, 1, 0, 0, 26, 0, 91, 169],
["2007/08", 2989, 15, 11, 0, 56, 0, 0, 0, 3, 0, 0, 18, 0, 95, 177],
["2008/09", 2564, 9, 10, 0, 20, 0, 0, 0, 2, 0, 0, 14, 0, 95, 138],
["2009/10", 2094, 12, 6, 0, 13, 0, 0, 0, 1, 0, 0, 8, 0, 92, 130],
["2010/11", 2208, 21, 4, 8, 28, 0, 0, 0, 1, 0, 0, 26, 0, 92, 176],
["2011/12", 521, 7, 0, 2, 6, 0, 0, 0, 0, 0, 0, 5, 146, 89, 49]
}
mycode.php
$con=mysqli_connect("SERVER","USERNAME","PASSWORD", "DATABASE") or die("Nope.");
//GETS THE PAGE WITH THE PLAYER DETAILS
$i = 219; //will loop this for every user later
$str = file_get_contents('http://fantasy.premierleague.com/web/api/elements/'.$i.'/');
$jsonarray = json_decode($str, true);
//PLAYER DETAILS
$id = $jsonarray['id'];
$firstname = addslashes($jsonarray['first_name']);
$secondname = addslashes($jsonarray['second_name']);
//Count how many seasons
$numberOfSeasons = count($jsonarray['season_history']);
//For Each Season
for($SeasonCount = 0; $SeasonCount < $numberOfSeasons; $SeasonCount++) {
$whichSeason = $jsonarray['season_history'][$SeasonCount][0];
$SeasonMins = $jsonarray['season_history'][$SeasonCount][1];
$SeasonGoalsScored = $jsonarray['season_history'][$SeasonCount][2];
$SeasonAssists = $jsonarray['season_history'][$SeasonCount][3];
mysqli_query
($con, "
SELECT EXISTS (SELECT 1 FROM myTable WHERE id='$id' AND whichSeason='$whichSeason')
UPDATE myTable SET
id = '$id',
whichSeason = '$whichSeason',
SeasonMins = '$SeasonMins',
SeasonGoalsScored = '$SeasonGoalsScored',
SeasonAssists = '$SeasonAssists'
ELSE
INSERT INTO myTable (id, whichSeason, SeasonMins, SeasonGoalsScored, SeasonAssists)
VALUES ('$id', '$whichSeason', '$SeasonMins', '$SeasonGoalsScored', '$SeasonAssists')
")
or die (mysqli_error($con));
}
ノート
- 主キーはありません。どのエントリを 1 つとして使用できるかわかりません。
何をすべきか
myTable
エントリがすでに存在するかどうかを検索します。- 一致しない場合は追加します。
- 既にある場合は、更新します。
さらに情報が必要な場合はお知らせください。PDO については何も知りませんが、このデータベースはローカルであり、ページはブラウザーでのみ実行されるため、必要ありませんよね?
ご協力いただきありがとうございます。