abstract:<?php $pdo = new PDO('mysql:dbname=php_edu','root','root'); //準(zhǔn)備sql語句 $sql = "SELECT `id`,`name`,`email`,`create_time` FROM `u
<?php $pdo = new PDO('mysql:dbname=php_edu','root','root'); //準(zhǔn)備sql語句 $sql = "SELECT `id`,`name`,`email`,`create_time` FROM `user` WHERE `status` = :status ;"; $stmt = $pdo->prepare($sql); $stmt->execute([':status'=>1]); //fetch()進(jìn)行遍歷 while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { $rows[] = $row; } $stmt = null; $pdo = null; ?> <table> <caption style="">用戶信息表</caption> <tr> <th>ID</th> <th>姓名</th> <th>郵箱</th> <th>注冊時間</th> </tr> <?php foreach ($rows as $row) :?> <tr> <td><?php echo $row['id'] ?></td> <td><?php echo $row['name'] ?></td> <td><?php echo $row['email'] ?></td> <td><?php echo date('Y年 m月 d日',$row['create_time']) ?></td> </tr> <?php endforeach;?> </table>
注意循環(huán)遍歷中使用塊的形式而不是大括號的形式,有利于提高程序的可讀性。
Correcting teacher:天蓬老師Correction time:2019-04-10 10:16:27
Teacher's summary:與fetchAll()相比, fetch()+while()應(yīng)該有更高的效率的