问题是我的总行数没有显示的输出结果。 我的实际代码:
<?
$stmt = $dbh->prepare("SELECT SQL_CALC_FOUND_ROWS * FROM table WHERE mark=0 ORDER BY id ASC LIMIT 0, 15; SELECT FOUND_ROWS() as total;");
$stmt->execute();
if ($stmt->rowCount() > 0)
{
?>
<span>Number or rows: <? echo $stmt->total; ?></span>
<?
while($result = $stmt->fetch(PDO::FETCH_OBJ))
{
<?=$result->id;?>
<?=$result->user;?>
}
?>
为什么不工作的原因是什么?我错过了什么吗?
答案 0 :(得分:5)
您可以使用->nextRowset()
访问下一个数据(如果是计数)。首先获取(获取)所需的行。然后得到计数:
<?php
$stmt = $dbh->prepare("
SELECT SQL_CALC_FOUND_ROWS * FROM table WHERE mark=0 ORDER BY id ASC LIMIT 0, 15;
SELECT FOUND_ROWS() as total;
");
$stmt->execute();
$values = $stmt->fetchAll(PDO::FETCH_OBJ);
$stmt->nextRowset(); // shift to the total
$count = $stmt->fetchColumn(); // get the total
?>
<span>Number of rows: <? echo $count; ?></span>
<?php
if($count > 0) {
foreach($values as $v) {
// iterate fetched rows
echo $v->id;
}
}
?>