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
廣告

© . All rights reserved.