[英]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 * FROM
或SELECT 1 FROM
。
I won't give you the solution because I think you need to develop some basic troubleshooting skills. 我不会提供解决方案,因为我认为您需要发展一些基本的故障排除技能。
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.