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();
字符串是非对象。 – AbraCadaver
在你的问题的代码中,'$ row [“SummonerId”]'输出后''fetch_assoc()'不被使用,'$ result'不能是一个字符串,所以它似乎不可能产生这个特定的错误。是否有更多的代码不包括在内? –
是的,我不知道这可能是如何产生这个错误。 – VikingBlooded