Correcting teacher:PHPz
Correction status:qualified
Teacher's comments:
简称:CURD,增删改查
$sql= 'INSERT `staff` SET `name` = ? , `sex` = ? , `email` = ? ';
//1.创建SQL模板
$stmt = $db->prepare($sql);
// var_dump($stmt);
echo '<br>';
//2. 执行SQL
// 单条数据插入
$stmt->execute(['杨过','1','yangguo@qq.com']);
//多条数据插入
$stmt->execute(['天宁','1','tianming@qq.com']);
$stmt->execute(['老冒','1','laomo@qq.com']);
$stmt->execute(['红红','1','honghong@qq.com']);
$stmt->execute(['小龙女','0','xiaolongnv@qq.com']);
//上线后
//$stmt->rowCount(): 返回受影响的记录数量
if($stmt->rowCount() > 0){
//查看主键id 用
echo '新增成功 ' . $db->LastInsertId();
}else{
echo '新增失败';
print_r($stmt->errorInfo());
}
1.连接数据库
require DIR.’/confint/db.php’;
//2.CURD UPDATE (修改)
// UPDATE 表命 SET 字段1=值1 … WHERE 更新的条件
$sql= ‘UPDATE staff
SET name
= ? WHERE id
= ?’;
//1.创建SQL模板
$stmt = $db->prepare($sql);
$stmt->execute([‘建红’,51]);
// var_dump( $stmt->execute([‘建红’,50]));
// die;
if ($stmt->rowCount() > 0) {
echo ‘更新成功’;
} else {
echo ‘更新失败’;
print_r($stmt->errorInfo());
} ```
// DELETE FROM 表命 ... WHERE 条件
$sql= 'DELETE FROM `staff` WHERE `id` = ? ';
// '?': 匿名占位符
// '?': 匿名占位符
// $sql= 'DELETE FROM `staff` WHERE `id` = :id ';
$stmt = $db->prepare($sql);
// '?': 匿名占位符 插值
$stmt->execute([$id]);
// '?': 匿名占位符 插值
// $stmt->execute([':id'=>55]);
if ($stmt->rowCount() > 0) {
echo '更新成功';
} else {
echo '更新失败';
print_r($stmt->errorInfo());
## .CURD SELECT (单条查询)
//1.连接数据库
require __DIR__.'/confint/db.php';
//2.CURD SELECT (单条查询)
// SELECT 字段名 FROM 表名.. WHERE 更新的条件
$sql= 'SELECT `id`,`name` FROM `staff` WHERE `id` > ?';
$stmt = $db->prepare($sql);
$stmt->execute([50]);
//单条查询fetch
// $staff = $stmt->fetch();
// printf('<pre>%s</pre>',print_r($staff,true));
// //获取关联部分
// $staff = $stmt->fetch(PDO::FETCH_ASSOC);
// printf('<pre>%s</pre>',print_r($staff,true));
// $staff = $stmt->fetch(PDO::FETCH_ASSOC);
// printf('<pre>%s</pre>',print_r($staff,true));
$staff = $stmt->fetch();
printf('<pre>%s</pre>',print_r($staff,true));