Php 致命错误:对字符串调用成员函数fetch_assoc()

Php 致命错误:对字符串调用成员函数fetch_assoc(),php,mysqli,Php,Mysqli,我尝试执行一个查询并迭代结果。echo''$第[“传票ID”]行。"";工作并在页面上打印出来。但不知何故,这个错误发生在打印结果之后: 致命错误:对字符串调用成员函数fetch_assoc() 我看到许多类似的错误。但它们是非对象上的,而不是字符串上的。那是什么错误 这是我的密码: $servername = "localhost:3307"; $username = "root"; $password = ""; $dbname = "st-datacollector"; $conn =

我尝试执行一个查询并迭代结果。
echo''$第[“传票ID”]行。"";工作并在页面上打印出来。但不知何故,这个错误发生在打印结果之后:

致命错误:对字符串调用成员函数fetch_assoc()

我看到许多类似的错误。但它们是非对象上的
,而不是字符串上的
。那是什么错误

这是我的密码:

$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();
$servername=“localhost:3307”;
$username=“root”;
$password=“”;
$dbname=“st datacollector”;
$conn=newmysqli($servername、$username、$password、$dbname);
$sql=“从召唤者中选择*”;
$result=$conn->query($sql);
如果($result->num_rows>0){
而($row=$result->fetch_assoc()){
回显“$行[“召唤ID”]”;
$COUNDERID=$row[“COUNDERID”];
$url=”https://euw.api.pvp.net/api/lol/euw/v1.3/game/by-summoner/“$courderid.”/recent?api_key=“.$api_key;
$result=file\u get\u contents($url);
$resultJSON=json_decode($result);
foreach($resultJSON_解码->游戏为$game){
echo$游戏->游戏模式。“$游戏->游戏类型。”“$游戏->子类型;
回声“
”; } } }否则{ 回显“0结果”; } $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();
$servername=“localhost:3307”;
$username=“root”;
$password=“”;
$dbname=“st datacollector”;
$conn=newmysqli($servername、$username、$password、$dbname);
$sql=“从召唤者中选择*”;
$result=$conn->query($sql);
如果($result->num_rows>0){
而($row=$result->fetch_assoc()){
回显“$行[“召唤ID”]”;
$COUNDERID=$row[“COUNDERID”];
$url=”https://euw.api.pvp.net/api/lol/euw/v1.3/game/by-summoner/“$courderid.”/recent?api_key=“.$api_key;
$fcontents=file\u get\u contents($url);
$resultJSON=json_decode($fcontents);
foreach($resultJSON_解码->游戏为$game){
echo$游戏->游戏模式。“$游戏->游戏类型。”“$游戏->子类型;
回声“
”; } } }否则{ 回显“0结果”; } $conn->close();

还有一件事我注意到了
$resultJSON
$resultJSON_decoded
在循环内部不匹配

字符串是非对象。在您问题中的代码中,
fetch_assoc()
在输出
$row[“召唤ID”]
后不使用,并且
$result
不能是字符串,因此它似乎不可能产生此特定错误。有更多的代码没有包括在内吗?是的,我不知道这是怎么产生这个错误的。这个错误消息与这个代码一起出现<代码>$mysqli
未分配。如果
$result
是一个字符串,则
$result->fetch_assoc()
将不会执行,因为它不会无错误地通过
$result->num_rows