亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

完成參數(shù)綁定技術案例,創(chuàng)建自定義模板,格式化輸出數(shù)據(jù)表中的數(shù)據(jù)

Original 2019-04-13 23:23:25 257
abstract:<?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);
//預定義語句
    $sql = "SELECT `id`,`name`,`sex`,`age`, `email`,`create_time` FROM `user` WHERE `age`>:age LIMIT :num";
    $stmt = $pdo->prepare($sql);
    //設置顯示記錄條數(shù)
    $num = 6;
    //篩選條件
    $age = 65;
    //參數(shù)綁定
    $stmt ->bindParam(':num',$num,$pdo::PARAM_INT);
    $stmt ->bindValue(':age',$age,$pdo::PARAM_INT);

    //執(zhí)行預定義語句
    $stmt->execute();
    //解析遍歷
    $res  = $stmt->fetchAll($pdo::FETCH_ASSOC);
//    關閉連接
    $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>

20190413231816.jpg

經(jīng)過本章節(jié)的學習練習對PDO操作數(shù)據(jù)庫技術基本掌握。數(shù)據(jù)庫技術過多強大,后續(xù)更加深入學習才能掌握得更加得心應手

Correcting teacher:天蓬老師Correction time:2019-04-14 21:52:25
Teacher's summary:數(shù)據(jù)表的操作, 也前端密不可分的, 其實你的這個作業(yè), 已經(jīng)有點MVC的特點了

Release Notes

Popular Entries