摘要:<?php //1.创建PDO对象,连接数据库 $pdo = new PDO('mysql:host=127.0.0.1;dbname=php_edu;charset=utf8','root','123456'); //2.创建预处理对象$stmt $sql = "SE
<?php
//1.创建PDO对象,连接数据库
$pdo = new PDO('mysql:host=127.0.0.1;dbname=php_edu;charset=utf8','root','123456');
//2.创建预处理对象$stmt
$sql = "SELECT `user_id`,`name`,`email`,`create_time` FROM `user` WHERE `status` = :status";
$stmt = $pdo->prepare($sql);
//参数绑定
$status = 1;
//bindValue()支持字面量,bindParam()不允许
$stmt->bindParam(':status',$status,PDO::PARAM_INT);
//$stmt->bindValue(':status',1,PDO::PARAM_INT);
//3.执行
$stmt->execute();
//4.遍历结果
$stmt->bindColumn(1,$id,PDO::PARAM_INT);
$stmt->bindColumn(2,$name,PDO::PARAM_STR,20);
$stmt->bindColumn(3,$email,PDO::PARAM_STR,100);
$stmt->bindColumn(4,$createTime,PDO::PARAM_STR,100);
$rows=[];
while ( $stmt->fetch(PDO::FETCH_ASSOC))
{
//echo $id.'--'.$name.'--'.$email.'--'.$createTime.'<br>';
//将变量转化为关联数组
$rows[] = compact('id','name','email','createTime'); //必须写成 $rows[] 带中括号(相当于往数组里添加,如果不带相当于赋值)
// print_r($rows);
}
//5.释放结果集
$stmt = null;
//6.关闭连接
$pdo = null;
//tr>td*4
?>
<style>
table,th,td{
border:1px solid #666;
}
table{
width:50%;
text-align: center;
margin:30px auto;
border-collapse: collapse;
}
table caption{
font-size: 1.5em;
font-weight: bold;
}
table tr:first-child{
background: lightblue;
}
</style>
<table>
<caption>用户信息表</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['name'] ?></td>
<td><?php echo $row['email'] ?></td>
<td><?php echo date('Y/m/d',$row['createTime']) ?></td>
</tr>
<?php endforeach; ?>
<!-- --><?php //foreach($rows as $row){
//
// echo '<tr>';
// echo '<td>'.$row['id'].'</td>';
// echo '<td>'.$row['name'].'</td>';
// echo '<td>'.$row['email'].'</td>';
// echo '<td>'.date('Y/m/d',$row['createTime']).'</td>';
// echo '</tr>';
//
// }
// ?>
</table>PDO处理步骤:
1、创建pdo对象,连接数据库
$pdo = new PDO('mysql:host=127.0.0.1;dbname=php_edu;charset=utf8','root','123456');
2、创建预处理对象$stmt
$sql = "SELECT `user_id`,`name`,`email`,`create_time` FROM `user` WHERE `status` = :status";
$stmt = $pdo->prepare($sql);
{
之间可以写:
参数绑定:
$status=1;
$stmt->bindParam('status',$status,PDO::PARAM_INT);
}
3、执行
$stmt->execute();
4、遍历结果
{
$stmt->bindColumn(1,$id,PDO::PARAM_INT);
....
}
while($stmt->fetch(PDO::FETCH_BOUND)){
$rows[] = compact('id','name','email','createTime');
}
批改老师:天蓬老师批改时间:2018-11-12 07:38:12
老师总结:很棒,如果再 配一张执行效果图 , 就会更好了