php - Notice: Trying to get property of non-object at if ( $result->num_rows >0) {

<?php
            include "conn.php";
            include "session.php";
        // Define $username and $password
            $n1=$_POST['Name'] ;
            $sql = "SELECT *FROM myDB.Mynew WHERE Fname like '%".$n1."%' ";
            $result = $conn->query($sql);
            if ( $result->num_rows >0) {
                    // output data of each row
                    echo '<table>';
                    echo '<tr>';
                    echo'<th>First Name</th>
                        <th>Last Name</th>
                        <th>Email</th>
                        <th>Password</th>';
                    echo'<tr>';
                    while($row = $result->fetch_assoc())  {
                        echo '<tr>';
                        echo '  <td>' . $row["firstname"] . '</td>';
                        echo '  <td>' . $row["lastname"] . '</td>';
                        echo '  <td>' . $row["email"] . '</td>';
                        echo '  <td>' . $row["password"] . '</td>';
                        echo '  </tr> ';                
                    }
                echo'</table>';
            }
            else {
                echo "<br> No Record Found to display";
            }
?>

When I run this code I get the following notice:

Notice: Trying to get property of non-object at line 14 which is "if ( $result->num_rows >0) {"

Please help me.

2 Answers

  1. Edgar- Reply

    2019-11-16

    You need to add a space after * in your sql query ...

    Try the following code. It also checks if your sql query is returning results or not:

                include "conn.php";
                include "session.php";
                // Define $username and $password
                $n1=$_POST['Name'] ;
                $sql = "SELECT * FROM myDB.Mynew WHERE Fname like '%".$n1."%' ";
                $result = $conn->query($sql);
                if($result){
                if ( $result->num_rows >0) {
                        // output data of each row
                        echo '<table>';
                        echo '<tr>';
                        echo'<th>First Name</th>
                            <th>Last Name</th>
                            <th>Email</th>
                            <th>Password</th>';
                        echo'<tr>';
                        while($row = $result->fetch_assoc())  {
                            echo '<tr>';
                            echo '  <td>' . $row["firstname"] . '</td>';
                            echo '  <td>' . $row["lastname"] . '</td>';
                            echo '  <td>' . $row["email"] . '</td>';
                            echo '  <td>' . $row["password"] . '</td>';
                            echo '  </tr> ';                
                        }
                    echo'</table>';
                }
                else {
                    echo "<br> No Record Found to display";
                }
                }else {
                    echo "<br> Database error.";
                }
    
  2. Edward- Reply

    2019-11-16

    This error occurs when the structure of the table is updated. ie, either the table column name is changed or some columns are removed from that particular table which is currently used for retrieve data. I think in your table Mynew any of the columns firstname,lastname,email,password are deleted or it's names are updayed

Leave a Reply

Your email address will not be published. Required fields are marked *

You can use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>