gpt4 book ai didi

php - fatal error : Call to a member function fetch_assoc() on string

转载 作者:行者123 更新时间:2023-12-04 13:51:49 31 4
gpt4 key购买 nike

我尝试执行查询并遍历结果。 echo "<h1>" . $row["SummonerId"]. "</h1>";工作,并在页面上打印出来。但不知何故在打印结果后发生了这个错误:

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();

最佳答案

$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在循环内不匹配

关于php - fatal error : Call to a member function fetch_assoc() on string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36849960/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com