11-mysqli pre-compiled and executed DML statements

Source: Internet
Author: User

What is pre-compilation?

For example, if you want to add records of 100 users to the database,

The method is as follows: 1. Use the For Loop

2. Use Batch Processing Technology

The principle of SQL statement execution is: the PHP program sends the SQL statement to the database, then the database compiles the statement, and then continues the operation.

In order to reduce the Compilation Time, the pre-compilation technology emerged. Pre-compilation is performed in the database, and the same insert statement is only compiled once in the database. The PHP program is used to transmit data to the database.

Case:

<? PHP // 1. create a mysqli object $ mysqli = new mysqli ("localhost", "root", "root", "test"); $ SQL = "insert into account (balance) values (?) "; // 2. create a pre-compiled object $ mysqli_stmt = $ mysqli-> prepare ($ SQL) or die ($ mysql-> error); // 3. bind the parameter $ balance = 1000; // 4. parameter binding --> assign a value to the question mark $ mysqli_stmt-> bind_param ("I", $ balance); // 5. run $ B = $ mysqli_stmt-> execute (); // Add another // 3. bind the parameter $ balance = 2000; // 4. parameter binding --> assign a value to the question mark $ mysqli_stmt-> bind_param ("I", $ balance); // 5. run $ B = $ mysqli_stmt-> execute (); // Add another // 3. bind the parameter $ balance = 2000; // 4. parameter binding --> assign a value to the question mark $ mysqli_stmt-> bind_param ("I", $ balance); // 5 . Execute $ B = $ mysqli_stmt-> execute (); If (! $ B) {die ("operation failed! ". $ Mysqli-> error);} else {echo" OKK ";} // release $ mysqli-> close ();?>

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.