PHP | mysqli_multi_query() Function

PHP mysqli_multi_query() Function


Hello folks! welcome back to another edition of our tutorial on PHP. In this tutorial guide, we are going to be discussing about the PHP mysqli_multi_query() Function.

The built-in PHP mysqli_multi_query() function accepts a string value representing a query as one of the parameters and, executes and performs the given query on the database.

Multiple queries can also be passed to this PHP function by separating them with a semicolon.

You can retrieve the result set from this PHP function (in case of SELECT, SHOW, DESCRIBE and EXPLAIN queries) making use of the mysqli_use_result() and the PHP mysqli_more_results() or mysqli_store_result() and mysqli_next_result() functions.

Syntax

Following below is the syntax to use this function -

mysqli_multi_query($con,  query)


Parameter Details

Sr.NoParameter & Description
1

con(Mandatory)

This is an object representing a connection to MySQL Server.

2

query(Mandatory)

This is a string value representing the query to be executed.

3

mode(Optional)

This is a integer value representing the result mode. You can pass MYSQLI_USE_RESULT or MYSQLI_STORE_RESULT as values to this parameter.


Return Value

This built-in PHP function returns false Incase of the first function.

PHP Version

This built-in PHP function was first introduced in PHP version 5 and works in all the later versions.

Example1

The following example demonstrates the usage of the PHP mysqli_multi_query() function (in procedural style) -

<?php
   //Creating a connection
   $con = mysqli_connect("localhost", "root", "password", "mydb");
   $query = "CREATE TABLE Test(Name VARCHAR(255), AGE INT); insert into Test values('Kennedy', 27),('Paul', 30),('Justice', 28)";
   mysqli_multi_query($con, $query);
   print("Data added........");
   //Closing the connection
   mysqli_close($con);
?>

Output

When the above code is executed, it will produce the following result -

Data added........

If you observe the table contents in the database you can see the inserted records as shown below -

mysql> select * from players;
+------------+-----------+-------------+
| First_Name | Last_Name | Country     |
+------------+-----------+-------------+
| Kennedy    | Nkpara    | Nigeria     |
| Jonathan   | Trott     | SouthAfrica |
+------------+-----------+-------------+
2 rows in set (0.00 sec)

Example2

In an object oriented style the syntax of this built-in function is $con->multi_query(); The following below is an example of this PHP function in an object oriented style $minus;

<?php
   $con = new mysqli("localhost", "root", "password", "mydb");
   $query = "CREATE TABLE Test(Name VARCHAR(255), AGE INT); insert into Test values('Kennedy', 27),('Paul', 30),('Justice', 28)";
   //Inserting a records into the players table
   $con->multi_query($query);
   print("Data Created.........");
   //Closing the connection
   $res = $con -> close();
?>

Output

When the above code is executed, it will produce the following result -

Data added.........

Example3

Following example below creates a table and populates it by executing the queries one after the other using the built-in PHP mysqli_multi_query() function -

<?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");

   //Inserting a records into the my_team table
   mysqli_multi_query($con, "insert into my_team values(1, 'Kennedy', 'Nkpara', 'PortHarcourt', 'Nigeria')");
   mysqli_multi_query($con, "insert into my_team values(2, 'Jonathan', 'Trott', 'CapeTown', 'SouthAfrica')");
   mysqli_multi_query($con, "insert into my_team values(3, 'Paul', 'Peters', 'Texas', 'UnitedStates')");
   mysqli_multi_query($con, "insert into my_team values(4, 'Precious', 'Amah', 'Anambra', 'Nigeria')");

   print("Records Inserted ..."."\n");

   //Closing the connection
   mysqli_close($con);
?>

Output

When the above code is executed, it will produce the following result below -

Table Created...
Record Inserted...

Example4

Assume we have created a table players in the database and populated it, as shown below -

CREATE TABLE Players (Name VARCHAR(255), Age INT, Score INT);
insert into Players values('Prince', 28, 90),('Bethel', 27, 26),('Stephanie', 24, 50);

The following example below retrieves the resultset from a PHP mysqli_multi_query() function -

<?php
   //Creating a connection
   $con = mysqli_connect("localhost", "root", "password", "mydb");

   //Executing the multi query
   $query = "SELECT * FROM players;CREATE TABLE Test (Name VARCHAR(255), Age INT);insert into Test values('Kennedy', 27),('Paul', 30),('Justice', 28);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

When the above code is executed, it will produce the following result below -

Name: Prince
Age: 28
Name: Bethel
Age: 27
Name: Stephanie
Age: 24

Name: Kennedy
Age: 27
Name: Paul
Age: 30
Name: Justice
Age: 28


Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial, we will discuss about the PHP mysqli_next_result() Function.

Do feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Do follow us on our various social media handles available and also subscribe to our newsletter to get our tutorial posts delivered directly to your emails.

Thanks for reading and bye for now.