<?php
header("Content-type: text/html;charset=utf-8");
//建立链接
$mysqli = new MySQLi("localhost","root","254416","test");
$sql = "insert into user (username,password,email,address,age) values (?,?,?,?,?)";
//设置编码
$mysqli->query("SET NAMES utf8");
//数据库预处理
$mysqli_stmt = $mysqli->prepare($sql) or die("数据库预处理失败");
//绑定参数
$name="小倩";
$password = "123";
$email = "xiaoqian@suhu.com";
$address = "郑州";
$age = 20;
$mysqli_stmt->bind_param("ssssi",$name,$password,$email,$address,$age) or die("绑定参数失败");
//执行
$b = $mysqli_stmt->execute();
if(!$b){
die("预处理执行失败");
}
echo "添加成功";
$mysqli_stmt->close();
?>
处理dql语句:
<?php
header("Content-type: text/html;charset=utf-8");
$mysqli = new MySQLi("localhost","root","254416","test");
$mysqli->query("set names utf8");
$sql="select * from user where id>?";
$mysqli_stmt = $mysqli->prepare($sql) or die("数据库预处理失败");
$id=20;
$mysqli_stmt->bind_param("i",$id) or die("参数绑定失败");
$mysqli_stmt->bind_result($id,$username,$password,$email,$address,$age) or ("绑定结果集失败");
$b = $mysqli_stmt->execute();
if(!$b){
die("数据库预处操作理失败");
}
while($mysqli_stmt->fetch()){
echo "--$id--$username";
}
$mysqli_stmt->free_result();
$mysqli_stmt->close();
$mysqli->close();
?>
BC链 http://www.chinabic.com/?id=174 转载需授权!
网友评论