abstrak:<?php // 數(shù)據(jù)庫連接信息 $type = 'mysql'; $host = '127.0.0.1'; $dbName&nbs
<?php // 數(shù)據(jù)庫連接信息 $type = 'mysql'; $host = '127.0.0.1'; $dbName = 'php_io'; $charset = 'utf8'; $user = 'root'; $password = 'root'; $dsn = $type.':host='.$host.';dbname='.$dbName.';charset='.$charset; $pdo = new PDO($dsn,$user,$password); //預(yù)定義語句 $sql = "SELECT `id`,`name`,`sex`,`age`, `email`,`create_time` FROM `user` WHERE `age`>:age LIMIT :num"; $stmt = $pdo->prepare($sql); //設(shè)置顯示記錄條數(shù) $num = 6; //篩選條件 $age = 65; //參數(shù)綁定 $stmt ->bindParam(':num',$num,$pdo::PARAM_INT); $stmt ->bindValue(':age',$age,$pdo::PARAM_INT); //執(zhí)行預(yù)定義語句 $stmt->execute(); //解析遍歷 $res = $stmt->fetchAll($pdo::FETCH_ASSOC); // 關(guān)閉連接 $stmt = null; $pdo = null; ?> <!----------------------------表格樣式-------------------------------------------------> <style> .table{ margin:50px auto; width: 80%; } .table,th,td{ border: 1px solid #ccc; border-collapse: collapse; text-align: center; } .table caption{ font-size: 1.5em; font-weight: bold; background-color: lightblue; } </style> <!----------------------------------------------表格模板---------------------------------------------> <table class="table"> <caption>梁山英雄榜</caption> <tr> <th>工號</th> <th>姓名</th> <th>性別</th> <th>年齡</th> <th>郵箱</th> <th>上山時間</th> </tr> <?php foreach ($res as $row):?> <tr> <td><?php echo $row['id']?> </td> <td><?php echo $row['name']?></td> <td><?php echo $row['sex']?'男':'女'; ?></td> <td><?php echo $row['age']?></td> <td><?php echo $row['email']?></td> <td><?php echo date('Y-m-d',$row['create_time'])?></td> </tr> <?php endforeach;?> </table>
經(jīng)過本章節(jié)的學(xué)習(xí)練習(xí)對PDO操作數(shù)據(jù)庫技術(shù)基本掌握。數(shù)據(jù)庫技術(shù)過多強大,后續(xù)更加深入學(xué)習(xí)才能掌握得更加得心應(yīng)手
Guru membetulkan:天蓬老師Masa pembetulan:2019-04-14 21:52:25
Rumusan guru:數(shù)據(jù)表的操作, 也前端密不可分的, 其實你的這個作業(yè), 已經(jīng)有點MVC的特點了