【发布时间】:2019-08-28 16:27:05
【问题描述】:
所以我是 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 中的连接也被声明为正确。
【问题讨论】:
-
这确实是重复的,但很难找到。因为此类问题通常被关闭为“由错字引起的题外问题。”。 $result 变量不包含任何接近 mysqli stmt 的内容,并且永远无法拥有 num_rows 属性。因为它总是布尔值