简体   繁体   English

试图获取非对象错误的属性

[英]Trying to get property of non-object Error

I need to get the number of rows returned of a certain query but it kept me this error 我需要获取某个查询返回的行数,但它使我出现此错误

Trying to get property of non-object in C:\\xampp\\htdocs\\sample\\includes\\CheckUsername.php on line 18 尝试在第18行的C:\\ xampp \\ htdocs \\ samples \\ includes \\ CheckUsername.php中获取非对象的属性

Here's my code: 这是我的代码:

 <?php
        class Db_CheckUsername{
            protected $_conn;
            protected $_username;
            protected $_errors = array();

            public function __construct($username,$conn){
                $this->_username = $username;
                $this->_conn = $conn;
            }

            public function isUsernameAvailable(){
                $sql = "SELECT ";
                $sql .= "FROM accounts ";
                $sql .= "WHERE username = {$this->_username}";

                $result = $this->_conn->query($sql);
                $numRows = $result->num_rows;

                if($numRows == 0){
                    return true;
                }else{
                    return false;
                }
            }
        }
    ?>

How do I resolve this? 我该如何解决?

SELECT FROM accounts

is probably invalid. 可能无效。 You probably want SELECT * FROM or SELECT 1 FROM . 您可能需要SELECT * FROMSELECT 1 FROM

I won't give you the solution because I think you need to develop some basic troubleshooting skills. 我不会提供解决方案,因为我认为您需要发展一些基本的故障排除技能。

  • Inspect your data. 检查您的数据。 What data does $result contain? $result包含哪些数据? Check with var_dump . var_dump检查。
  • Is your query correct? 您的查询正确吗? Print the query to see it. 打印查询以查看它。 Copy and paste the exact query into an SQL prompt to see if it's working. 将确切的查询复制并粘贴到SQL提示中,以查看其是否有效。

You aren't checking for errors in your query. 您没有在查询中检查错误。 If an error occured, $result will be false, which is exactly the case. 如果发生错误, $result将为false,这是正确的情况。 You have a syntax error in your query. 您的查询中有语法错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM