Blogger Information
Blog 55
fans 0
comment 1
visits 42211
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
(数据库)PDO实现删除操作(4-25)-2018年5月24日8点30分
旺小舞的博客
Original
1099 people have browsed it

效果图:

4-25.jpg

步骤:

  1. 连接数据库,创建PDO对象

  2. 准备预处理SQL语句,占位符使用命名参数格式:

  3. 创建PDO预处理对象

PDO删除记录 代码:


<?php
/医院
 * 简化预处理新增操作
 */
header("Content-type:text/html;charset=utf-8");
//1.连接数据库,创建PDO对象
$pdo = new PDO('mysql:dbname=php', 'root', 'root');

//2.准备预处理SQL语句,占位符使用命名参数格式:
$sql = "DELETE FROM `user` WHERE `user_id`=:id;";

//3.创建PDO预处理对象
$stmt = $pdo->prepare($sql);

//4.绑定参数并执行SQL语句
$data = ['id'=>'13'];
$stmt->execute($data);  //可直接给execute()传参数,省去参数绑定语句
echo '<h3>成功删除了'.$stmt->rowCount().'条记录</h3>';

//如果删除多条,只需要提供一下数据,执行一下就行,预处理对象与语句已经在缓存中了,所以预处理效率最高
// $data = ['name'=>'老顽童','email'=>'lwt@php.cn', 'password'=>'123'];
// $stmt->execute($data);
// echo '<h3>成功添加了'.$stmt->rowCount().'条记录</h3>';



Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post