<?php $dsn = 'mysql:host=127.0.0.1;dbname=test'; $pdo = new PDO($dsn,'root','root'); $sql = "SELECT `id`,`name`,`email`,`create_time` FROM `user` WHERE `status`=:status AND `id`>=:id"; $stmt = $pdo->prepare($sql); $status = 0; $stmt->bindParam(':status',$status,PDO::PARAM_INT); $stmt->bindValue(':id',1,PDO::PARAM_INT); $stmt->execute(); $stmt->bindColumn(1,$id,PDO::PARAM_INT); $stmt->bindColumn(2,$name,PDO::PARAM_STR,20); $stmt->bindColumn(3,$email,PDO::PARAM_STR,100); $stmt->bindColumn(4,$time,PDO::PARAM_INT,10); $rows = []; while($stmt->fetch(PDO::FETCH_BOUND)){ echo $id,$name,$email,$time,'<br>'; # 将变量转为关联数组 $rows[] = compact('id','name','email','time'); } $stmt = null; $pdo = null; ?> <style> table,th,td{border: 1px solid #ccc;} table{text-align: center;border: 1px solid #ccc;width: 50%;margin: 30px auto;border-collapse: collapse;} table caption{font-size: 1.5em;font-weight: bolder;margin-bottom: 15px;} table tr:first-child{background-color: lightsalmon;} </style> <table> <caption>用户信息表</caption> <tr> <th>ID</th> <th>姓名</th> <th>邮箱</th> <th>注册时间</th> </tr> <?php foreach($rows as $row):?> <tr> <td><?=$row['id']?></td> <td><?=$row['name']?></td> <td><?=$row['email']?></td> <td><?=date("Y-m-d H:i:s",$row['time']);?></td> </tr> <?php endforeach; ?> </table>