我在对象上调用函数时为什么在非对象上出现此函数调用错误?

错误:

Fatal error: Call to a member function
bind_param() on a non-object in
/var/www/web55/web/pdftest/events.php
on line 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创建一个准备好的语句.

为什么会出现此错误?

解决方法:

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);
}
上一篇:PHP-选择“全部”,同时在MySQL中选择“单独”


下一篇:如何从PHP MySQL SQL注入易受攻击的查询转到MySQLi不易受攻击的查询