2016-04-25 7 views
2

クエリを実行して結果を反復しようとしました。 echo "<h1>" . $row["SummonerId"]. "</h1>";が動作し、ページに印刷されます。しかし、どういうわけかこのエラーは、印刷結果の後に発生しました。致命的なエラー:文字列のメンバ関数fetch_assoc()を呼び出す

Fatal error: Call to a member function fetch_assoc() on string

このような多くのエラーが発生しました。しかし、それらはon a non-objectであり、on stringではありません。そのエラーは何ですか?

はここに私のコードです:

$servername = "localhost:3307"; 
$username = "root"; 
$password = ""; 
$dbname = "st-datacollector"; 

$conn = new mysqli($servername, $username, $password, $dbname); 

$sql = "SELECT * FROM summoner"; 
$result = $conn->query($sql); 

if ($result->num_rows > 0) { 
    while($row = $result->fetch_assoc()) { 
     echo "<h1>" . $row["SummonerId"]. "</h1>"; 
     $summonerId = $row["SummonerId"]; 
     $url = "https://euw.api.pvp.net/api/lol/euw/v1.3/game/by-summoner/" . $summonerId . "/recent?api_key=" . $api_key; 

     $result = file_get_contents($url); 
     $resultJSON = json_decode($result); 


     foreach($resultJSON_decoded->games as $game){ 
      echo $game->gameMode." ".$game->gameType." ".$game->subType; 
      echo "<br>"; 
     }   
    } 
} else { 
    echo "0 results"; 
} 

$conn->close(); 
+0

文字列は非オブジェクトです。 – AbraCadaver

+0

あなたの質問のコードでは、 '$ row [" SummonerId "]'が出力された後に 'fetch_assoc()'は使用されず、 '$ result'は文字列にすることはできませんので、 。含まれていないコードがもうありますか? –

+0

ええ、私はこれがどのようにこのエラーを引き起こしているのか見ていません。 – VikingBlooded

答えて

4

$result変数は、MySQLのクエリを通してループに使用されている、あなたのループ私が気づい

$servername = "localhost:3307"; 
$username = "root"; 
$password = ""; 
$dbname = "st-datacollector"; 

$conn = new mysqli($servername, $username, $password, $dbname); 

$sql = "SELECT * FROM summoner"; 
$result = $conn->query($sql); 

if ($result->num_rows > 0) { 
    while($row = $result->fetch_assoc()) { 
     echo "<h1>" . $row["SummonerId"]. "</h1>"; 
     $summonerId = $row["SummonerId"]; 
     $url = "https://euw.api.pvp.net/api/lol/euw/v1.3/game/by-summoner/" . $summonerId . "/recent?api_key=" . $api_key; 

     $fcontents = file_get_contents($url); 
     $resultJSON = json_decode($fcontents); 

     foreach($resultJSON_decoded->games as $game){ 
      echo $game->gameMode." ".$game->gameType." ".$game->subType; 
      echo "<br>"; 
     }   
    } 
} else { 
    echo "0 results"; 
} 

$conn->close(); 

一つより小さい事の内側に別の変数名を使用する必要があります。 $resultJSON$resultJSON_decodedはループ内で一致しません

関連する問題