English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The mysqli_multi_query() function executes one or more queries against the database. Multiple queries are separated by semicolons.
mysqli_multi_query()The function accepts a string value representing the query as one of its parameters and executes it on the database/Execute the given query.
Execute an SQL statement, or multiple SQL statements separated by semicolons.
To get the first result set from the execution result, use the mysqli_use_result() or mysqli_store_result() function. To read subsequent result sets, use the mysqli_more_results() and mysqli_next_result() functions.
mysqli_multi_query($con, $query)
Serial Number | Parameters and Description |
---|---|
1 | con (required) This is an object representing the connection with the MySQL Server. |
2 | query (required) This is a string value representing the query to be executed. |
3 | mode (optional) This is an integer value representing the result mode. You can useMYSQLI_USE_RESULT or MYSQLI_STORE_RESULT is passed as a value to this parameter. |
If the first SQL statement fails, it returns FALSE. If batch execution of SQL statements is performed, the mysqli_next_result() function must be called first before obtaining the error information of subsequent statements.
This function was originally introduced in PHP version5introduced and can be used in all higher versions.
The following examples demonstratemysqli_multi_query()Function Usage (Procedural Style)-
<?php //Establish Connection $con = mysqli_connect("localhost", "root", "password", "mydb"); $query = "CREATE TABLE Test(Name VARCHAR(255), AGE INT); insert into Test values('Raju', 25),('Rahman', 30),('Sarmista', 27)"; mysqli_multi_query($con, $query); print("Data added......"); //Close Connection mysqli_close($con); ?>
Output Result
Data added......
If you observe the content of the table in the database, you can see the inserted records as follows:
mysql> select * from players; +------------+-----------+-------------+ | First_Name | Last_Name | Country | +------------+-----------+-------------+ | Shikhar | Dhawan | India | | Jonathan | Trott | SouthAfrica | +------------+-----------+-------------+ 2 rows in set (0.00 sec)
In the object-oriented style, the syntax of this function is$con-> multi_query();.Here is an example of this function in an object-oriented style;
<?php $con = new mysqli("localhost", "root", "password", "mydb"); $query = "CREATE TABLE Test(Name VARCHAR(255), AGE INT); insert into Test values('Raju', 25),('Rahman', 30),('Sarmista', 27)"; //Insert records into the players table $con-> multi_query($query); print("Data Created......"); //Close Connection $res = $con -> close(); ?>
Output Result
Data added......
The following example creates a table and usesmysqli_multi_query()Execute queries one after another to fill it-
<?php $con = mysqli_connect("localhost", "root", "password", "mydb"); mysqli_multi_query($con, "CREATE TABLE IF NOT EXISTS my_team(ID INT, First_Name VARCHAR(255), Last_Name VARCHAR(255), Place_Of_Birth VARCHAR(255), Country VARCHAR(255))"); print("Table created ..."."\n"); //Insert records into the my_team table mysqli_multi_query($con, "insert into my_team values(")1, 'Shikhar', 'Dhawan', 'Delhi', 'India')"); mysqli_multi_query($con, "insert into my_team values(")2, 'Jonathan', 'Trott', 'CapeTown', 'SouthAfrica')"); mysqli_multi_query($con, "insert into my_team values(")3, 'Kumara', 'Sangakkara', 'Matale', 'Srilanka')"); mysqli_multi_query($con, "insert into my_team values(")4, 'Virat', 'Kohli', 'Delhi', 'India')"); print("Insert record ..."."\n"); //Close Connection mysqli_close($con); ?>
Output Result
Table Created ... Insert Record ...
Assuming we have already created a table Players and populated it as follows-
CREATE TABLE Players (Name VARCHAR(255), Age INT, Score INT); insert into Players values('Dhavan', 33, 90),('Rohit', 28, 26),('Kohli', 25, 50);
The following example frommysqli_multi_queryFunction to Retrieve Result Set-
<?php //Establish Connection $con = mysqli_connect("localhost", "root", "password", "mydb"); //Execute Multiple Queries $query = "SELECT * FROM players;CREATE TABLE Test (Name VARCHAR(255), Age INT);insert into Test values('Raju', 25),('Rahman', 30),('Ramani', 22);SELECT * FROM Test"; $res = mysqli_multi_query($con, $query); if ($res) { do { if ($result = mysqli_use_result($con)) { while ($row = mysqli_fetch_row($result)) { print("Name: ".$row[0]."\n"); print("Age: ".$row[1]."\n"); } mysqli_free_result($result); } if (mysqli_more_results($con)) { print("\n"); } } while (mysqli_next_result($con)); } mysqli_close($con); ?>
Output Result
Name: Dhavan Age: 33 Name: Rohit Age: 28 Name: Kohli Age: 25 Name: Raju Age: 25 Name: Rahman Age: 30 Name: Ramani Age: 22