韩顺平php 利用mysqli实现预编译查询任务(sql语句)

在这里插入图片描述

<?php
//预编译演示
//需求:请使用预处理的方式,从数据库查询select语句
//使用预处理的方法,查询所有id>5的用户 id name email
$mysqli=new MYSQLi("localhost","root","hsp123","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->blind_param("i",$id);
//绑定结果集
$mysqli_stmt->bind_result($id,$name,$email);
//执行
$mysqli_stmt->execute();
while($mysqli_stmt->fetch()){
 echo "<br/>-----$id--$name---$email";
}

//还想执行另一个sql语句
echo "<br/>***************************";
$id=10;
//绑定参数
$mysqli_stmt->blind_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;
?>

猜你喜欢

转载自blog.csdn.net/weixin_43345480/article/details/89787572