PHP mysqli_next_result() 函数



定义和用法

mysqli_next_result() 函数准备上一个多查询中的下一个结果。您可以使用 mysqli_use_result() 函数检索准备好的结果集。

语法

mysqli_next_result($con)

参数

序号 参数和描述
1

con(必填)

这是一个表示与 MySQL 服务器连接的对象。

返回值

mysqli_next_result() 函数如果还有更多结果集则返回 true,如果不再有结果集或下一个查询有错误则返回 false

PHP 版本

此函数首次在 PHP 5 版中引入,并在所有后续版本中都能使用。

示例

以下示例演示了 mysqli_next_result() 函数的用法(以过程式风格)−

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

   //Executing the multi query
   $query = "SELECT * FROM players;SELECT * FROM emp;SELECT * FROM tutorials";
   $res = mysqli_multi_query($con, $query);

   $count = 0;

   if ($res) {
      do {
         $count = $count+1;
	     mysqli_use_result($con);
      } while (mysqli_next_result($con));
   }
   print("Number of result sets: ".$count);
   mysqli_close($con);
?>

这将产生以下结果−

Number of result sets: 3

示例

在面向对象风格中,此函数的语法为 $con->next_result(); 以下是此函数在面向对象风格中的示例 $minus;

<?php
   $con = new mysqli("localhost", "root", "password", "test");

   //Multi query
   $res = $con->multi_query("SELECT * FROM players;SELECT * FROM emp;SELECT * FROM tutorials");

   $count = 0;
   if ($res) {
      do {
         $count = $count+1;
         $con-> use_result();
      } while ($con->next_result());
   }
   print("Number of result sets: ".$count);

   //Closing the connection
   $res = $con -> close();
?>

这将产生以下结果−

Number of result sets: 3

示例

以下示例检索多查询的所有结果集的记录−

//Creating a connection
$con = mysqli_connect("localhost", "root", "password", "test");

//Executing the multi query
$query = "SELECT * FROM players;SELECT * FROM emp";

$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);

这将产生以下结果−

Name: Dhavan
Age: 33
Name: Rohit
Age: 28
Name: Kohli
Age: 25

Name: Raju
Age: 25
Name: Rahman
Age: 30
Name: Ramani
Age: 22
php_function_reference.htm
广告