PHP mysqli_use_result() 函数



定义和用法

mysqli_use_result() 函数开始检索先前执行的查询的结果集。

语法

mysqli_use_result($con)

参数

序号 参数及描述
1

con(必填)

表示与 MySQL 服务器连接的对象。

返回值

mysqli_use_result() 函数返回一个结果对象,并在发生错误时返回布尔值 false

PHP 版本

此函数首次引入 PHP 5 版本,并在所有后续版本中均有效。

示例

以下示例演示了 mysqli_use_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->use_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_use_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
广告