PHP 8.3.4 Released!

mysqli::use_result

mysqli_use_result

(PHP 5, PHP 7, PHP 8)

mysqli::use_result -- mysqli_use_result結果セットの取得を開始する

説明

オブジェクト指向型

public mysqli::use_result(): mysqli_result|false

手続き型

mysqli_use_result(mysqli $mysql): mysqli_result|false

データベース接続上で mysqli_real_query() 関数を使用して実行した直近のクエリから、結果セットの取得を開始します。

この関数あるいは mysqli_store_result() 関数は、 クエリ結果を取得する前にコールされる必要があります。また、どちらかの 関数をコールしなければ、データベース接続上の次のクエリは失敗します。

注意:

mysqli_use_result() は、データベースから結果セットの 全体を転送するわけではありません。そのため、セット内の行を移動するために mysqli_data_seek() を使用することはできません。 この機能を使用するには、mysqli_store_result() を使用して結果をバッファに取得する必要があります。クライアント側で 大量の処理を行う際は、mysqli_use_result() を 使用すべきではありません。なぜなら、この関数はサーバーとの接続を保持 し続け、取得しているデータに関連するテーブルについて、他のスレッドから 更新ができなくなるからです。

パラメータ

この関数にはパラメータはありません。

戻り値

バッファに取得しないで結果オブジェクトを返します。 エラー時には false を返します。

エラー / 例外

mysqli のエラー報告 (MYSQLI_REPORT_ERROR) が有効になっており、かつ要求された操作が失敗した場合は、警告が発生します。さらに、エラー報告のモードが MYSQLI_REPORT_STRICT に設定されていた場合は、mysqli_sql_exception が代わりにスローされます。

例1 mysqli::use_result() の例

オブジェクト指向型

<?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->use_result()) {
while (
$row = $result->fetch_row()) {
printf("%s\n", $row[0]);
}
$result->close();
}
/* 区切り線を表示します */
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_use_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

参考

add a note

User Contributed Notes 1 note

up
4
Anonymous
11 years ago
> One should not use mysqli_use_result() if a lot of processing on the client side is performed, since this will tie up the server and prevent other threads from updating any tables from which the data is being fetched.

Another way of understanding the "blocking" behavior of this "use_result" method is that by using this method (or the MYSQLI_USE_RESULT flag on the "query" method), if attempting to run a second query of any kind - updates, inserts, selects, or other - while still working with these first results, the second query will fail. Checking mysqli->error, you should get a "Commands out of sync" error on the second query call.

However, if you use the "store_result" method (or the default MYSQLI_STORE_RESULT flag on the "query" method) instead, the second query will execute just fine.

Just to demonstrate this "blocking" behavior of this "use_result" method, the second query on line 7 below would otherwise fail if you instead used "use_result" on line 3:

<?php
$mysqli
->real_query('SELECT * FROM `test`');
$query = $mysqli->store_result();
while (
$row = $query->fetch_assoc()) {
$id = (int) $row['id'];
$query2 = $mysqli->query("UPDATE `test` SET `label` = md5(rand()) WHERE `id` = $id");
}
?>
To Top