Mysqli's support for prepare is very beneficial for websites with large traffic volumes. it greatly reduces system overhead and ensures the stability and security of query creation. prepare preparation statements are divided into binding parameters and binding results.
Mysqli's support for prepare is very beneficial for websites with large traffic volumes. it greatly reduces system overhead and ensures the stability and security of query creation. prepare preparation statements are divided into binding parameters and binding results.
Mysqli's support for prepare is very good for websites with large traffic volumes. it greatly reduces system overhead and ensures the stability and security of query creation. Prepare preparation statements are divided into binding parameters and binding results. we will introduce them one by one!
(1) bind parameters
See the following php code:
The code is as follows:
// Create a connection
$ Mysqli = new mysqli ("localhost", "root", "", "volunteer ");
// Check whether the connection is created
If (mysqli_connect_errno ()){
Printf ("Connect failed: % s \ n", mysqli_connect_error ());
Exit ();
}
/*
* Create a prepared query statement:
*? It is a wildcard and can be used in any data with text
* It is equivalent to a template, that is, a prepared SQL statement.
*/
If ($ stmt = $ mysqli-> prepare ("insert into 'vol _ msg '(mid, content) values (?,?) ")){
/* The first parameter is the binding type. "s" refers to a string, or "I", or int. It can also be "db ",
* D indicates the double precision and floating point type, B indicates the blob type, and the second parameter is the variable.
*/
$ Stmt-> bind_param ("is", $ id, $ content );
// Assign values to variables
$ Id = "";
$ Content = "this is the inserted content ";
// Execute the prepared statement
$ Stmt-> execute ();
// Display the inserted statement
Echo "Row inserted". $ stmt-> affected_rows;
// You can add multiple statements below without prepare pre-compilation.
// Close the database link
$ Mysqli-> close ();
}
?>
The above php instance running result:
Row inserted: 1
(2). binding result: the binding result is to give the fields you bind to php variables so that these variables can be used as necessary.
See the following php code:
The code is as follows:
// Create a connection
$ Mysqli = new mysqli ("localhost", "root", "", "volunteer ");
// Sets the mysqli encoding.
Mysqli_query ($ mysqli, "set names utf8 ");
// Check whether the connection is created
If (mysqli_connect_errno ()){
Printf ("Connect failed: % s \ n", mysqli_connect_error ());
Exit ();
}
// Create a prepared statement
If ($ stmt = $ mysqli-> prepare ("select mid, content from 'vol _ msg '")){
// Execute the query
$ Stmt-> execute ();
// Bind the actual variable to the prepared statement
$ Stmt-> bind_result ($ id, $ content );
// Display the binding result variable
While ($ stmt-> fetch ()){
Echo ". $ id.": ". $ content ."
";
}
// Close the database link
$ Mysqli-> close ();
}
?>