abstrakt:<?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">數(shù)據(jù)庫連接成功...</p>'); }catch(PDOException $error){ print_r('<p class="layui-btn layui-btn-lg layui-btn-danger">數(shù)據(jù)庫連接失敗!</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>狀態(tài)</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;
}
Korrigierender Lehrer:西門大官人Korrekturzeit:2019-04-02 11:13:47
Zusammenfassung des Lehrers:可以的,只是實現(xiàn)方法不一樣,最終結果都是從結果集中取出數(shù)據(jù)