格式化输出数据表中数据

原创 2019-08-12 17:16:49 390
摘要:<?php header("content-type:text/html;charset=utf-8"); //1.创建PDO对象,连接数据库 $pdo=new PDO("mysql:host=127.0.0.1;dbname=php_edu;charset=utf8","root","root")
<?php
header("content-type:text/html;charset=utf-8");


//1.创建PDO对象,连接数据库
$pdo=new PDO("mysql:host=127.0.0.1;dbname=php_edu;charset=utf8","root","root");
//2.创建预处理对象stmt
$sql = "SELECT `user_id`,`name`,`sex`,`age` FROM `staff`";
$stmt=$pdo->prepare($sql);

//3.执行
$stmt->execute();

//4.遍历结果
$rows=[];
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $rows[]=$row;
}

//释放结果集
$stmt=null;
//关闭连接
$pdo=null;
//print_r($rows);

?>
<style>
    table,th,td{
        border:1px solid #666;
    }
    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-botton:15px;
    }
    table tr:first-child{
        background-color:lightblue;
    }
</style>
<table>
    <caption>用户信息表</caption>
    <tr>
        <th>ID</th>
        <th>姓名</th>
        <th>性别</th>
        <th>年龄</th>
    </tr>
    <?php foreach($rows as $row):?>
    <tr>
        <th><?php echo $row['user_id']?></th>
        <th><?php echo $row['name']?></th>
        <th><?php echo ($row['sex'])==1 ? '男生':'女生'?></th>
        <th><?php echo $row['age']?></th>
    </tr>
    <?php endforeach;?>
</table>

1.png

批改老师:天蓬老师批改时间:2019-08-15 10:55:24
老师总结:你的参数绑定呢, SQL中没有看到参数, 参数可以是查询条件中的变量

发布手记

热门词条