Preparestament supported by mysqli in MySQL 5

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.