Mysqli:预编译 mysqli_stmt

来源:互联网 发布:网络看电视软件 编辑:程序博客网 时间:2024/06/05 14:37

       当数据库操作语句很多时,传统的方法:每次传一条操作语句,数据库都需要先编译,然后关闭;而数据库编译语句的时间仅次于连接时间,所以造成时间过长,效率不高;所以PHP作者想到用预编译的方法,只需预编译一次,后面传的都是数据,大大节省了数据库编译时间,效率迅速提高!!!

<?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="polly@sohu.com";$age=34;$b=$stmt->execute();if(!$b){    echo "FAILED".$stmt->error;}else{    echo "SUCCESS";}$name="Lily";$password="74E738";$email="Lily@sohu.com";$age=24;$stmt->execute();$name="Luna";$password="74E738";$email="Luna@sohu.com";$age=29;$stmt->execute();echo "<br><br>END";$mysqli->close();
<?php$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 "<br>--$id--$name--$email--$age";}echo "<br>***************************************************************************";$id=38;$stmt->execute();while($stmt->fetch()){    echo "<br>--$id--$name--$email--$age";}//释放内存结果集;$stmt->free_result();//关闭预编译;$stmt->close();$mysqli->close();

0 0
原创粉丝点击