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
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
.
I won't give you the solution because I think you need to develop some basic troubleshooting skills.
$result
contain? Check with var_dump . You aren't checking for errors in your query. If an error occured, $result
will be false, which is exactly the case. You have a syntax error in your query.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.