本篇文章,介紹了PHP mysqli擴(kuò)展庫 預(yù)處理技術(shù)的使用分析。需要的朋友參考下
1、mysqli擴(kuò)展庫 預(yù)處理技術(shù) mysqli stmt 向數(shù)據(jù)庫添加3個用戶
<?php //mysqli擴(kuò)展庫 預(yù)處理技術(shù) mysqli stmt 向數(shù)據(jù)庫添加3個用戶 //1、創(chuàng)建mysqli對象 $mysqli = new MySQLi("localhost","root","root","test"); if($mysqli->connect_error){ ????????die($mysqli->conncet_error); ????} ????//2、創(chuàng)建預(yù)編譯對象 ????$sql="insert?into?user1(name,password,email,age)?values(?,?,?,?)"; ????$mysqli_stmt=$mysqli->prepare($sql); ????//綁定參數(shù) ????$name="小芳"; ????$password="123456"; ????$email="xiaofang@126.com"; ????$age=18; ????//參數(shù)綁定->給?號賦值?這里類型和順序要一致 ????$mysqli_stmt->bind_param("sssi",$name,$password,$email,$age); ????//執(zhí)行 ????$b=$mysqli_stmt->execute(); ????//繼續(xù)添加 ????$name="小楊"; ????$password="123456"; ????$email="xiaoyang@126.com"; ????$age=18; ????//參數(shù)綁定->給?號賦值?這里類型和順序要一致 ????$mysqli_stmt->bind_param("sssi",$name,$password,$email,$age); ????//執(zhí)行 ????$b=$mysqli_stmt->execute();??? ????//繼續(xù)添加 ????$name="小G"; ????$password="123456"; ????$email="xiaoG@126.com"; ????$age=18; ????//參數(shù)綁定->給?號賦值?這里類型和順序要一致 ????$mysqli_stmt->bind_param("sssi",$name,$password,$email,$age); ????//執(zhí)行 ????$b=$mysqli_stmt->execute();??? ????if(!$b){ ????????echo?"操作失敗".$mysqli_stmt->error; ????}else{ ????????echo?"操作成功"; ????} ????//關(guān)閉預(yù)編譯 ????$mysqli_stmt->close(); ????$mysqli->close(); ?>
2、使用預(yù)處理mysqlid>5的用戶id name email
<?php //使用預(yù)處理查詢id>5的用戶id?name?email ????$mysqli=new?MySQLi("localhost","root","root","test"); ????if($mysqli->connect_error){ ????????die($mysqli->connect_error); ????} ????//創(chuàng)建預(yù)編譯對象 ????$sql="select?id,name,email?from?user1?where?id>?"; ????$mysqli_stmt=$mysqli->prepare($sql); ????$id=5; ????//綁定參數(shù) ????$mysqli_stmt->bind_param("i",$id); ????//綁定結(jié)果集 ????$mysqli_stmt->bind_result($id,$name,$email); ????//執(zhí)行 ????$mysqli_stmt->execute(); ????//取出綁定的值 ????while($mysqli_stmt->fetch()){ ????????echo?"<br>$id--$name--$email"; ????} ????//關(guān)閉資源 ????//釋放結(jié)果 ????$mysqli_stmt->free_result(); ????//關(guān)閉與編譯語句 ????$mysqli_stmt->close(); ????//關(guān)閉連接 ????$mysqli->close(); ?>
? 版權(quán)聲明
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載。
THE END