1. 程式人生 > >php mysqli預編譯

php mysqli預編譯

name use value sel sele mys rom sqli 2-2

<?php

/**
* mysqli預編譯
*/

//1、創建mysqli對象
$mysqli = new MYSQLI("wk.php","root","root","db_text");
!$mysqli->connect_error or die("連接失敗".$mysqli->connect_error);

//2、創建預編譯對象
$sql = "INSERT INTO t_user2(name,pwd,sex,age,birthday) VAlUES(?,?,?,?,?)";
$mysqli_stmt = $mysqli->prepare($sql);

//3、綁定參數(給?傳值,必須傳變量)
$name = "趙六";
$pwd = "123";
$sex = 1;
$age = 20;
$birthday = "1990-2-2";

$mysqli_stmt->bind_param("ssiis",$name,$pwd,$sex,$age,$birthday);

//4、執行
$b = $mysqli_stmt->execute();

if($b){
echo "執行成功";
}else{
echo "執行失敗".$mysqli_stmt->error;
}

$mysqli_stmt->close();
$mysqli->close();

<?php

/**
* mysqli預編譯查詢
* 預編譯可以防止sql註入攻擊
*/

//1、創建mysqli對象
$mysqli = new MYSQLI("wk.php","root","root","db_text");
!$mysqli->error or die("連接失敗".$mysqli->connect_error);
//2、創建預編譯對象
$sql = "select id,name,age from t_user2 where id > ?";
$mysqli_stmt = $mysqli->prepare($sql);
//3、綁定參數(給占位符傳值)
$id = 55;
$mysqli_stmt->bind_param("i",$id);
//4、綁定結果集
$result = $mysqli_stmt->bind_result($id,$name,$age);
//5、執行
$mysqli_stmt->execute();

//6、取出綁定的值
while($mysqli_stmt->fetch()){
echo "----$id-----$name-----$age<br>";
}

echo "****************<br>";

$id = 60;
$mysqli_stmt->bind_param("i",$id);

// $result = $mysqli_stmt->bind_result($id,$name,$age);

$mysqli_stmt->execute();


while($mysqli_stmt->fetch()){
echo "----$id-----$name-----$age<br>";
}

$mysqli_stmt->free_result();
$mysqli_stmt->close();
$mysqli->close();
?>

php mysqli預編譯