Php/docs/mysqli.multi-query

提供:Dev Guides
< Php
移動先:案内検索

mysqli::multi_query

mysqli_multi_query

(PHP 5, PHP 7)

mysqli::multi_query -- mysqli_multi_queryデータベース上でクエリを実行する


説明

オブジェクト指向型

public mysqli::multi_query ( string $query ) : bool

手続き型

mysqli_multi_query ( mysqli $link , string $query ) : bool

セミコロンで連結されたひとつまたは複数のクエリを実行します。

最初のクエリの結果セットを取得するには、 mysqli_use_result() あるいは mysqli_store_result() を使用します。その他のクエリの結果は、 mysqli_more_results() および mysqli_next_result() を使用して取得します。


パラメータ

link

手続き型のみ: mysqli_connect() あるいは mysqli_init() が返すリンク ID。

query

クエリを表す文字列。

クエリ内のデータは 適切にエスケープ する必要があります。


返り値

最初のステートメントが失敗した場合にのみ false を返します。 その他のステートメントのエラーを取得するには、まず mysqli_next_result() をコールする必要があります。


例1 mysqli::multi_query() の例

オブジェクト指向型


<?php$mysqli = new mysqli("localhost", "my_user", "my_password", "world");/* 接続状況をチェックします */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";/* マルチクエリを実行します */if ($mysqli->multi_query($query)) {    do {        /* 最初の結果セットを格納します */        if ($result = $mysqli->store_result()) {            while ($row = $result->fetch_row()) {                printf("%s\n", $row[0]);            }            $result->free();        }        /* 区切り線を表示します */        if ($mysqli->more_results()) {            printf("-----------------\n");        }    } while ($mysqli->next_result());}/* 接続を閉じます */$mysqli->close();?>

手続き型


<?php$link = mysqli_connect("localhost", "my_user", "my_password", "world");/* 接続状況をチェックします */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";/* マルチクエリを実行します */if (mysqli_multi_query($link, $query)) {    do {        /* 最初の結果セットを格納します */        if ($result = mysqli_store_result($link)) {            while ($row = mysqli_fetch_row($result)) {                printf("%s\n", $row[0]);            }            mysqli_free_result($result);        }        /* 区切り線を表示します */        if (mysqli_more_results($link)) {            printf("-----------------\n");        }    } while (mysqli_next_result($link));}/* 接続を閉じます */mysqli_close($link);?>

上の例の出力は、 たとえば以下のようになります。


my_user@localhost
-----------------
Amersfoort
Maastricht
Dordrecht
Leiden
Haarlemmermeer

参考