abstrakt:<?php //1.連接數(shù)據(jù)庫 注意要有編碼集否則有亂碼。 $pdo = new PDO('mysql:host=127.0.0.1; dbname=php_edu;charset=utf8;','root','root'); //2.執(zhí)行查詢 //準(zhǔn)備sql語句 :status為
<?php //1.連接數(shù)據(jù)庫 注意要有編碼集否則有亂碼。 $pdo = new PDO('mysql:host=127.0.0.1; dbname=php_edu;charset=utf8;','root','root'); //2.執(zhí)行查詢 //準(zhǔn)備sql語句 :status為占位符,不影響SQL語句的執(zhí)行 $sql = "SELECT `id`,`name`,`email`,`create_time` FROM `user` WHERE `status` = :status ;"; //驗證sql語句并生成預(yù)處理對象 $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ù)處理對象 $stmt = null; //關(guān)閉PDO連接數(shù)據(jù)庫 $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="">用戶信息表</caption> <tr> <th>ID</th> <th>姓名</th> <th>郵箱</th> <th>注冊時間</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>
Korrigierender Lehrer:查無此人Korrekturzeit:2019-05-09 14:07:04
Zusammenfassung des Lehrers:完成的不錯。數(shù)據(jù)庫操作和前端賦值,都會了。繼續(xù)加油。