Perform multiple queries against the database with mysqli_next_result() in PHP

Description

The following code shows how to perform multiple queries against the database with mysqli_next_result().

Example


//from   w  w w  .  j  a  v a2 s. c  om

<?php
    $con=mysqli_connect("localhost","root","","test");
    // Check connection
    if (mysqli_connect_errno()){
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    $sql = "SELECT name FROM employee;";
    $sql .= "SELECT 1 + 1";

    // Execute multi query
    if (mysqli_multi_query($con,$sql)){
         do{
           // Store first result set
           if ($result=mysqli_store_result($con)){
             while ($row=mysqli_fetch_row($result)){
               printf("%s\n",$row[0]);
             }
             mysqli_free_result($con);
           }
          }while (mysqli_next_result($con));
    }

    mysqli_close($con);
?>

The code above generates the following result.





















Home »
  PHP Tutorial »
    MySQL »




MySQLi
MySQLi Object Oriented