php - Why doesn't the rowcount and results show at the same time, when i am using setFetchMode(PDO::FETCH_ASSOC) -


well, trying query data , show results , see row_count. below code using:

    <?php     $servername = "localhost";     $username = "root";     $password = "";     $dbname = "db_xx";        try {      $conn = new pdo("mysql:host=$servername;dbname=$dbname", $username, $password);       $sql = 'select * tbl_registration_requests flag_acceptance="0"';       $q = $conn->query($sql);      $q->setfetchmode(pdo::fetch_assoc);        $rows = $q->fetchall();     $num_rows = count($rows);       } catch (pdoexception $pe) {      die("could not connect database $dbname :" . $pe->getmessage());     }     ?> 

and showing in table::

<table id="table_reg_confirm" border="1"  cellpadding="5px" ">          <thead bgcolor="#999999">              <tr>              <th>id</th>              <th>first name</th>              <th>last name</th>              <th>email</th>              <th>phone</th>              <th>address</th>              <th>date of birth</th>              <th>comments</th>                                             </tr>          </thead>           <tbody>               <?php while ($r = $q->fetch()):                  $i=0;              ?>                  <tr>                      <td><?php echo htmlspecialchars($r['id'])?></td>                       <td><input type="text" name="frname".<?php $i; ?> value="<?php echo htmlspecialchars($r['first_name']).$i?> "></td>                      <td><?php echo htmlspecialchars($r['last_name']); ?></td>                      <td><?php echo htmlspecialchars($r['email']); ?></td>                      <td><?php echo htmlspecialchars($r['phone']); ?></td>                      <td><?php echo htmlspecialchars($r['address']); ?></td>                      <td><?php echo htmlspecialchars($r['dob']); ?></td>                      <td><?php echo htmlspecialchars($r['comments']); ?></td>                  </tr>              <?php  endwhile; ?>           </tbody>   </table> 

to see row_count::

 <?php  echo $num_rows; ?> 

but whenever using $rows = $q->fetchall(); $num_rows = count($rows); row_count- result table isn't showing. whats wrong it? except these 2 lines, result shows fine.

change while loop line use foreach:

<?php foreach($rows $r): ?> ...  <?php  endforeach; ?> 

you must use rows captured in $rows array variable. cannot fetch rows pdo handler after having fetched them once.


Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -