PHP Fatal Error with PDOStatement fetchColumn()
-
04-06-2021 - |
문제
I can't understand this error, could someone please explain it to me?
PHP Fatal error: Call to a member function fetchColumn() on a non-object in /user/dal.php on line 27
I am trying to get the count of the last query, by using regex to perform a new SELECT COUNT(*)
query on the last query. The problem is $stmt->fetchColumn()
causes a fatal error and then I get Error 500
.
function dbRowsCount($sql) {
global $db;
$regex = '/^SELECT\s+(?:ALL\s+|DISTINCT\s+)?(?:.*?)\s+FROM\s+(.*)$/i';
if (preg_match($regex, $sql, $output) > 0) {
$stmt = $db->query("SELECT COUNT(*) FROM {$output[1]}", PDO::FETCH_NUM);
return $stmt->fetchColumn();
}
return false;
}
What am I doing wrong?
해결책
You should check that your call to $db->query
returns a valid PDO object. Something like this:
$stmt = $db->query(...);
if (! $stmt) {
print_r($db->errorInfo());
}
다른 팁
probably
$db->query("SELECT COUNT(*) FROM {$output[1]}", PDO::FETCH_NUM);
fails so $stmt takes value of false and you can't call on it fetchColumn(); I think there is something wrong with the sql query
제휴하지 않습니다 StackOverflow