Friday 12 July 2019

php - Why do I get this function call error on an non-object when I am calling a function on an object?




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?


Answer



AND c.category_id = ? - there is no table alias c in your query.




Besides that try



$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);
}


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...