1
<?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) {"

Dharman
  • 30,962
  • 25
  • 85
  • 135
Atif
  • 77
  • 1
  • 2
  • 9
  • This looks procedural, not object-orientated. Shouldn't you be using `mysqli_connect()` ? – SierraOscar Aug 17 '15 at 15:54
  • 2
    As per usual, your query failed and returned boolean FALSE. You failed to check for failure, and now your failure has caused further failures. **NEVER** assume success with DB operations. `if ($result === false) { die(mysqli_error($conn)); }` or whatever. And you are vulnerable to [sql injection attacks](http://bobby-tables.com) – Marc B Aug 17 '15 at 15:57
  • What does `$conn` stands for? What kind of method are you using to connect to DB? – al'ein Aug 17 '15 at 15:58

3 Answers3

2

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.";
            }
Hasan Tareque
  • 1,761
  • 11
  • 21
2

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

Codemaker2015
  • 12,190
  • 6
  • 97
  • 81
-1
if ( isset($result->num_rows) && $result->num_rows >0) {

//--------

}
Sutharshan
  • 21
  • 2
  • 4
    This doesn't solve the problem. This just sweeps it under the carpet. – Dharman Oct 11 '19 at 19:13
  • Please describe in your answer, what was the problem, and how will this snippet solve it, to help others understand this answer – FZs Oct 12 '19 at 11:32