2016-08-18 75 views
4

问题是我的总行数的输出结果没有显示出来。 我的实际代码:在pdo中获取多个选择

<? 
$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

'$ stmt-> total'不存在。 – hjpotter92

回答

3

您可以使用->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; 
    } 
} 
?> 
+0

工作过,谢谢! – John