PHP PDO rowCount() 返回 -1

PHP PDO rowCount() returning -1

我需要在 API 中添加分页系统。我的数据库服务器是 ODBC。我的代码如下所示:

public function pagination($query, $maxperpage, $values=array()){
    $query = explode('LIMIT', $query);
    try {
        $statement = $this->db->prepare($query[0]);
        $statement->execute($values);

        $result['row_number'] = $statement->rowCount();
        $result['max_per_page'] = $maxperpage;
        $result['page_nb'] = ceil($result['row_number']/$maxperpage);
        return $result;
    } catch (\PDOException $e) {
        exit($e->getMessage());
    }
}

但这给了我以下信息:

"pagination": {
    "row_number": -1,
    "max_per_page": "1000",
    "page_nb": 0
}

为什么行号不起作用?

查询看起来不错

SELECT lo_sit, lo_dep, lo_wrh, lo_addr, lo_rotcls, lo_asscls, lo_stotyp, lo_extcod, lo_blccod 来自 G2001F.LOCMSTP WHERE LO_DEP=:lodep

而且这些值似乎也插入得很好

Array
(
    [lodep] => DEMO
)

$statement->rowCount() 返回 -1 没有意义。

来自PHP manual

PDOStatement::rowCount() returns the number of rows affected by the last DELETE, INSERT, or UPDATE statement executed by the corresponding PDOStatement object.

If the last SQL statement executed by the associated PDOStatement was a SELECT statement, some databases may return the number of rows returned by that statement. However, this behaviour is not guaranteed for all databases and should not be relied on for portable applications.

rowCount() 不能保证与 SELECT 查询一起使用,这不是错误。如果你想知道你应该使用的行数

SELECT COUNT(*) FROM G2001F.LOCMSTP WHERE LO_DEP=:lodep