尝试获取 属性 'num_rows' 的非目标代码与所有源代码类似,但它不起作用。 PHP 7

Trying to get property 'num_rows' of non-object code is similar to all the sources, but it does not work. PHP 7

<html>
<table>
<tr>
 <th>Id</th>
 <th>Username</th>
 <th>Result</th>
 </tr>
    <?php
        $conn = new mysqli("localhost", "root", "", "roboit");
        // Check connection
        if ($conn->connect_error) 
        {
        die("Connection failed: " . $conn->connect_error);
        }
        $sql = "SELECT id, username, result FROM result";
        $result = $conn->query($sql);
        if ($result->num_rows > 0) 
        {
        // output data of each row
        while($row = $result->fetch_assoc()) {
        echo "<tr><td>" . $row["id"]. "</td><td>" . $row["username"] . "</td><td>". $row["result"]. " 
        </td></tr>";
        }
        echo "</table>";
        } else { echo "0 results"; }
        $conn->close();
        ?>
  </table>
</html>

这是代码。我从不同的来源(从印度程序员开始到 W3Schools 结束)检查过,它们非常相似甚至完全相同。但它告诉我 $result 是一个非对象。怎么办?

我也找不到类似问题的答案。 (这里我是说)

<table class="scoretable;">
 <tr>
 <th>Id</th>
 <th>Username</th>
 <th>Result</th>
 </tr>
 <?php
 $conn = new mysqli("localhost", "root", "", "roboit");
 // Check connection
 if ($conn->connect_error) {
 die("Connection failed: " . $conn->connect_error);
 }
 $sql = "SELECT `id`, `login`, `score` FROM `result`";
 $result = $conn->query($sql);
 $rows = mysqli_num_rows($result);
 if ($rows > 0) {
 // output data of each row
 while($rows = $result->fetch_assoc()) {
 echo "<tr><td>" . $rows['id']. "</td><td>" . $rows['login'] . "</td><td>". 
 $rows['score']. "</td></tr>";
 }
 echo "</table>";
 } else { echo "0 results"; }
 $conn->close();
 ?>
</table>

好吧,也许没人在乎,但答案就在这里。