abstract:<?php // driver $dsn = 'mysql:host=127.0.0.1;dbname=php_mysql;charset=utf8'; $user = 'root'; $pass = '123456'; // conn try{ $pdo 
<?php // driver $dsn = 'mysql:host=127.0.0.1;dbname=php_mysql;charset=utf8'; $user = 'root'; $pass = '123456'; // conn try{ $pdo = new PDO($dsn,$user,$pass); print_r('<p class="layui-btn layui-btn-lg layui-btn-normal">数据库连接成功...</p>'); }catch(PDOException $error){ print_r('<p class="layui-btn layui-btn-lg layui-btn-danger">数据库连接失败!</p>'); exit($error->getMessage()); } // sql $sql = 'select `name`,`status`,`skill`,`email`,`create_time` from `user_info` where `status` = :status'; $stmt = $pdo->prepare($sql); // bindParam $status = 1; $stmt->bindParam(':status',$status,PDO::PARAM_INT); $stmt->execute(); // bindColumn $stmt->bindColumn('status',$status,PDO::PARAM_INT); $stmt->bindColumn('name',$name,PDO::PARAM_STR,50); $stmt->bindColumn('skill',$skill,PDO::PARAM_STR,50); $stmt->bindColumn('email',$email,PDO::PARAM_STR,50); $stmt->bindColumn('create_time',$create_time,PDO::PARAM_STR,100); // while $res = []; while($stmt->fetch(PDO::FETCH_BOUND)){ $res[] = compact('name','status','skill','email','create_time'); } // var_dump($res); // foreach // foreach($res as $value){ // print_r($value); // } // 释放结果集 $stmt = null; $pdo = null; // other echo '<link rel="stylesheet" href="/layui/layui/css/layui.css">'; echo '<script type="text/javascript" src="/layui/layui/layui.js"></script>'; ?> <?php date_default_timezone_set('Asia/Shanghai');//'Asia/Shanghai' 亚洲/上海 var_dump (date('Y-m-d H:i:s')); ?> <table class="layui-table"> <button class="layui-btn layui-btn-fluid">用户信息表</button> <colgroup> <col width="150"> <col width="200"> <col> </colgroup> <thead> <tr> <th>id</th> <th>姓名</th> <th>性别</th> <th>年龄</th> <th>状态</th> <th>简介</th> <th>时间</th> <th>email</th> <th>密码</th> </tr> </thead> <tbody> <?php foreach ($res as $row) : ?> <tr> <td><?php echo 'null' ?></td> <td><?php echo $row['name'] ?></td> <td><?php echo 'null' ?></td> <td><?php echo 'null' ?></td> <td><?php echo $row['status'] ?></td> <td><?php echo $row['skill'] ?></td> <td><?php echo date('Y-m-d H:i:s',$row['create_time']) ?></td> <td><?php echo $row['email'] ?></td> <td><?php echo 'null' ?></td> </tr> <?php endforeach; ?> </tbody> </table>
while($stmt->fetch(PDO::FETCH_BOUND)){
$res[] = compact('name','status','skill','email','create_time');
}
老师通过绑定之后的结果集为什么不像之前一样可以赋值呢?
之前的写法,可以用$row变量来接收
while($row=$stmt->fetch(PDO::FETCH_ASSOC)){
$rows[] = $row;
}
Correcting teacher:西门大官人Correction time:2019-04-02 11:13:47
Teacher's summary:可以的,只是实现方法不一样,最终结果都是从结果集中取出数据