Blogger Information
Blog 42
fans 0
comment 1
visits 25785
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
MySQLi面向对象实现更新操作以及 PDO实现删除操作-2018年4月26日下午19::0完成
邵军-山东-84918的博客
Original
493 people have browsed it

MySQLi面向对象实现更新操作代码:

实例

<?php
// 创建连接
$conn = new mysqli('localhost', 'root', 'root','telphone');
// 检测连接
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}
$sql="UPDATE  number SET  tel='13854687511' WHERE name='zhang'";

if ($conn->query($sql)) {
    echo "新记录插入成功";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();

运行实例 »

点击 "运行实例" 按钮查看在线实例

PDO实现删除操作:

实例

<?php
/**
 * 简化预处理新增操作
 */

//1.连接数据库,创建PDO对象
$pdo = new PDO('mysql:dbname=telphone', 'root', 'root');

//2.准备预处理SQL语句,占位符使用命名参数格式:
$sql="DELETE FROM number WHERE `home`=:home";
// $sql = "INSERT `user` SET `user_name`=:name, `email`=:email,`password`=sha1(:password)";

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

//4.绑定参数并执行SQL语句
$data = ['home'=>'beijing'];
$stmt->execute($data);
if($stmt->rowCount()>0) {
 //可直接给execute()传参数,省去参数绑定语句

echo '<h3>成功删除了'.$stmt->rowCount().'条记录</h3>';
}else
{
    echo '<h3>未找到该数据,删除失败</h3>';
}
// 5.销毁对象
$pdo = null;

运行实例 »

点击 "运行实例" 按钮查看在线实例


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