问题是我的总行数没有显示输出结果。我的实际代码:
<?
$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;?>
}
?>
Run Code Online (Sandbox Code Playgroud)
为何无法正常工作,我错过了什么吗?
您可以使用->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;
}
}
?>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2056 次 |
| 最近记录: |