Why can't I loop over $q->fetch() twice?

后端 未结 2 956
北恋
北恋 2021-01-20 14:50

Assume I\'m in need to loop twice with the result I got form PDO prepared statement.

First loop works fine, the next one does not work

The value of var_d

2条回答
  •  粉色の甜心
    2021-01-20 15:31

    Your are fecth a row time by time if you need two iteration you should perform a copy eg storing rows

      while ($row = $q->fetch(PDO::FETCH_ASSOC)){
          echo "
    " . $row['user_id'] . "--". $row['fname']. "
    "; $my_rows[] = $row; } // second loop this loop will NOT echo any thing ?! foreach($my_rows as $key=>$value){ echo "
    " . $value['user_id'] . "--". $value['fname']. "
    "; }

提交回复
热议问题