返回 格式化输出数据...... 登陆

格式化输出数据表中数据

. 2019-08-12 17:16:49 391
<?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

最新手记推荐

• 用composer安装thinkphp框架的步骤 • 省市区接口说明 • 用thinkphp,后台新增栏目 • 管理员添加编辑删除 • 管理员添加编辑删除

全部回复(0)我要回复

暂无评论~
  • 取消 回复 发送
  • PHP中文网