致命错误:调用成员函数 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创建一个准备好的语句。
为什么我收到此错误?
答案 0 :(得分:3)
AND c.category_id = ?
- 您的查询中没有表别名c。
除了尝试
$stmt = $this->link->prepare($sql);
if (!$stmt) {
throw new ErrorException($this->link->error, $this->link->errno);
}
if (!$stmt->bind_param('i', $this->locationID) || !$stmt->execute()) {
throw new ErrorException($stmt->error, $stmt->errno);
}
答案 1 :(得分:1)
我认为问题显然在于准备功能..
该函数可能失败,在这种情况下$ stmt将为FALSE,因此没有bind_param方法作为成员。
来自php mysqli manual:
mysqli_prepare()返回一个语句对象,如果发生错误则返回FALSE。
检查您的查询!也许你的SELECT语句有问题。并且在尝试对您认为是由prepare函数返回的对象执行任何成员函数之前,还要检查FALSE。
if($stmt === FALSE)
die("Prepare failed... ");// Handle Error Here
// Normal flow resumes here
$stmt->bind_param("i","");
的修改
我怀疑由于子查询,该语句可能会出错:
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)
相反,为什么不写这样的查询:
public function countDaysWithoutEvents()
{
$count = FALSE;
$sql = "SELECT COUNT(d.date) ";
$sql .= " FROM cali_events e ";
$sql .= " LEFT JOIN cali_dates d ON e.event_id = d.event_id ";
$sql .= " WHERE YEARWEEK(d.date) = YEARWEEK(CURRENT_DATE()) ";
$sql .= " AND c.category_id = ? ";
$sql .= " GROUP BY DAY(d.date) ";
$stmt = $this->link->prepare($sql);
if($stmt !== FALSE)
{
$stmt->bind_param('i', $this->locationID);
$stmt->execute();
$stmt->bind_result($count);
$stmt->fetch(); // I think you need to do a fetch
// here to get the result data..
$stmt->close();
}else // Or, provide your own error
die("Error preparing Statement"); // handling here
return (7 - $count);
}
P.S。我想你也错过了对 fetch 的调用.. (见上面的例子)
答案 2 :(得分:0)
$ this-> link->准备此语句不返回对象 所以它给你错误