PHP PDO:获取返回的行数

时间:2014-04-11 09:57:35

标签: php pdo

在搜索manual和其他问题后,我找不到找到返回行数的方法。如何修改fetchAll()方法?

class Product {
    var $count;
    public function fetchAll(){
        $this->query = $this->pdo->prepare("SELECT * FROM $table");
        $this->query->setFetchMode( PDO::FETCH_ASSOC );
        $this->query->execute();
    }
    public function next(){
        $this->row = $this->query->fetch();
        if (!is_array($this->row)) return false;
        foreach ($this->row as $key => $val) {
            $this->{$key} = $val;
        }   
    }
    public function getCount(){
        return $this->count;
    }
}

2 个答案:

答案 0 :(得分:1)

$rows = $this->query->fetchAll(PDO::FETCH_ASSOC); return count($rows);后尝试$this->query->execute(); 您也可以使用rowCount()

答案 1 :(得分:1)

这取决于为什么你需要计数。

如果您想了解自己拥有的产品数量而不是产品本身,那么您必须运行不同的查询。

如果您想要产品,那么您根本不需要专门的计数方法。