0

I am trying to retrieve data that has been stored into php mysql. When I tried to execute code below, I get an error which is:

Notice: Trying to get property of non-object in /storage/ssd5/459/2549459/public_html/x.php on line 27 0 result

x.php

<?php 
    $conn = mysqli_connect("localhost" , "id2549459_salamnti" , "0000000000" , "id2549459_tutorial");
    if($conn -> connect_error){
        die("my connection faild" . $conn -> connect_error);
    }

    $sql = "SELECT id, military_num , firstname, lastname,image from students";
    $result = $conn-> query($sql);
    if($result -> num_rows > 0){
        while($row = $result-> fetch_assoc()){
            echo "<tr> <td>" . $row["id"] . "</td> <td>" . $row["militry_num"] . "</td> <td>". $row["firstname"]. "</td><td>" . $row["lastname"] . "</td><td>" . $row["image"] . "</td> </tr>";
        }
        echo "</table>";
    }
    else{
        echo "0 result";
    }
    $conn -> close();
?>
pushkin
  • 9,575
  • 15
  • 51
  • 95
  • What line is 27? And the error makes it clear that you are trying to access the property of a class when the variable is not a class. I would guess that something is wrong with your query and `$result` is actually `false` which means that the query failed to run for some reason (this is different than zero rows found in which case you would get back a valid result set with zero rows). – Jonathan Kuhn Jun 14 '18 at 23:27
  • your query fails, for what ever reason, thus $result is false/NULL. – Jeff Jun 14 '18 at 23:29
  • line 27 and 28 if($result -> num_rows > 0){ while($row = $result-> fetch_assoc()){ – salim ahmed Jun 14 '18 at 23:29
  • 5
    Possible duplicate of [Reference - What does this error mean in PHP?](https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – Jeff Jun 14 '18 at 23:30
  • sidenote: you could just search in Stackoverflow for "Trying to get property of non-object" and you'd find plenty of matching answers. – Jeff Jun 14 '18 at 23:32

1 Answers1

1

plenty of reasons...1 1. user on mysql doesnt have rights to access from students 2. columns do not exist in DB

Tommys
  • 121
  • 8