abstract://连接数据库 $pdo = new PDO('mysql:host=127.0.0.1;dbname=blog', 'root', 'root'); //创建预处理对象 //$sql = "SELECT `adId`,`username`,`password`
//连接数据库 $pdo = new PDO('mysql:host=127.0.0.1;dbname=blog', 'root', 'root'); //创建预处理对象 //$sql = "SELECT `adId`,`username`,`password` FROM `tp_admin` WHERE `adId` = :adId"; $sql = "SELECT `adId`,`username`,`password` FROM `tp_admin`"; $stmt = $pdo->prepare($sql); //参数绑定 //$adId = 1; //$stmt->bindParam(':adId',$adId,PDO::PARAM_INT); //$stmt->bindValue(':adId',1,PDO::PARAM_INT); //执行SQL语句 $stmt->execute(); $stmt->bindColumn(1,$adId,PDO::PARAM_INT); $stmt->bindColumn(2,$username,PDO::PARAM_STR,20); $stmt->bindColumn(3,$password,PDO::PARAM_STR,200); //遍历结果集 $rows = []; //while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){ // $rows[] = $row; //} while ($stmt->fetch(PDO::FETCH_BOUND)){ echo $adId,$username,$password."<br>"; $rows[] = compact('adId','username','password'); } //释放预处理对象 $stmt=null; //关闭数据库 $pdo=null; <table> <caption>用户信息表</caption> <tr style="color: white"> <td>ID</td> <td>用户名</td> <td>密码</td> </tr> <?php foreach ($rows as $row) : ?> <tr> <td><?php echo $row['adId'] ?></td> <td><?php echo $row['username'] ?></td> <td><?php echo $row['password'] ?></td> </tr> <?php endforeach; ?> </table>
Correcting teacher:韦小宝Correction time:2018-11-26 14:52:25
Teacher's summary:写的很不错!参数绑定在以后的开发实战中真正的大项目是非常常用的!要记得课后多多练习哦!