PDO预处理对数据的增删查改
PDO预处理对数据的增删查改1.增
2. 删除
$sql = "INSERT INTO T_product (proid,proname,price,description) VALUES (?,?,?,?)";
// 预处理要执行的语句
$stmt = $dbh->prepare($sql);
$proid = '120019';
$proname = '笔记本';
$price = 12800;
$description = '华硕笔记本,高配置';
$flag = $stmt->execute([$proid,$proname,$price,$description]);
if($flag){
echo '添加成功'.$dbh->lastInsertId();
}else{
echo '添加失败';
die();
}
3. 查
<?php
//预处理sql语句
$sql = 'DELETE from T_product WHERE id=?';
//准备要执行的语句
$stmt = $dbh->prepare($sql);
$delid = 2;
$flag= $stmt->execute([$delid]);
if($flag){
echo '删除成功';
}else{
echo '删除失败';
die(;)
}
4. 改
$sql = 'SELECT * FROM T_product WHERE id>?';
$stmt = $dbh->prepare($sql);
$selectid = 1;
$stmt->execute([$selectid]);
$res = $stmt->fetchAll();
if ($res){
print_r($res);
}else{
die('查询失败');
}
【文章转自中东服务器 http://www.558idc.com/dibai.html欢迎留下您的宝贵建议】
$sql = 'UPDATE T_product SET proid=?,proname=?,price=? WHERE id=?';
$stmt = $dbh->prepare($sql);
$proid = '120016';
$proname = '手机';
$price = 2600;
$updateid=1;
$flag = $stmt->execute([$proid,$proname,$price,$updateid]);
if($flag){
echo '更新成功';
}else{
die('更新失败');
}