PHP mysqli扩展库 预处理技术的使用分析
时间:2023-11-21 07:10:21
1、使用mysqli扩展库 预处理技术 mysqli stmt 向数据库添加3个用户
<?php
//mysqli扩展库 预处理技术 mysqli stmt 向数据库添加3个用户
//1、创建mysqli对象
$mysqli = new MySQLi("localhost","root","root","test");
if($mysqli->connect_error){
die($mysqli->conncet_error);
}
//2、创建预编译对象
$sql="insert into user1(name,password,email,age) values(?,?,?,?)";
$mysqli_stmt=$mysqli->prepare($sql);
//绑定参数
$name="小芳";
$password="123456";
$email="xiaofang@126.com";
$age=18;
//参数绑定->给?号赋值 这里类型和顺序要一致
$mysqli_stmt->bind_param("sssi",$name,$password,$email,$age);
//执行
$b=$mysqli_stmt->execute();
//继续添加
$name="小杨";
$password="123456";
$email="xiaoyang@126.com";
$age=18;
//参数绑定->给?号赋值 这里类型和顺序要一致
$mysqli_stmt->bind_param("sssi",$name,$password,$email,$age);
//执行
$b=$mysqli_stmt->execute();
//继续添加
$name="小G";
$password="123456";
$email="xiaoG@126.com";
$age=18;
//参数绑定->给?号赋值 这里类型和顺序要一致
$mysqli_stmt->bind_param("sssi",$name,$password,$email,$age);
//执行
$b=$mysqli_stmt->execute();
if(!$b){
echo "操作失败".$mysqli_stmt->error;
}else{
echo "操作成功";
}
//关闭预编译
$mysqli_stmt->close();
$mysqli->close();
?>
2、使用预处理查询id>5的用户id name email
<?php
//使用预处理查询id>5的用户id name email
$mysqli=new MySQLi("localhost","root","root","test");
if($mysqli->connect_error){
die($mysqli->connect_error);
}
//创建预编译对象
$sql="select id,name,email from user1 where id>?";
$mysqli_stmt=$mysqli->prepare($sql);
$id=5;
//绑定参数
$mysqli_stmt->bind_param("i",$id);
//绑定结果集
$mysqli_stmt->bind_result($id,$name,$email);
//执行
$mysqli_stmt->execute();
//取出绑定的值
while($mysqli_stmt->fetch()){
echo "<br/>$id--$name--$email";
}
//关闭资源
//释放结果
$mysqli_stmt->free_result();
//关闭与编译语句
$mysqli_stmt->close();
//关闭连接
$mysqli->close();
?>
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python中三种输出格式总结(%,format,f-string)
![](https://img.aspxhome.com/file/2023/6/75356_0s.png)
使用selenium自动控制浏览器找不到Chromedriver问题
![](https://img.aspxhome.com/file/2023/2/131932_0s.jpg)
django如何自定义manage.py管理命令
![](https://img.aspxhome.com/file/2023/4/63874_0s.png)
onerror图片失效时显示其他图片
一文轻松掌握python语言命名规范规则
Python中getservbyport和getservbyname函数的用法大全
![](https://img.aspxhome.com/file/2023/8/107838_0s.png)
浅谈JavaScript中你可能不知道URL构造函数的属性
![](https://img.aspxhome.com/file/2023/9/136419_0s.jpg)
python unittest单元测试的步骤分析
将Sql Server对象的当前拥有者更改成目标拥有者
Python中if语句的基本格式实例代码
ant-design-vue中的select选择器,对输入值的进行筛选操作
![](https://img.aspxhome.com/file/2023/5/133125_0s.gif)
用非动态SQL Server SQL语句来对动态查询进行执行
Pandas数据形状df.shape的实现
![](https://img.aspxhome.com/file/2023/6/133586_0s.png)
超链“确认”对话框confirm
mysql如何分别按年/月/日/周分组统计数据详解
![](https://img.aspxhome.com/file/2023/8/106578_0s.png)
zabbix监控MySQL主从状态的方法详解
![](https://img.aspxhome.com/file/2023/4/93894_0s.png)