我开始使用 mysqli,但我对它的工作原理有点困惑。
所以我有一个功能:
function verify_payment_date()
{
$today = date("Y-m-d");
$email = $_SESSION['email'];
$result = $this->conn->query("SELECT * FROM user WHERE email=$email");
while ($row = $result->fetch_object())
{
$next_payment_date = $row['next_payment_date'];
}
}
要设置我的连接,我在同一类中执行此操作:
private $conn;
function __construct()
{
$this->conn = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME) or
die('There was a problem connecting to the database.');
}
有人可以帮我一下吗,因为我完全迷路了。我基于这个网站的一些代码: http://www.willfitch.com/mysqli-tutorial.html
我收到的错误是:
fatal error :在第 52 行/home/vhosts/tradingeliteclub.com/subdomains/test/httpdocs/FES/members/classes/Mysql.php 中的非对象上调用成员函数 fetch()
我不知道该去哪里。
感谢您的时间和帮助。
最佳答案
$result
不是 mysql 结果,您的查询失败。尝试一下
$result = $this->conn->query("SELECT * FROM user WHERE email='$email'");
// apostrophes
相反。
$this->conn->query
成功时返回 resource
,失败时返回 false
。您可以通过这种方式避免错误
if($result) while ($row = $result->fetch_object())
{
$next_payment_date = $row['next_payment_date'];
}
关于php - mysqli fetch_object() 抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8593149/