批改状态:未批改
老师批语:
<?php
//PDO查询
//1.连接数据库
$pdo = new PDO('mysql:host=127.0.0.1;dbname=php;','root','root');
//2.创建预处理对象
$sql ='SELECT `id`,`name`,`mobile` FROM `staff`WHERE `age`>:age AND `sex`=:sex ';//语句模板
$stmt = $pdo->prepare($sql);//预处理对象
//3.执行预处理对象
$stmt->bindParam('age',$age,PDO::PARAM_INT,3);
$stmt->bindParam('sex',$sex,PDO::PARAM_INT,1);
$age = 40;
$sex = 1;
$res = $stmt->execute();
if(true===$res){
//// 执行成功,打印员工信息
// $result = $stmt->fetch(PDO::FETCH_ASSOC);
// print_r($result);echo '<br>';
//使用fetchALL需要注意数据无限大时,需要做好分页,要不会撑爆内存,谨慎使用
// $result= $stmt->fetchAll(PDO::FETCH_ASSOC);
// foreach($result as $staff){
// print_r($staff);echo '<br>';
// }
}
//4.关闭数据库连接
$pdo = null;点击 "运行实例" 按钮查看在线实例
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号