In depth understanding of mysqli preprocessing compilation

  • 2020-05-14 05:13:50
  • OfStack

I remember that before, php diandian also wrote the php tutorial of mysqli's pre-processing. At that time, I just read books and wrote randomly without understanding the principle. After a few months, I suddenly understood a lot:
Think about it. If we were to insert a thousand users, what would you do, for loop? Or does mysqli handle multiple sql? no! There are many functions in php that operate on mysql database, which simply pass sql statement to mysql database. The real sql statement is processed by mysql, and mysql database is to compile sql statement for execution. These two operations will compile the same sql statement multiple times. Programmers are always smart, and that's where mysqli comes in! mysqli also prevents sql injection attacks!
Take a look at this precompiled code:

 
<?php 
// Create a connection  
$mysqli=new mysqli("localhost","root","","test"); 
// Set up the mysqli coding  
mysqli_query($mysqli,"SET NAMES utf8"); 
// Check if the connection was created  
if (mysqli_connect_errno()) { 
printf("Connect failed:".mysqli_connect_error()); 
exit(); 
} 
// Create prepared statements  
$stmt = $mysqli->prepare("select id,username from `user` where `id` > ?"); 
/*************************************************************/ 
$id=5; 
// Binding parameters  
$stmt->bind_param("i",$id); 
// Bind result set  
$stmt->bind_result($id,$username); 
// Execute the query  
$stmt->execute(); 
// A variable that displays the result of the binding  
while($stmt->fetch()){ 
echo " The first ".$id." A user:  ".$username."<br />"; 
} 
/**************************************************************/ 
/*www.phpddt.com Hint for you: above * You can perform similar functions over and over again without having to compile again */ 
// Release of the results  
$stmt->free_result(); 
// Close the compile statement  
$stmt->close(); 
// Close the link to the database  
$mysqli->close(); 
?> 


Related articles: