Deep understanding of mysqli preprocessing and compilation bitsCN.com
I remember that I used to write a php Tutorial on mysqli preprocessing in php. at that time, I only read a book and did not understand the principle. after a few months, I suddenly understood a lot:
Think about it. If we want to insert many 1000 users, what do you do, for loop? Or does mysqli process multiple SQL statements? No! These processes are slow. There are many functions in php that operate mysql databases. they are nothing more than passing SQL statements to mysql databases. what really processes SQL statements is mysql, mysql databases need to compile SQL statements for execution. the above two operations will compile the same SQL statement multiple times. is this necessary? Programmers are always very smart, so with mysqli preprocessing technology! Mysqli can also prevent SQL injection attacks!
Take a look at the following pre-compiled code:
// Create a connection
$ Mysqli = new mysqli ("localhost", "root", "", "test ");
// Sets the mysqli encoding.
Mysqli_query ($ mysqli, "set names utf8 ");
// Check whether the connection is created
If (mysqli_connect_errno ()){
Printf ("Connect failed:". mysqli_connect_error ());
Exit ();
}
// Create a prepared statement
$ Stmt = $ mysqli-> prepare ("select id, username from 'user' where 'id'>? ");
/*************************************** **********************/
$ Id = 5;
// Bind parameters
$ Stmt-> bind_param ("I", $ id );
// Bind the result set
$ Stmt-> bind_result ($ id, $ username );
// Execute the query
$ Stmt-> execute ();
// Display the binding result variable
While ($ stmt-> fetch ()){
Echo "no.". $ id. "User:". $ username ."
";
}
/*************************************** ***********************/
/* Www.phpddt.com prompts you: the above * content can be repeated to execute similar functions, no need to re-compile */
// Release result
$ Stmt-> free_result ();
// Close the compilation statement
$ Stmt-> close ();
// Close the database link
$ Mysqli-> close ();
?>
BitsCN.com