Mysqli:プリコンパイルmysqli_stmt
7094 ワード
データベースの操作文が多い場合、従来の方法では、操作文が1つ伝わるたびに、データベースがコンパイルされ、閉じる必要があります.データベースのコンパイル文の時間は接続時間に次ぐので、時間が長すぎて効率が悪い.だからPHP作者はプリコンパイルの方法で、一度だけプリコンパイルすることを考えて、后ろに伝わるのはすべてデータで、大いにデータベースのコンパイルの时间を节约して、効率は急速に向上します!!!
$mysqli=new mysqli("localhost","root","123456","test002");
if($mysqli->connect_error){
die ("LINK FAILED".$mysqli->connect_error);
}
$query="insert into user1 (name,password,email,age) values (?,?,?,?)";
$stmt=$mysqli->prepare($query);
$stmt->bind_param('sssi',$name,$password,$email,$age);
$name="Polly";
$password="74E738";
$email="[email protected]";
$age=34;
$b=$stmt->execute();
if(!$b){
echo "FAILED".$stmt->error;
}else{
echo "SUCCESS";
}
$name="Lily";
$password="74E738";
$email="[email protected]";
$age=24;
$stmt->execute();
$name="Luna";
$password="74E738";
$email="[email protected]";
$age=29;
$stmt->execute();
echo "
END";
$mysqli->close();
$mysqli=new mysqli("localhost","root","123456","test002"); if($mysqli->connect_error){ die ("LINK FAILED".$mysqli->connect_error); } $mysqli->query("set names utf8"); $query="select id,name,email,age from user1 where id>?"; $stmt=$mysqli->prepare($query); $stmt->bind_param('i',$id); $id=8; $stmt->bind_result($id,$name,$email,$age); $stmt->execute(); while($stmt->fetch()){ echo "
--$id--$name--$email--$age"; } echo "
***************************************************************************"; $id=38; $stmt->execute(); while($stmt->fetch()){ echo "
--$id--$name--$email--$age"; } // ; $stmt->free_result(); // ; $stmt->close(); $mysqli->close();