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:
Copy codeThe Code is as follows:
<? Php
// 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. "users:". $ username. "<br/> ";
}
/*************************************** ***********************/
/* 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 ();
?>