2013-07-11 115 views
0

好吧..我想從外部JSON頁面信息到我的本地MySQL數據庫(通過XAMPP)。它不工作..這是我的代碼。難度從JSON頁面服用信息,並把到MySQL

JSON PAGE

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

} 

注意

  • 沒有主鍵。我不知道什麼條目可以作爲一個。

它應該做的

  • 搜索myTable以查看是否有條目已經存在。
  • 如果不匹配,則添加它。
  • 如果它已經存在,請更新它。

請讓我知道如果你需要任何更多的信息。我對PDO一無所知,但由於這個數據庫是本地的,而且頁面只在我的瀏覽器上運行,我不需要它嗎?

感謝您提供任何幫助。

+0

是您的查詢工作 – DevZer0

+0

號我有這使數據更簡單的查詢,但在每次運行時,它只是複製了數據,而不是更新它。 – Cully

+0

@ DevZer0這個工作 - 我不得不爲:mysqli_query($ CON,「INSERT INTO mytable的(ID,whichSeason,SeasonMins,SeasonGoalsScored,SeasonAssists)VALUES( '的$ id', '$ whichSeason', '$ SeasonMins',「$ 「SeasonGoalsScored','$ SeasonAssists')」)或死(mysqli_error($ con)); – Cully

回答

1

既然你正在使用MySQL,你應該能夠使用和replace into查詢。

首先要在ID添加主鍵和whichSeason

ALTER TABLE myTable add primary key (id, whichSeason); 

然後你就可以發出更換成查詢。

REPLACE INTO myTable 
    (id, whichSeason, SeasonMins, SeasonGoalsScored, SeasonAssists) 
VALUES 
    ('$id', '$whichSeason', '$SeasonMins', '$SeasonGoalsScored', '$SeasonAssists'); 
+0

感謝這個..它完美地工作。我的互聯網昨晚切斷了,所以我無法排序,但現在它完美運行。祝賀10k :) – Cully

+0

@非常感謝你。 :) – Orangepill

相關問題