<?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();
?>