当我在对象上调用函数时,为什么会在非对象上出现此函数调用错误?
错误:
致命错误:调用成员函数 中的非对象上的bind_param() /var/www/web55/web/pdftest/events.php 第76行
代码:
public function countDaysWithoutEvents(){
$sql = "SELECT 7 - COUNT(*) AS NumDaysWithoutEvents
FROM
(SELECT d.date
FROM cali_events e
LEFT JOIN cali_dates d
ON e.event_id = d.event_id
WHERE YEARWEEK(d.date) = YEARWEEK(CURRENT_DATE())
AND c.category_id = ?
GROUP BY DAY(d.date)
) AS UniqueDates";
$stmt = $this->link->prepare($sql);
$stmt->bind_param('i', $this->locationID);
$stmt->execute();
$stmt->bind_result($count);
$stmt->close();
return $count;
}
$this->link->prepare($sql)
为 MySQLi 创建一条准备好的语句。
为什么我会收到此错误?
Error:
Fatal error: Call to a member function
bind_param() on a non-object in
/var/www/web55/web/pdftest/events.php
on line 76
Code:
public function countDaysWithoutEvents(){
$sql = "SELECT 7 - COUNT(*) AS NumDaysWithoutEvents
FROM
(SELECT d.date
FROM cali_events e
LEFT JOIN cali_dates d
ON e.event_id = d.event_id
WHERE YEARWEEK(d.date) = YEARWEEK(CURRENT_DATE())
AND c.category_id = ?
GROUP BY DAY(d.date)
) AS UniqueDates";
$stmt = $this->link->prepare($sql);
$stmt->bind_param('i', $this->locationID);
$stmt->execute();
$stmt->bind_result($count);
$stmt->close();
return $count;
}
$this->link->prepare($sql)
creates a prepared statement for MySQLi.
Why am I getting this error?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
AND c.category_id = ?
- 您的查询中没有表别名 c。除此之外尝试
AND c.category_id = ?
- there is no table alias c in your query.Besides that try
我认为问题显然出在 prepare 函数上。
该函数可能会失败,在这种情况下 $stmt 将为 FALSE,因此没有 bind_param 方法作为成员。
检查您的查询! 也许您的 SELECT 语句有问题。 在尝试对您认为是由准备函数返回的对象执行任何成员函数之前,还要检查 FALSE。
编辑
我怀疑该语句可能由于子查询而出错:
相反,你为什么不这样写你的查询:
PS我认为你还缺少一个也调用fetch..(参见上面的示例)
I think the problem is obviously with the prepare function..
The function is probably failing, in which case $stmt would be FALSE and hence not have the bind_param method as a member.
Check your query! Maybe there is a problem with your SELECT statement. And also check for FALSE before trying to execute any member function on what you think is an object returned by the prepare function.
EDIT
I would suspect that the statement may be erroring out because of the sub-query:
Instead, why don't you write your query like this:
P.S. I think you also had a missing a call to fetch as well.. (see example above)
$this->link->prepare 该语句不返回对象
所以它给了你错误
$this->link->prepare this statement is not returning the object
so it is giving you the error