문제

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

http://www.php.net/manual/en/pdo.query.php

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top