<?php //创建PDO对象 连接数据库 $pdo = new PDO('mysql:host=127.0.0.1;dbname=sanguozhi','root','root'); //创建预处理对象 $sql = 'SELECT `w_id`,`w_name`,`force`,`arms` FROM `warriors` WHERE `healthy`=:healthy'; $stmt = $pdo->prepare($sql); $stmt->execute([':healthy'=>1]); //遍历 $rows = []; while($row = $stmt->fetch(PDO::FETCH_ASSOC)){ $rows[] = $row; } print_r($rows); ?> <style> table,th,td{ border:1px solid #333; } table{ text-align:center; border:1px solid #333; width:50%; margin:30px auto; border-collapse:collapse; } table caption{ font-size:1.5em; font-weight:bolder; margin-bottom:15px; } table tr:first-chile{ background-color:lightblue; } </style> <table> <caption>武将资料</caption> <tr> <th>ID</th> <th>武将姓名</th> <th>武力</th> <th>兵种</th> </tr> <?php foreach ($rows as $row):?> <tr> <td> <?php echo $row['w_id']?></td> <td> <?php echo $row['w_name']?></td> <td> <?php echo $row['force']?></td> <td> <?php echo $row['arms']?></td> </tr> <?php endforeach;?> </table>