abstract:<?php //链接数据库,创建pdo对象 $pdo=new PDO('mysql:host=127.0.0.1;dbname=php_edu','root','root'); //创建预处理对象$stmt $stmt=$pod->prepare("SELECT `id`,`name`,`sex`,`crea
<?php //链接数据库,创建pdo对象 $pdo=new PDO('mysql:host=127.0.0.1;dbname=php_edu','root','root'); //创建预处理对象$stmt $stmt=$pod->prepare("SELECT `id`,`name`,`sex`,`create_time` FROM `user` WHERE `statue`=:statue") //执行查询 $stmt->bindValue(':statue',0,PDO::PARAM_INT); //1).进行参数绑定 $stmt->excute(); //2)执行查询 //结果解析与遍历 $stmt->bindColumn(1,$id,PDO::PARAM_INT) //列绑定:将结果集某一列绑定到变量中 $stmt->bindColumn(2,$name,PDO::PARAM_STR,20) //列绑定:将结果集某一列绑定到变量中 $stmt->bindColumn(3,$sex,PDO::PARAM_INT) //列绑定:将结果集的某一列绑定到变量中 $stmt->bindColumn(4,$time,PDO::PARAM_STR,100) //列绑定:将结果集的某一列绑定到变量中 $rows=[]; while($res=$stmt->fetch(PDO::FETCH_BOUND)){//关联模式为绑定模式 $rows[]=compact('id','name','sex','time'); } //释放结果集和关闭数据库链接 $stmt=null; $pdo=null; ?> <!-------------------将结果格式化输出到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>
Correcting teacher:天蓬老师Correction time:2019-04-27 17:39:27
Teacher's summary:$stmt->bindValue(':statue',0,PDO::PARAM_INT),
':statue', 这里的冒号, 建议不要写了, 允许省略的, 这样更直观 , 不是吗?