格式化输出数据

Original 2019-05-07 21:22:12 268
abstract:<?php//连接数据库$pdo=new PDO('mysql:dbname=php_edu;charset=utf8','root','123456');//sql语句$sql="SELECT `name`,`email`,`php_time` FROM `user` WHERE `status`=:status ";//

<?php

//连接数据库

$pdo=new PDO('mysql:dbname=php_edu;charset=utf8','root','123456');

//sql语句

$sql="SELECT `name`,`email`,`php_time` FROM `user` WHERE `status`=:status ";

//创建预处理对象

$stmt=$pdo->prepare($sql);

//参数绑定

$status=1;

$stmt->bindParam(':status',$status,PDO::PARAM_INT);

$stmt->execute();

//列绑定

$stmt->bindColumn(1,$name,PDO::PARAM_STR,20);

$stmt->bindColumn('email',$email,PDO::PARAM_STR,100);

$stmt->bindColumn(3,$php_time,PDO::PARAM_INT,100);

//列绑定 记住要用fetch_bound

while($stmt->fetch(PDO::FETCH_BOUND)){

$rows[]=[$name,$email,$php_time];

};


?>

<style>

    table,th,td {

        border: 1px solid #333;

    }

    table {

        text-align: center;

        border: 1px solid #333;

        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>姓名</th>

        <th>邮箱</th>

        <th>注册时间</th>

    </tr>

    <?php foreach ($rows as $row) :?>

        <tr>

            <td><?php echo $row[0] ?></td>

            <td><?php echo $row[1] ?></td>

            <td><?php echo date('Y/m/d',$row[2]) ?></td>

        </tr>

    <?php endforeach;?>

</table>


Correcting teacher:查无此人Correction time:2019-05-08 09:29:11
Teacher's summary:完成的不错。pdo操作数据库是最好的选择,mysqli也了解下,老项目会用到。继续加油。

Release Notes

Popular Entries