在搜索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;
}
}
答案 0 :(得分:1)
在$rows = $this->query->fetchAll(PDO::FETCH_ASSOC); return count($rows);
后尝试$this->query->execute();
您也可以使用rowCount()
答案 1 :(得分:1)
这取决于为什么你需要计数。
如果您想了解自己拥有的产品数量而不是产品本身,那么您必须运行不同的查询。
如果您想要产品,那么您根本不需要专门的计数方法。