Blogger Information
Blog 42
fans 0
comment 1
visits 26347
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
OOP和PDO方式实现更新和删除操作-4月25日作业
日薪月e的博客
Original
625 people have browsed it

本次作业内容为:

1、MySQLi面向对象实现更新操作,实例代码如下:

<?php
/*mysqli面向对象的预处理技术实现更新操作*/

//1.连接数据库
require '0425hw_mysqli_connect.php';

//2.准备SQL语句
$sql = "UPDATE staff SET salary=? WHERE name=?;";

//3.创建预处理对象stmt
$stmt = $mysqli->stmt_init();

//4.检测stmt,预处理的SQL语句对象
//检测SQL语句是否正确
if($stmt->prepare($sql)) {

	//创建二维数组来保存要添加的数据
	$data[] = ['salary'=>7777, 'name'=>'喜羊羊'];
	$data[] = ['salary'=>8888, 'name'=>'美羊羊'];
	$data[] = ['salary'=>2500, 'name'=>'懒羊羊'];

	//绑定参数
	$stmt->bind_param('is',$salary,$name);

	foreach ($data as $staff) {
		$name=$staff['name'];
		$salary=$staff['salary'];
		//5.执行语句
		$stmt->execute();
		if ($stmt->affected_rows > 0) {
			echo '<br>更新成功'.$stmt->affected_rows.'条记录,'.$name.'的工资已被更新为'.$salary;
		} else {
			echo '<br>没有更新记录';
		}
	}
}

运行实例 »

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

实现效果图如下:

00-1.jpg

00.jpg

2、PDO实现删除操作,实例代码如下:

实例

<?php
/*简化PDO预处理实现删除操作*/

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

//2.创建预处理语句对象
$stmt = $pdo->prepare("DELETE FROM user WHERE user_name=:user_name");

//3.将参数绑定到预处理语句对象并执行
$stmt->execute(['user_name'=>'瑛姑']);

echo '<h2>成功删除了'.$stmt->rowCount().'条记录</h3>';

运行实例 »

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

实现效果图如下:

00-2.jpg

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