创建自定义模板,格式化输出数据表中的数据

Original 2019-04-18 17:38:03 215
abstract:<?php    //创建自定义模板,格式化输出数据表中的数据 //1.创建PDO,连接数据库 $pdo=new PDO('mysql:host=127.0.0.1;dbname=php_edu;','root','root'); //2.创建预处理对象STMT $sql="SELECT&nbs
<?php   
//创建自定义模板,格式化输出数据表中的数据
//1.创建PDO,连接数据库
$pdo=new PDO('mysql:host=127.0.0.1;dbname=php_edu;','root','root');
//2.创建预处理对象STMT
$sql="SELECT `user_id`,`name`,`age`,`email`,`create_time` FROM `user` WHERE `status`=:status";
$stmt=$pdo->prepare($sql);
//3.执行
//参数绑定
$stmt->bindValue(':status',1,PDO::PARAM_INT);
$stmt->execute();
//4.遍历结果
$stmt->bindColumn(1,$id,PDO::PARAM_INT);
$stmt->bindColumn(2,$name,PDO::PARAM_STR,20);
$stmt->bindColumn(3,$age,PDO::PARAM_INT);
$stmt->bindColumn(4,$email,PDO::PARAM_STR,100);
$stmt->bindColumn(5,$createTime,PDO::PARAM_STR,100);
$rows=[];
while($stmt->fetch(PDO::FETCH_BOUND)){
    $rows[]=compact('id','name','age','email','createTime');
}
//5.释放结果集
$stmt=null;
//6.关闭链接
$pdo=null;
?>
//格式化输出
<style>
    table,th,td{
        border:1px solid #666;
    }
    table{
        border-collapse:collapse;
        margin:50px auto;
        text-align:center;
        width:50%;
    }
    th{
        background-color:lightblue;
    }
    caption{
        margin-bottom:15px;
        font-size:18px;
        font-weight:bolder;
    }
 
</style>
<table>
<caption>用户信息表</caption>
<tr>
    <th>ID</th>
    <th>姓名</th>
    <th>年龄</th>
    <th>邮箱</th>
    <th>注册时间</th>
</tr>
<?php foreach($rows as $row): ?>
<tr>
    <td><?php echo $row['id'];?></td>
    <td><?php echo $row['name'];?></td>
    <td><?php echo $row['age'];?></td>
    <td><?php echo $row['email'];?></td>
    <td><?php echo $row['createTime'];?></td>    
</tr>
<?php endforeach; ?>
</table>


Correcting teacher:查无此人Correction time:2019-04-19 09:31:28
Teacher's summary:完成的不错。现在数据库都是使用pdo,不过mysqli也要熟悉下,工作中,还会有老的项目。继续加油。

Release Notes

Popular Entries