Blogger Information
Blog 31
fans 3
comment 1
visits 34316
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
mysqli面向过程使用预处理技术实现更新与删除操作
php学习笔记
Original
656 people have browsed it

一、预处理实现更新操作

<?php
//1.连接数据库
require 'mysqli_connect.php';
//2.准备SQL语句
$sql = "UPDATE user SET email=? WHERE user_name=?";
//3.创建stmt对象
$stmt = mysqli_stmt_init($db);
//4.检测SQL语句
if(mysqli_stmt_prepare($stmt,$sql)){
	//参数绑定
	mysqli_stmt_bind_param($stmt,'ss',$email,$username);
	$email = 'peter@php.cn';
	$username = 'peter';
	//执行SQL语句
	mysqli_stmt_execute($stmt);
	echo '更新了'.mysqli_stmt_affected_rows($stmt).'条记录';
}else{
	exit(mysqli_stmt_errno($stmt).':'.mysqli_stmt_error($stmt));
}
//注销stmt对象
mysqli_stmt_close($stmt);
//关闭数据库连接
mysqli_close($db);

运行结果:

1.png

二、预处理实现删除操作

<?php
//1.连接数据库
require 'mysqli_connect.php';
//2.准备SQL语句
$sql = "DELETE FROM user WHERE user_id=?";
//3.创建stmt对象
$stmt = mysqli_stmt_init($db);
//4.检测SQL语句
if(mysqli_stmt_prepare($stmt,$sql)){
	//参数绑定
	mysqli_stmt_bind_param($stmt,'i',$id);
	$id = 2;
	//执行SQL语句
	mysqli_stmt_execute($stmt);
	echo '删除了'.mysqli_stmt_affected_rows($stmt).'条记录';
}else{
	exit(mysqli_stmt_errno($stmt)).':'.mysqli_stmt_error($stmt);
}
//注销stmt对象
mysqli_stmt_close($stmt);
//关闭数据库连接
mysqli_close($db);

运行结果:

2.png


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