亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

利用PDO技術(shù)查詢(xún)數(shù)據(jù)庫(kù),格式化輸出表中數(shù)據(jù)

Original 2019-04-27 12:08:37 278
abstrakt:<?php //鏈接數(shù)據(jù)庫(kù),創(chuàng)建pdo對(duì)象 $pdo=new PDO('mysql:host=127.0.0.1;dbname=php_edu','root','root'); //創(chuàng)建預(yù)處理對(duì)象$stmt $stmt=$pod->prepare("SELECT `id`,`name`,`sex`,`crea
<?php
//鏈接數(shù)據(jù)庫(kù),創(chuàng)建pdo對(duì)象
$pdo=new PDO('mysql:host=127.0.0.1;dbname=php_edu','root','root');
//創(chuàng)建預(yù)處理對(duì)象$stmt
$stmt=$pod->prepare("SELECT `id`,`name`,`sex`,`create_time` FROM `user` WHERE `statue`=:statue")
//執(zhí)行查詢(xún)
$stmt->bindValue(':statue',0,PDO::PARAM_INT);      //1).進(jìn)行參數(shù)綁定
$stmt->excute();   //2)執(zhí)行查詢(xún)
//結(jié)果解析與遍歷
$stmt->bindColumn(1,$id,PDO::PARAM_INT)    //列綁定:將結(jié)果集某一列綁定到變量中
$stmt->bindColumn(2,$name,PDO::PARAM_STR,20)    //列綁定:將結(jié)果集某一列綁定到變量中
$stmt->bindColumn(3,$sex,PDO::PARAM_INT)    //列綁定:將結(jié)果集的某一列綁定到變量中
$stmt->bindColumn(4,$time,PDO::PARAM_STR,100)    //列綁定:將結(jié)果集的某一列綁定到變量中
$rows=[];
while($res=$stmt->fetch(PDO::FETCH_BOUND)){//關(guān)聯(lián)模式為綁定模式
    $rows[]=compact('id','name','sex','time');
}
//釋放結(jié)果集和關(guān)閉數(shù)據(jù)庫(kù)鏈接
$stmt=null;
$pdo=null;
?>


<!-------------------將結(jié)果格式化輸出到table中---------------->
<!--表格樣式-->
<style>
table,tr,td{ border:1px solid #ccc};
table{
width:50%;
border-collapse:collapse;
text-align=center;
margin:30px auto;
}
table caption{
font-size:1.5em;
font-weight:bolder;
margin-bottom:10px;
}
table tr:first-child{
background:#666}
</style> 
<!------表格模板----->
<table>
<caption>用戶(hù)信息表</caption>
<tr>
    <th>ID</th>
    <th>NAME</th>
    <th>SEX</th>
    <th>CREATE_TIME</th>
</tr>
<?php echo foreach($rows as $row):?>
    <tr>
    <td><?php echo $row['id']?></td>
    <td><?php echo $row['name']?></td>
    <td><?php echo $row['sex']?></td>
    <td><?php echo date('y/m/d',$row['time'])?></td>
    </tr>
<?php endforeach;?>
</table>



Korrigierender Lehrer:天蓬老師Korrekturzeit:2019-04-27 17:39:27
Zusammenfassung des Lehrers:$stmt->bindValue(':statue',0,PDO::PARAM_INT), ':statue', 這里的冒號(hào), 建議不要寫(xiě)了, 允許省略的, 這樣更直觀 , 不是嗎?

Versionshinweise

Beliebte Eintr?ge