CakeFest 2024: The Official CakePHP Conference

mysqli_result::data_seek

mysqli_data_seek

(PHP 5, PHP 7, PHP 8)

mysqli_result::data_seek -- mysqli_data_seek結果の任意の行にポインタを移動する

説明

オブジェクト指向型

public mysqli_result::data_seek(int $offset): bool

手続き型

mysqli_data_seek(mysqli_result $result, int $offset): bool

mysqli_data_seek() 関数は、 結果セットの任意の行 offset にポインタを移動します。

パラメータ

result

手続き型のみ: mysqli_query()mysqli_store_result()mysqli_use_result()mysqli_stmt_get_result() が返す mysqli_result オブジェクト。

offset

行のオフセット。ゼロから全行数 - 1 までの間 (0..mysqli_num_rows() - 1)である必要があります。

戻り値

成功した場合に true を、失敗した場合に false を返します。

例1 mysqli::data_seek() の例

オブジェクト指向型

<?php

mysqli_report
(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

$query = "SELECT Name, CountryCode FROM City ORDER BY Name";
$result = $mysqli->query($query);

/* 401 番目の行にシークします */
$result->data_seek(400);

/* 1行取得します */
$row = $result->fetch_row();

printf("City: %s Countrycode: %s\n", $row[0], $row[1]);

手続き型

<?php

mysqli_report
(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$link = mysqli_connect("localhost", "my_user", "my_password", "world");

$query = "SELECT Name, CountryCode FROM City ORDER BY Name";

$result = mysqli_query($link, $query);

/* 401 番目の行にシークします */
mysqli_data_seek($result, 400);

/* 1行取得します */
$row = mysqli_fetch_row($result);

printf ("City: %s Countrycode: %s\n", $row[0], $row[1]);

上の例の出力は以下となります。

City: Benin City  Countrycode: NGA

例2 繰り返し処理を行う際に、結果セットのポインタを調整する

この関数は、 結果セットに対して処理を繰り返す時にカスタムの順序に並び替えたり、 結果セット全体に複数回処理を繰り返す時に、 結果セットを巻き戻すのに便利です。

<?php

mysqli_report
(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

$query = "SELECT Name, CountryCode FROM City ORDER BY Name LIMIT 15,4";
$result = $mysqli->query($query);

/* 結果セットを逆順に処理します */
for ($row_no = $result->num_rows - 1; $row_no >= 0; $row_no--) {
$result->data_seek($row_no);

/* 1行取得します */
$row = $result->fetch_row();

printf("City: %s Countrycode: %s\n", $row[0], $row[1]);
}

/* 結果セットの最初にポインタをリセットします */
$result->data_seek(0);

print
"\n";

/* 同じ結果セットを再度処理します */
while ($row = $result->fetch_row()) {
printf("City: %s Countrycode: %s\n", $row[0], $row[1]);
}

上の例の出力は以下となります。

City: Acmbaro  Countrycode: MEX
City: Abuja  Countrycode: NGA
City: Abu Dhabi  Countrycode: ARE
City: Abottabad  Countrycode: PAK

City: Abottabad  Countrycode: PAK
City: Abu Dhabi  Countrycode: ARE
City: Abuja  Countrycode: NGA
City: Acmbaro  Countrycode: MEX

注意

注意:

この関数は、mysqli_store_result()mysqli_query()mysqli_stmt_get_result() によって取得した、 バッファリングされた結果に対してのみ使えます。

参考

add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top