Execute multi query and print result in PHP

Description

The following code shows how to execute multi query and print result.

Example


<?php/*from w w w. j  av  a2  s . com*/
    $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