abstrak:<?php //1.連接數(shù)據(jù)庫(kù) 注意要有編碼集否則有亂碼。 $pdo = new PDO('mysql:host=127.0.0.1; dbname=php_edu;charset=utf8;','root','root'); //2.執(zhí)行查詢(xún) //準(zhǔn)備sql語(yǔ)句 :status為
<?php //1.連接數(shù)據(jù)庫(kù) 注意要有編碼集否則有亂碼。 $pdo = new PDO('mysql:host=127.0.0.1; dbname=php_edu;charset=utf8;','root','root'); //2.執(zhí)行查詢(xún) //準(zhǔn)備sql語(yǔ)句 :status為占位符,不影響SQL語(yǔ)句的執(zhí)行 $sql = "SELECT `id`,`name`,`email`,`create_time` FROM `user` WHERE `status` = :status ;"; //驗(yàn)證sql語(yǔ)句并生成預(yù)處理對(duì)象 $stmt = $pdo->prepare($sql); //綁定數(shù)據(jù).1.bindParam只能是變量,2.bindValue可以是變量也可是字面量。 $status = 1; //$status = 0; //執(zhí)行 $stmt->bindParam(':status',$status,PDO::PARAM_INT); $stmt->execute(); $rows = []; while($row=$stmt->fetch(PDO::FETCH_ASSOC)){ $rows[] = $row; } //清空預(yù)處理對(duì)象 $stmt = null; //關(guān)閉PDO連接數(shù)據(jù)庫(kù) $pdo = null; ?> <style> table,th,td { border: 2px solid #666; line-height:1.5em; } table { text-align: center; 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: lightblue; } </style> <table> <caption style="">用戶(hù)信息表</caption> <tr> <th>ID</th> <th>姓名</th> <th>郵箱</th> <th>注冊(cè)時(shí)間</th> </tr> <!--循環(huán)輸出數(shù)據(jù)foreach--> <?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 $row['create_time'] ?></td> </tr> <?php endforeach ?> <!--結(jié)束輸出--> </table>
Guru membetulkan:查無(wú)此人Masa pembetulan:2019-05-09 14:07:04
Rumusan guru:完成的不錯(cuò)。數(shù)據(jù)庫(kù)操作和前端賦值,都會(huì)了。繼續(xù)加油。