Mysqli的先行編譯機制處理批量資料過程,mysqli編譯機制

來源:互聯網
上載者:User

Mysqli的先行編譯機制處理批量資料過程,mysqli編譯機制

mysqli增強,還有一部分是對事物處理機制和先行編譯機制的增加,其實這兩者都是為安全和執行效率著想的,這裡主要說一下mysqli的先行編譯機制。

所謂的先行編譯,並不是在php的核心進行編譯,而是資料庫管理系統進行先行編譯,由於用於批量資料,說白了就是把一部分固定的資料格式先在mysql上面進行一次編譯,編譯之後就不在對其進行再次編譯,我們要做的就是,向編譯的預留位置(就是資料佔位)添加資料,之後發送,這樣的話,大大的減少了編譯次數,提高了批處理的效率。


下面是一段簡單的範例程式碼:

<?php$mysqli=new MySQLi("localhost","root","toor","test");$sql="insert into account (id,blance) values (?,?)";//也可以多個問好,逗號隔開$mysqli_stmt=$mysqli->prepare("$sql");//創造先行編譯對象/**每一條資料發送都是獨立執行的*/$id=6;$blance=23;$mysqli_stmt->bind_param("id",$id,$blance);//添加$b=$mysqli_stmt->execute();//發送$id=7;$blance=33;$mysqli_stmt->bind_param("id",$id,$blance);$b=$mysqli_stmt->execute();$id=8;$blance=99;$mysqli_stmt->bind_param("id",$id,$blance);$b=$mysqli_stmt->execute();if($b){die("執行成功");}else{die("執行失敗".$mysqli_stmt->error);}$mysqli->close();?>
下面是做的查詢操作:

<?php$mysqli=new MySQLi("localhost","root","toor","test");if(mysqli_connect_error()){die(mysqli_connect_error());}$sql="select id,blance from account where id>?";$mysqli_stmt=$mysqli->prepare($sql);/*第一次查詢*/$id=3;$mysqli_stmt->bind_param("i",$id);//綁定資料$mysqli_stmt->bind_result($id,$account);//綁定結果集$mysqli_stmt->execute();while($mysqli_stmt->fetch()){echo "<br/>--$id-----$account";}echo "<br/>---------------------------------";/*第二次查詢*/$id=5;$mysqli_stmt->bind_param("i",$id);//綁定資料//$mysqli_stmt->bind_result($id,$account);//綁定結果集$mysqli_stmt->execute();while($mysqli_stmt->fetch()){echo "<br/>--$id-----$account";}echo "<br/>---------------------------------";/*第三次查詢*/$id=6;$mysqli_stmt->bind_param("i",$id);//綁定資料//$mysqli_stmt->bind_result($id,$account);//綁定結果集這裡已經綁定了$mysqli_stmt->execute();while($mysqli_stmt->fetch()){echo "<br/>--$id-----$account";}$mysqli_stmt->close();$mysqli->close();?>


註:僅供參考和csdn轉載。

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.