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

用預(yù)處理中參數(shù)綁定,格式化輸入數(shù)據(jù)表中的數(shù)據(jù)

Original 2019-06-11 22:10:41 327
abstract:<?php//1.創(chuàng)建pdo對象,連接數(shù)據(jù)庫$pdo = new PDO('mysql:host=127.0.0.1;dbname=php;charset=utf8','root','root');//2.創(chuàng)建預(yù)處理對象$stmt$sql = "SELECT `id`,`name`,`position`,`mobile`,`hired

<?php
//1.創(chuàng)建pdo對象,連接數(shù)據(jù)庫
$pdo = new PDO('mysql:host=127.0.0.1;dbname=php;charset=utf8','root','root');
//2.創(chuàng)建預(yù)處理對象$stmt
$sql = "SELECT `id`,`name`,`position`,`mobile`,`hiredate` FROM `staff` WHERE `is_show` = :is_show";
$stmt = $pdo->prepare($sql);
//3.執(zhí)行
$isShow = 1;
$stmt->bindParam(':is_show',$isShow,PDO::PARAM_INT);
//$stmt->bindValue(':is_show',1,PDO::PARAM_INT);//用bindValue可以直接把字面量綁定給命名占位符
$res = $stmt->execute();
//4.拿到結(jié)果
$stmt->bindColumn(1,$id,PDO::PARAM_INT);
$stmt->bindColumn(2,$name,PDO::PARAM_STR,20);
$stmt->bindColumn(3,$position,PDO::PARAM_STR,20);
$stmt->bindColumn(4,$mobile,PDO::PARAM_STR,11);
$stmt->bindColumn(5,$hiredate,PDO::PARAM_INT);

if($res){
   //1,直接用fetchAll()方法獲取到所有數(shù)據(jù)
//    $staffs = $stmt->fetchAll(PDO::FETCH_ASSOC);

   //2.用while+fetch()循環(huán)遍歷出結(jié)果集
   $satffs = [];
   while($stmt->fetch(PDO::FETCH_BOUND)){
       //將變量轉(zhuǎn)換為關(guān)聯(lián)數(shù)組
       $staffs[] = compact('id','name','position','mobile','hiredate');
   }
}
//5.釋放結(jié)果集
$stmt = null;
//6.關(guān)閉連接
$pdo = null;

//print_r($staffs);
?>

<style>
   table, tr, th, td {
       border: 1px solid #ccc;

   }
   table{
       width: 60%;
       margin: 30px auto;
       border-collapse: collapse;
       text-align: center;
   }
   table caption {
       font-size: 1.5em;
       font-weight: bolder;
       margin-bottom: 15px;
   }
   table thead {
       background: lightblue;
   }
</style>
<table>
   <caption>員工信息表</caption>
   <thead>
   <tr>
       <th>ID</th>
       <th>姓名</th>
       <th>職位</th>
       <th>手機(jī)號</th>
       <th>入職時間</th>
   </tr>
   </thead>
   <tbody>
   <?php foreach($staffs as $staff): ?>
   <tr>
       <td><?=$staff['id']?></td>
       <td><?=$staff['name']?></td>
       <td><?=$staff['position']?></td>
       <td><?=$staff['mobile']?></td>
       <td><?=date('Y-m-d',$staff['hiredate'])?></td>
   </tr>
   <?php endforeach; ?>
   </tbody>
</table>
PDO預(yù)處理中的參數(shù)綁定.png

Correcting teacher:查無此人Correction time:2019-06-12 09:18:55
Teacher's summary:完成的不錯。pdo操作速度比之前的mysqli快很多。繼續(xù)加油

Release Notes

Popular Entries