Php+mysqli preprocessing technique for adding, modifying and deleting multiple data

Source: Internet
Author: User
Tags stmt

This paper describes the method of Php+mysqli preprocessing to add, modify and delete multiple data. Share to everyone for your reference. The specific analysis is as follows:

First of all, why is pre-processing (pre-compiled) technology necessary? For example: Suppose you want to add 100 users to a database, as a general idea, is to send 100 execution requests to the database, at this point, according to how the MySQL database works, it needs to compile each execution statement (there are 100 times). So, the efficiency here is very low.

The role of pre-processing (precompiled) technology is to reduce the number and time of compilation to improve the effect. A case to illustrate how pre-processing (precompiled) technology is done (well, first of all, when the PHP program first sends SQL statements, the MySQL database is compiled, to the next 99 times, PHP just send the data in the past, no need to compile).

<?PHP//1, CREATE database Connection object $mysqli = new mysqli ("localhost", "root", "123456", "Liuyan"), if ($mysqli->connect_error) {die ($mysqli->connect_error);} $mysqli->query ("Set names ' GBK '");//2, create precompiled object $sql = "INSERT into account (Id,balance) VALUES (?,?)"; /use here? Instead of the data value to be inserted $stmt = $mysqli->prepare ($sql);//Returns a statement object, the method in the object is shown in the manual MYSQLI_STMT//3, binding parameters (data to be inserted), and execution $id=null ;//Here My database is set to primary key auto_increment$balance=100.5; $stmt->bind_param ("id", $id, $balance);//bind parameter, return value is a Boolean value. "If" in order represents the data type of the inserted data//here $id for int, with I, $balance float type, in D, see manual $res = $stmt->execute ();//execute statement, return value is Boolean type//4, Determine if execution succeeds if (! $res) {echo "Data insertion failed with balance value:". $balance;} else{echo "Success";/****** Insert the second data *///3, bind parameters (data to be inserted), and execute $id=null;//here my database is set to primary key auto_increment$balance= 400.3; $stmt->bind_param ("id", $id, $balance);//bind parameter, return value is a Boolean value. "If" in order represents the data type of the inserted data//here $id is an int, denoted by I, $balance float type, expressed in D. $res = $stmt->execute ();//execute statement, return value is Boolean type//4, determine if execution succeeds if (! $res) {echo "Data insert failed, Balance value:". $balance;} else{echo "Success";}? >

Transferred from: http://www.jb51.net/article/60578.htm

Php+mysqli preprocessing technique for adding, modifying and deleting multiple data

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.