标签:
思路原理
代码如下:
1 <?php 2 //预编译演示 3 //需求: 请使用预处理的方式,向数据库添加三个用户 4 //1.创建mysqli对象 5 $mysqli=new MySQLi("localhost","root","hsp123","test"); 6 //2.创建预编译对象 7 $sql="insert into user1 (name,password,email,age) values(?,?,?,?)"; 8 $mysqli_stmt=$mysqli->prepare($sql) or die($mysqli->error); 9 //绑定参数 10 $name="小倩"; 11 $password="xiaoqian"; 12 $email="aa@sohu.com"; 13 $age="200"; 14 //参数绑定->给?赋值,这里类型和顺序都要对应. 15 // 16 $mysqli_stmt->bind_param("sssi",$name,$password,$email,$age); 17 //执行 18 $b=$mysqli_stmt->execute(); 19 20 //继续添加 21 $name="老妖"; 22 $password="laoyao"; 23 $email="laoyao@sohu.com"; 24 $age="210"; 25 $mysqli_stmt->bind_param("sssi",$name,$password,$email,$age); 26 //执行 27 $b=$mysqli_stmt->execute() ; 28 29 //继续添加 30 $name="菜层"; 31 $password="aaa"; 32 $email="aa@sohu.com"; 33 $age="30"; 34 $mysqli_stmt->bind_param("sssi",$name,$password,$email,$age); 35 //执行 36 $b=$mysqli_stmt->execute(); 37 if(!$b){ 38 die("操作失败".$mysqli_stmt->error); 39 }else{ 40 echo "操作ok"; 41 } 42 //释放 43 $mysqli->close(); 44 ?>
标签:
原文地址:http://www.cnblogs.com/zzg521/p/4206303.html