-1
<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>

This is the code. I've checked it from different sources (starting from Indian programmers and ending by W3Schools) and they are very similar or even absolutely equal. But it tells me that $result is a non-object. What to do?

I couldn't find the answer on the similar questions too. (here I mean)

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • You are getting this cryptic error, because you have a typo in SQL and you have mysqli error reporting switched off. Switch it on and you will see the proper error. – Dharman Apr 12 '20 at 14:55

1 Answers1

-1
<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>

Ok, maybe no one cares but here is the answer.