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

mysqli::multi_query

(PHP 5, PHP 7)

mysqli :: multi_query - mysqli_multi_query - 对数据库执行查询

描述

面向对象的风格

代码语言:javascript
复制
bool mysqli::multi_query ( string $query )

程序风格

代码语言:javascript
复制
bool mysqli_multi_query ( mysqli $link , string $query )

执行一个或多个以分号连接的查询。

要从第一个查询中检索结果集,可以使用mysqli_use_result()或mysqli_store_result()。所有后续查询结果可以使用mysqli_more_results()和mysqli_next_result()进行处理。

参数

代码语言:txt
复制
`link`   

仅过程样式:由mysqli_connect()或mysqli_init()返回的链接标识符

query

作为一个字符串查询

查询中的数据应正确转义。

返回值

如果第一条语句失败,则返回FALSE。 要从其他语句中检索后续错误,必须先调用mysqli_next_result()。

例子

示例#1 mysqli :: multi_query()示例

面向对象的风格

代码语言: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->store_result()) {
            while ($row = $result->fetch_row()) {
                printf("%s\n", $row[0]);
            }
            $result->free();
        }
        /* 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_store_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_query() - 对数据库执行查询
  • mysqli_use_result() - 启动结果集检索
  • mysqli_store_result() - 传输最后一个查询的结果集
  • mysqli_next_result() - 从multi_query准备下一个结果
  • mysqli_more_results() - 检查多查询是否有更多查询结果

← mysqli::more_results

mysqli::next_result →

扫码关注腾讯云开发者

领取腾讯云代金券