PDO操作读取数据库的数据,进行遍历。

Original 2019-02-18 11:47:30 1329
abstract:<?php //require 'demo21.php'; try{     $pdo=new PDO('mysql:host=localhost;dbname=edu','root','root'); }catch (Exception $e){
<?php
//require 'demo21.php';
try{
    $pdo=new PDO('mysql:host=localhost;dbname=edu','root','root');
}catch (Exception $e){
    exit($e->getMessage());
}
$sql="select id,name,sex,age,creat_time from user where status=:status";
$stmt=$pdo->prepare($sql);
$stmt->execute([':status'=>1]);
$result=$stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<style>
   table{width: 600px;border: #ccc solid;border-spacing:0px;}
   table td{width:20%;white-space:nowrap;height: 30px;line-height: 30px;text-align: center;border: 1px solid #ccc}
   table tr:nth-child(odd){background-color: #9acfea}
</style>
<table cellpadding="0" cellspacing="0">
   <tr>
       <th>id</th>
       <th>姓名</th>
       <th>性别</th>
       <th>年龄</th>
       <th>注册时间</th>
   </tr>
 <?php foreach($result as $k=>$v): ?>
      <tr>
      <td><?php echo $v['id'] ?></td>
      <td><?php echo $v['name'] ?></td>
      <td><?php echo $v['sex'] ?></td>
      <td><?php echo $v['age'] ?></td>
      <td><?php echo date('Y-h-d',$v['creat_time'])?></td>
      </tr>
 <?php
 endforeach;
 ?>
</table>

WX20190218-114631@2x.png

Correcting teacher:天蓬老师Correction time:2019-02-18 13:45:54
Teacher's summary:$stmt->execute([':status'=>1]);键名建议不要加冒号, 如果参数是通过接口,或者参数传入会有问题, php支持省略掉冒号

Release Notes

Popular Entries