Mysqli in mysql5 is indeed worth using and learning carefully. Today I will look at some of the new
Features. For example, the preparestament support is added. The example is as follows (from the apress book)
<? PHP
// Create a new server connection
$ Mysqli = new mysqli ("127.0.0.1", "siteuser", "secret", "company ");
// Create the query and corresponding placeholders
$ Query = "insert into product set rowid = NULL, productid = ?,
Name = ?, Price = ?, Description =? ";
// Create a statement object
$ Stmt = $ mysqli-> stmt_init ();
// Prepare the statement for execution
$ Stmt-> prepare ($ query );
// Bind the parameters
$ Stmt-> bind_param ('ssds', $ productid, $ name, $ price, $ description );
// Assign the posted productid Array
$ Productidarray = $ _ post ['produd d'];
// Assign the posted name Array
$ Namearray = $ _ post ['name'];
// Assign the posted price Array
$ Pricearray =$ _ post ['price'];
// Assign the posted description Array
$ Descarray = $ _ post ['description'];
// Initialize the counter
$ X = 0;
// Cycle through each posted URL in the array, and iteratively execute the query
While ($ x <sizeof ($ productidarray )){
$ Productid = $ productidarray [$ X];
$ Name = $ namearray [$ X];
$ Price = $ pricearray [$ X];
$ Description = $ descarray [$ X];
$ Stmt-> execute ();
}
// Recuperate the statement Resources
$ Stmt-> close ();
// Close the connection
$ Mysqli-> close ();
?>
The above is a typical example, which is similar to that in Java. Note that
Create a statement object
$ Stmt = $ mysqli-> stmt_init ();
// Prepare the statement for execution
$ Stmt-> prepare ($ query );
Initialize statement first, and then call the prepare method of stmt Like java.
Finally, in the SQL statement? Assign a value
Execute with execute and close the connection with $ stmt-> close (). Remember to use $ mysqli-> close () to close the connection to mysqli.