首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

mysqli::use_result

(PHP 5, PHP 7)

mysqli :: use_result - mysqli_use_result - 启动结果集检索

描述

面向对象的风格

代码语言:javascript
复制
mysqli_result mysqli::use_result ( void )

程序风格

代码语言:javascript
复制
mysqli_result mysqli_use_result ( mysqli $link )

用于从数据库连接上使用 mysqli_real_query()函数执行的上一次查询开始检索结果集。

必须在检索查询结果之前调用 this 或 mysqli_store_result()函数,并且必须调用其中一个或另一个来防止该数据库连接上的下一个查询失败。

注意mysqli_use_result()函数不会从数据库传输整个结果集,因此不能使用函数(如 mysqli_data_seek())移动到集合中的特定行。要使用此功能,结果集必须使用 mysqli_store_result()进行存储。如果在客户端执行了大量的处理,就不应该使用 mysqli_use_result(),因为这会绑定服务器并阻止其他线程更新从中获取数据的任何表。

返回值

返回未缓冲的结果对象或FALSE发生错误。

例子

Example #1 mysqli::use_result() example

面向对象的风格

代码语言:javascript
复制
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$query  = "SELECT CURRENT_USER();";
$query .= "SELECT Name FROM City ORDER BY ID LIMIT 20, 5";

/* execute multi query */
if ($mysqli->multi_query($query)) {
    do {
        /* store first result set */
        if ($result = $mysqli->use_result()) {
            while ($row = $result->fetch_row()) {
                printf("%s\n", $row[0]);
            }
            $result->close();
        }
        /* print divider */
        if ($mysqli->more_results()) {
            printf("-----------------\n");
        }
    } while ($mysqli->next_result());
}

/* close connection */
$mysqli->close();
?>

程序风格

代码语言:javascript
复制
<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$query  = "SELECT CURRENT_USER();";
$query .= "SELECT Name FROM City ORDER BY ID LIMIT 20, 5";

/* execute multi query */
if (mysqli_multi_query($link, $query)) {
    do {
        /* store first result set */
        if ($result = mysqli_use_result($link)) {
            while ($row = mysqli_fetch_row($result)) {
                printf("%s\n", $row[0]);
            }
            mysqli_free_result($result);
        }
        /* print divider */
        if (mysqli_more_results($link)) {
            printf("-----------------\n");
        }
    } while (mysqli_next_result($link));
}

/* close connection */
mysqli_close($link);
?>

上面的例子会输出:

代码语言:javascript
复制
my_user@localhost
-----------------
Amersfoort
Maastricht
Dordrecht
Leiden
Haarlemmermeer

也可以看看

  • mysqli_real_query() - 执行一个 SQL 查询
  • mysqli_store_result() - 传输最后一个查询的结果集

← mysqli::thread_safe

mysqli::$warning_count →

扫码关注腾讯云开发者

领取腾讯云代金券