How do I solve "Trying to get property 'num_rows' of non-object"?

1

The truth is I'm starting on this php and I have no idea why this error, I try to display the values of my database in a table and I get this

Notice: Trying to get property 'num_rows' of non-object in C: \ xampp \ htdocs \ Panel \ index.php on line 36

<?php
    $conn = mysqli_connect("localhost", "root", "");
      // Check connection
      if ($conn->connect_error) {
       die("Connection failed: " . $conn->connect_error);
      } 
      $sql = "SELECT * FROM mensajes";
      $result = $conn->query($sql);
      if ($result-> num_rows > 0) {
       while($row = $result->fetch_assoc()) {
        echo "<tr><td>" . $row["id"]. "</td><td>" . $row["nombre"] . "</td><td>"
    . $row["email"]. "</td></tr>";
    }
    echo "</table>";
    } else { echo "0 results"; }
    $conn->close();
?>
    
asked by RicardoCarlos de la Rosa 30.11.2018 в 22:44
source

4 answers

1

I propose this code, which will control at any time any eventuality that occurs, informing adequately of it.

As some have pointed out, it is convenient that you put the name of the database as the last parameter of the connection.

I have established a control to capture possible errors in the query.

Also, I changed the style of the connection, using the object-oriented style throughout the code. In your code there is a mixture of styles, which, although it is not a reason for error, is not recommended because it creates a confusing and difficult code to analyze. The object-oriented style is clearer, more modern and more intuitive, so it is preferred over the procedural style.

In the code, I use a variable $output that gathers the results during the flow. At the end I print that variable with the result I have obtained. By the way, you were not opening the <table> tag before while .

This is my proposal. I hope you find it useful:

<?php
    if ( !$conn = new mysqli("localhost", "root", "", "PON-AQUI-NOMBRE-BASE-DE-DATOS") ){
        $output="Connection failed: " . $conn->connect_error;      
    } else {
        $sql = "SELECT * FROM mensajes";
        if ( $result = $conn->query($sql) ){
            if ($result->num_rows > 0) {
                $output="<table>";
                while($row = $result->fetch_assoc()) {
                    $output.= "<tr><td>" . $row["id"]. "</td><td>" . $row["nombre"] . "</td><td>". $row["email"]. "</td></tr>";
                }
                $output.="</table>";
            } else { 
                $output= "0 results"; 
            }
        } else {
                $output="Error en la consulta: ".$conn->error;
        }
        $conn->close();
    }
    echo $output;
?>
    
answered by 01.12.2018 в 02:48
0

The safest thing is that your query SELECT * FROM mensajes fails so it returns false look here on this link mysqli.query . How are you returning false when you make $result->num_rows you would actually be doing this false->num_rows , so you get the error

    
answered by 30.11.2018 в 22:59
0

Add a condition for the case where the query returns empty:

<?php
    $conn = mysqli_connect("localhost", "root", "");
      // Check connection
      if ($conn->connect_error) {
       die("Connection failed: " . $conn->connect_error);
      } 
      $sql = "SELECT * FROM mensajes";
      $result = $conn->query($sql);
      if ($result && $result->num_rows > 0) { //Agregada condicion
       while($row = $result->fetch_assoc()) {
        echo "<tr><td>" . $row["id"]. "</td><td>" . $row["nombre"] . "</td><td>"
    . $row["email"]. "</td></tr>";
    }
    echo "</table>";
    } else { echo "0 results"; }
    $conn->close();
?>
    
answered by 30.11.2018 в 23:00
0

Look at the Possible thing is that you have bad your connection or the fields of the table, I have tested it in a database of my own and it throws me the elements, question of which revices your code and verifies if the data are correct, in this I see that you do not tell what database your question is going to be made.

in my case it's called * appointments.

$conn = mysqli_connect("localhost", "root", "*******","citas");

In the asterisks it would be the password of the user that uses the manager of the database.

this is my code:

    <?php
    $conn = mysqli_connect("localhost", "root", "*******","citas");
      // Check connection
      if ($conn->connect_error) {
       die("Connection failed: " . $conn->connect_error);
      } 
      $sql = "SELECT * FROM Pacientes";
      $result = $conn->query($sql);
      if ($result-> num_rows > 0) {
       while($row = $result->fetch_assoc()) {
        echo "<tr><td>" . $row["pacIdentificacion"]. "</td><td>" . $row["pacNombres"] . "</td><td>"
    . $row["pacApellidos"]. "</td></tr>";
    }
    echo "</table>";
    } 
    else { 
      echo "0 results"; 
    }
    $conn->close();
?>

I hope it serves you: greetings.

    
answered by 01.12.2018 в 00:11