????:<?php //鏈接數(shù)據(jù)庫,創(chuàng)建pdo對象 $pdo=new PDO('mysql:host=127.0.0.1;dbname=php_edu','root','root'); //創(chuàng)建預(yù)處理對象$stmt $stmt=$pod->prepare("SELECT `id`,`name`,`sex`,`crea
<?php //鏈接數(shù)據(jù)庫,創(chuàng)建pdo對象 $pdo=new PDO('mysql:host=127.0.0.1;dbname=php_edu','root','root'); //創(chuàng)建預(yù)處理對象$stmt $stmt=$pod->prepare("SELECT `id`,`name`,`sex`,`create_time` FROM `user` WHERE `statue`=:statue") //執(zhí)行查詢 $stmt->bindValue(':statue',0,PDO::PARAM_INT); //1).進行參數(shù)綁定 $stmt->excute(); //2)執(zhí)行查詢 //結(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ù)庫鏈接 $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>用戶信息表</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>
?? ???:天蓬老師?? ??:2019-04-27 17:39:27
???? ??:$stmt->bindValue(':statue',0,PDO::PARAM_INT),
':statue', 這里的冒號, 建議不要寫了, 允許省略的, 這樣更直觀 , 不是嗎?