abstract:<?phpheader('content-type:text/html;charset=utf-8');//1、創(chuàng)建PDO對象,鏈接數(shù)據(jù)庫$pdo = new PDO('mysql:host=127.0.0.1;dbname=stu','root','root');//2.創(chuàng)建預(yù)處理對象stmt$sql = "SELE
<?php
header('content-type:text/html;charset=utf-8');
//1、創(chuàng)建PDO對象,鏈接數(shù)據(jù)庫
$pdo = new PDO('mysql:host=127.0.0.1;dbname=stu','root','root');
//2.創(chuàng)建預(yù)處理對象stmt
$sql = "SELECT `id`,`username`,`sex`,`age`,`email` FROM `user` WHERE `status`= :status";
$stmt = $pdo->prepare($sql);
//3.執(zhí)行
//$stmt->execute([':status' => -1]);
//參數(shù)綁定
$status = -1;
$stmt->bindParam(':status',$status,PDO::PARAM_INT);
$stmt->execute();
//4.數(shù)據(jù)遍歷
$stmt->bindColumn(1,$id,PDO::PARAM_INT);
$stmt->bindColumn(2,$username,PDO::PARAM_STR,20);
$stmt->bindColumn(3,$sex,PDO::PARAM_STR,2);
$stmt->bindColumn(4,$age,PDO::PARAM_INT,3);
$stmt->bindColumn(5,$email,PDO::PARAM_STR,200);
$rows = [];
while($row = $stmt->fetch(PDO::FETCH_BOUND)){
//echo $username;
//將變量轉(zhuǎn)換為關(guān)聯(lián)數(shù)組
$rows[]=compact('id','username','sex','age','email');
}
//print_r($rows);
//5、釋放結(jié)果集
//$stmt = null;
//6、關(guān)閉連接
//$pdo = null;
?>
<style >
table ,th,td{
border: 1px solid #666;
}
table{
text-align: center;
border: 1px solid #666;
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>用戶信息表</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['username'] ?></td>
<td><?php echo $row['sex'] ?></td>
<td><?php echo $row['age'] ?></td>
<td><?php echo $row['email'] ?></td>
</tr>
<?php endforeach; ?>
<?php
foreach ($rows as $row){
echo '<tr>';
echo '<td>'.$row['id'].'</td>';
echo '<td>'.$row['username'].'</td>';
echo '<td>'.$row['sex'].'</td>';
echo '<td>'.$row['age'].'</td>';
echo '<td>'.$row['email'].'</td>';
echo '</tr>';
}
?>
</table>
Correcting teacher:天蓬老師Correction time:2019-04-10 10:39:46
Teacher's summary:將表中的數(shù)據(jù)循環(huán)遍歷出來, 這是一項基本操作, 抽空了解一下分頁技術(shù)