摘要:1. bindColumn('列名或索引', 变量, 变量类型, 最大长度),如果是字符串类型,应该指出最大长度进行预分配 2. fetch()中的结果集获取方式必须设置为: PDO::FETCH_BOUND,指明是通过bindParam()或bindColumen()来绑定变量 3. 在循环遍历时,fetch(
1. bindColumn('列名或索引', 变量, 变量类型, 最大长度),如果是字符串类型,应该指出最大长度进行预分配
2. fetch()中的结果集获取方式必须设置为: PDO::FETCH_BOUND,指明是通过bindParam()或bindColumen()来绑定变量
3. 在循环遍历时,fetch()是自动更新已经绑定到列的变量
<?php
$pdo = new PDO('mysql:dbname=php_edu','root','root');
$sql = "SELECT `id`,`name`,`email`,`create_time` FROM `user` WHERE `status` = :status ;";
$stmt = $pdo->prepare($sql);
$status = 1;
//使用字符串列名,更加直观
$stmt->bindColumn('id',$id,PDO::PARAM_INT);
$stmt->bindColumn(2,$ne,PDO::PARAM_STR,20);
$stmt->bindColumn('email',$el,PDO::PARAM_STR,100);
$stmt->bindColumn('create_time',$ct,PDO::PARAM_STR,100);
//fetch()进行遍历
while ($stmt->fetch(PDO::FETCH_BOUND)) {
    //将多个变量转为数组 compact方法中变量名要以字符串形式传入
    $rows[] = compact('id','name','el','createTime');
}
//遍历数组
foreach ($rows as $row) {
    print_r($row);
}
?>
<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>ID</th>
        <th>姓名</th>
        <th>邮箱</th>
        <th>注册时间</th>
    </tr>
    <?php foreach ($rows as $row) :?>
        <tr>
            <td><?php echo $row['id'] ?></td>
                        <td><?php echo $row['ne'] ?></td>
                        <td><?php echo $row['el'] ?></td>
                        <td><?php echo date('Y/m/d',$row['ct']) ?></td>
        </tr>
    <?php endforeach;?>
</table>
						批改老师:查无此人批改时间:2018-12-10 17:19:21		
						
老师总结:写的不错,PDO对数据库操作,方便很多了。继续努力。