2011-08-19 46 views
0

在下面試圖讓總行進行查詢,如rowCount時不使用MySQL的功能,有什麼解決辦法饒了我吧實驗的一個小時:PDO行計數輸出

foreach($DBH->query($sql) as $row){ 

//want the total $row count for output: 


      echo '<tr><td align="left">' . $row['name'] . '</td><td align="left">' . $row['dr'] . '</td></tr>'; 

      } 

我的目標是顯示輸出在這裏:

// Table header. 
    echo <<<EOT 
     <table class="app_table" summary="The Current roster of users registered in our application."> 
     <caption>The Current Registered Users:'<?php echo("count"); ?>'//Display total count 
<span id="summaryView"></span></caption> 
     <thead> 
     <tr> 

---------完整的腳本----------

<?php 


$page_title = 'View the Current Users'; 
include ('./inc/header.php'); 

// Page header: 
echo '<h1>Registered Users</h1>'; 

require_once ('../pdo_connect.php'); // Connect to the db. 

// Make the query: 

$sql = "SELECT CONCAT(last_name, ', ', first_name) AS name, DATE_FORMAT(registration_date, '%M %d, %Y') AS dr FROM users ORDER BY registration_date ASC"; 

    // Table header. 
    echo <<<EOT 
     <table class="app_table" summary="The Current roster of users registered in our application."> 
     <caption>The Current Registered Users:'<?php echo("12"); ?>' <span id="summaryView"></span></caption> 
     <thead> 
     <tr> 

     <th class="Corner">Name</th> 
     <th>Date Registred</th> 
     </tr> 
     </thead> 
     <tbody> 
EOT; 

try { 
    foreach($DBH->query($sql) as $row){ 


     echo '<tr><td align="left">' . $row['name'] . '</td><td align="left">' . $row['dr'] . '</td></tr>'; 

     } 

    echo '</tbody></table>'; // Close the table. 

    // $DBH = null;//using a persistant connect see php.net 
} catch (PDOException $e){ 

    echo'<p class="error">' . "Error!: " . $e->getMessage() . '</p>'; 
    die(); 
} 


include ('./inc/footer.php'); 
?> 

回答

1

試試這個:

$DBH->query($sql); 
$foundRows = $DBH->query("SELECT FOUND_ROWS()")->fetchColumn();