Php/func mysqli use result

来自菜鸟教程
跳转至:导航、​搜索

PHP mysqli use_result()函数

MySQL PHP MySQLi参考

示例-面向对象的样式

从最后执行的查询开始检索结果集:

<?php

$mysqli = new mysqli("localhost","my_user","my_password","my_db");


if ($mysqli -> connect_errno) {

  echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
  
  exit();

  }

$sql = "SELECT Lastname FROM Persons ORDER BY LastName;";

 $sql .= "SELECT Country FROM Customers";

// Execute multi query
if ($mysqli 
  -> multi_query($sql)) {
  do {
    // Store first result set
    if ($result = 
  $mysqli -> use_result()) {
      while ($row = 
  $result -> fetch_row()) {
        printf("%s\n", $row[0]);
      }
     $result 
  -> close();

      }
    // if there are more result-sets, the print a 
  divider
    if ($mysqli -> more_results()) {
      
  printf("-------------\n");
    }
     
  //Prepare next result set
  } while ($mysqli 
  -> next_result());
}

$mysqli -> close();

?>



在底部查看程序样式的示例。

定义和用法

use_result()/ mysqli_use_result()函数启动从上一次执行的查询中检索结果集。

句法

面向对象的样式:

$mysqli -> use_result()

程序风格:

mysqli_use_result(connection)

参数值

参数 描述
connection 需要。指定要使用的MySQL连接

技术细节

返回值: 返回一个无缓冲的结果对象。错误时为FALSE
PHP版本: 5+

示例-程序风格

从最后执行的查询开始检索结果集:

<?php

$con = mysqli_connect("localhost","my_user","my_password","my_db");


if (mysqli_connect_errno()) {

  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  
  exit();

  }

$sql = "SELECT Lastname FROM Persons ORDER BY LastName;";

 $sql .= "SELECT Country FROM Customers";

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

      }
    // if there are more result-sets, the print a 
  divider
    if (mysqli_more_results($con)) {
      
  printf("-------------\n");
    }
     
  //Prepare next result set
  } while (mysqli_next_result($con));
}

mysqli_close($con);

 ?>



MySQL PHP MySQLi参考