注意:尝试在if($ result-> num_rows> 0){获取非对象的属性

时间:2015-08-17 15:48:30

标签: php

<?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";
            }
?>

当我运行此代码时,我收到以下通知:

注意:尝试在第14行获取非对象的属性 “if($ result-&gt; num_rows&gt; 0){”

请帮帮我。

3 个答案:

答案 0 :(得分:2)

更新表的结构时会发生此错误。即,更改表列名称或从当前用于检索数据的特定表中删除某些列。 我想在你的表Mynew中删除任何列名字,姓氏,电子邮件,密码或者它们的名称是更新的

答案 1 :(得分:1)

你需要在sql查询中添加一个空格......

尝试以下代码。它还会检查您的SQL查询是否返回结果:

            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 :(得分:-1)

if ( isset($result->num_rows) && $result->num_rows >0) {

//--------

}