因此,我是OOP概念的新手。我决定打电话给我的数据库,但是以面向对象的方式。我收到的错误是:
试图获取非对象的属性“ num_rows”(在第83行*引用第83行)
我理解该错误消息,但是无法找出有关此错误的信息。我尝试了以下链接,但不幸的是,它们都没有进一步帮助我,否则我无法理解它们的含义。
Notice: Trying to get property 'num_rows' of non-object in line 35
Error - Trying to get property 'num_rows' of non-object
Get property num_rows of non-object
Trying to get property 'num_rows' of non-object
这就是我有意重复一个问题的原因,希望我的问题是(尚未)在其他帖子中得到解决的问题。
require 'connection.php';
//class DatabaseQueries handles all the queries required regarding CRUD.
class DatabaseQueries
{
//the SQL string
private $sql;
// The connection -> completegallery
private $conn;
public function __construct($conn)
{
$this->conn = $conn;
}
// function will check whether email already exists or not.
protected function getEmailExistance(string $email)
{
//create the SQL
$this->sql = $this->conn->prepare("SELECT * FROM userinfo WHERE email = ?");
//bind the parameter to it (preventing sql injection this way)
$this->sql->bind_param('s', $email);
// $result = the execution of the SQL.
$result = $this->sql->execute();
$resultCheck = $result->num_rows; //* line 83
var_dump($result); // returns boolean true.
var_dump($this->sql); // returns a mysqli stmt object
//check whether $resultCheck > 0
//if yes, that would mean the userName already exists.
if (!empty($result) && $resultCheck > 0)
{
exit('should show something');
} else
{
exit('always fails here');
}
}
} // ending class DatabaseQueries
我如何称呼DatabaseQueries类:
class Base extends DatabaseQueries
{
private $email;
private $userName;
private $name;
private $lastName;
private $pwd;
private $pwdConfirm;
// here is the code where I check and assign the user input to the variable $email etc.
//this method is for test purposes only and will be removed after the website is 'done'.
public function getEverything()
{
//link to check whether email is being used or not
$this->getEmailExistance($this->email);
}
}
我如何调用对象等。
$userInformation = new base($conn);
$userInformation->setEmail($_POST['registerEmail']);
//some other info, but not relevant to the problem.
我已经检查过我是否拼错了什么,但是事实并非如此。在connection.php中的连接也已声明为正确。
答案 0 :(得分:0)
如评论中所述,execute()
不返回结果,您必须fetch()
。这是应该允许您执行要求的代码。
// function will check whether email already exists or not.
protected function getEmailExistance(string $email)
{
//create the SQL
$this->sql = $this->conn->prepare("SELECT * FROM userinfo WHERE email = ?");
//bind the parameter to it (preventing sql injection this way)
$this->sql->bind_param('s', $email);
// $result = the execution of the SQL.
$this->sql->execute(); <---- No need for variable here, its boolean
$result = $this->sql->store_result(); <---- The result
$num_rows = $this->sql->num_rows; <---- This will contain your row count
if (!empty($result))
{
// fetch your results here
} else
{
exit('always fails here');
}
}