PHP mysqli擴大庫 預處置技巧的應用剖析。本站提示廣大學習愛好者:(PHP mysqli擴大庫 預處置技巧的應用剖析)文章只能為提供參考,不一定能成為您想要的結果。以下是PHP mysqli擴大庫 預處置技巧的應用剖析正文
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="[email protected]";
$age=18;
//參數綁定->給?號賦值 這裡類型溫柔序要分歧
$mysqli_stmt->bind_param("sssi",$name,$password,$email,$age);
//履行
$b=$mysqli_stmt->execute();
//持續添加
$name="小楊";
$password="123456";
$email="[email protected]";
$age=18;
//參數綁定->給?號賦值 這裡類型溫柔序要分歧
$mysqli_stmt->bind_param("sssi",$name,$password,$email,$age);
//履行
$b=$mysqli_stmt->execute();
//持續添加
$name="小G";
$password="123456";
$email="[email protected]";
$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();
?>