Insert multiple data entries in PHPMySQL. Insert multiple entries in phpmysql. _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Tags mysql delete mysql insert mysql update php mysql
Multiple data entries are inserted in PHPMySQL, and multiple data entries are inserted in phpmysql. PHPMySQL inserts multiple data records. phpmysql inserts multiple data records and uses MySQLi and PDO to insert multiple data records into MySQL. the mysqli_multi_query () function can be used to execute multiple SQL statements. The following example inserts multiple data records into PHP MySQL and multiple data records into phpmysql:
Use MySQLi and PDO to insert multiple data records into MySQL

The mysqli_multi_query () function can be used to execute multiple SQL statements.

The following example adds three new records to the "MyGuests" table:

Instance (MySQLi-object-oriented)

 Connect_error) {die ("Connection failed :". $ conn-> connect_error);} $ SQL = "INSERT INTO MyGuests (firstname, lastname, email) VALUES ('John', 'Doe ', 'John @ example.com '); "; $ SQL. = "insert into MyGuests (firstname, lastname, email) VALUES ('Mary ', 'Moe', 'Mary @ example.com');"; $ SQL. = "insert into MyGuests (firstname, lastname, email) VALUES ('julil', 'doole', 'Julie @ example.com ')"; if ($ conn-> multi_query ($ SQL) === TRUE) {echo "New records created successfully" ;}else {echo "Error :". $ SQL."
". $ Conn-> error;} $ conn-> close ();?>

Instance (MySQLi-process-oriented)

 ". Mysqli_error ($ conn);} mysqli_close ($ conn);?>

Instance (PDO)

 SetAttribute (PDO: ATTR_ERRMODE, PDO: ERRMODE_EXCEPTION); // start transaction $ conn-> beginTransaction (); // SQL statement $ conn-> exec ("INSERT INTO MyGuests (firstname, lastname, email) VALUES ('John', 'Doe ', 'John @ example.com ')"); $ conn-> exec ("insert into MyGuests (firstname, lastname, email) VALUES ('Mary ', 'Moe', 'Mary @ example.com ')"); $ conn-> exec ("insert into MyGuests (firstname, lastname, email) VALUES ('julil', 'doole', 'Julie @ example.com ')"); // commit the transaction $ conn-> commit (); echo "New records created successfully";} catch (PDOException $ e) {// roll back the transaction if something failed $ conn-> rollback (); echo $ SQL."
". $ E-> getMessage () ;}$ conn = null;?>

Use preprocessing statements

Mysqli extension provides the second method for inserting statements.

We can pre-process statements and bind parameters.

Mysql extension can be used to query mysql databases without data sending statements. You can associate columns or "bind" variables.

Example (MySQLi uses preprocessing statements)

 Connect_error) {die ("Connection failed:". $ conn-> connect_error);} else {$ SQL = "INSERT INTO MyGuests VALUES (?, ?, ?) "; // Initialize the statement object for mysqli_stmt_prepare () $ stmt = mysqli_stmt_init ($ conn); // preprocessing statement if (mysqli_stmt_prepare ($ stmt, $ SQL )) {// bind the parameter mysqli_stmt_bind_param ($ stmt, 'SS', $ firstname, $ lastname, $ email); // set the parameter and execute $ firstname = 'John '; $ lastname = 'Doe '; $ email = 'John @ example.com'; mysqli_stmt_execute ($ stmt); $ firstname = 'Mary '; $ lastname = 'Moe '; $ email = 'Mary @ example.com '; mysqli_stmt_execute ($ Stmt); $ firstname = 'julil'; $ lastname = 'doole'; $ email = 'Julie @ example.com '; mysqli_stmt_execute ($ stmt) ;}}?>

We can see that modularization is used in the above instances to solve the problem. We can create code blocks for easier reading and management.

Note the parameter binding. Let's take a look at the code in mysqli_stmt_bind_param:

Mysqli_stmt_bind_param ($ stmt, 'SS', $ firstname, $ lastname, $ email );

This function is bound to a parameter query and passed to the database. The second parameter is "sss ". The following lists the parameter types. The s character tells mysql that the parameter is a string.

This argument may be one of four types:

  • I-integer
  • D-double
  • S-string
  • B-BLOB

Each parameter must specify a type to ensure data security. You can determine the types to reduce the risks caused by SQL injection vulnerabilities.

Original article address:/php/php_mysql_insert_multiple.html

Php mysql-related reading materials:

  • Introduction to PHP MySQL
  • PHP connection to MySQL
  • Create a database in php
  • Create a table in php
  • Php mysq insert data
  • Insert multiple data entries in PHP MySQL
  • PHP MySQL pre-processing statement
  • Php mysql reads data
  • Php mysql where
  • PHP MySQL Order
  • PHP MySQL Update
  • PHP MySQL Delete
  • Php ODBC

Http://www.bkjia.com/PHPjc/1125994.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1125994.htmlTechArticlePHP MySQL insert multiple data, phpmysql insert multiple data using MySQLi and PDO insert multiple data to MySQL mysqli_multi_query () function can be used to execute multiple SQL statements. The following example shows...

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.