Although the documentation says you must provide a number between 0 and count - 1, you can actually supply a negative number, which appears to be cast to positive (such as in abs()).
For example:
<?php
$db = new mysqli('localhost', 'test', 'password', 'schema');
$db->multi_query("
SELECT * FROM
(
SELECT 1 as 'position'
UNION SELECT 2 as 'position'
UNION SELECT 3 as 'position'
UNION SELECT 4 as 'position'
UNION SELECT 5 as 'position'
) as rows");
$result = $db->store_result();
for ($i = 0; $i < $result->num_rows; $i++)
{
$offset = $i;
$result->data_seek($offset);
var_dump("Seek offset is: {$offset}", $result->fetch_object());
}
for ($i = 0; $i < $result->num_rows; $i++)
{
$offset = -$i;
$result->data_seek($offset);
var_dump("Seek offset is: {$offset}", $result->fetch_object());
}
mysqli_stmt::data_seek
mysqli_stmt_data_seek
(PHP 5)
mysqli_stmt::data_seek -- mysqli_stmt_data_seek — ステートメントの結果セットの任意の行に移動する
説明
オブジェクト指向型
void mysqli_stmt::data_seek
( int
$offset
)手続き型
ステートメントの結果セット内で、 任意の位置に結果ポインタを移動します。
mysqli_stmt_store_result() は、 mysqli_stmt_data_seek() より前にコールしなければなりません。
パラメータ
-
stmt -
手続き型のみ: mysqli_stmt_init() が返すステートメント ID。
-
offset -
ゼロから行の総数 - 1(0.. mysqli_stmt_num_rows() - 1) までの間である必要があります。
返り値
値を返しません。
例
例1 オブジェクト指向型
<?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 Name, CountryCode FROM City ORDER BY Name";
if ($stmt = $mysqli->prepare($query)) {
/* クエリを実行します */
$stmt->execute();
/* 結果変数をバインドします */
$stmt->bind_result($name, $code);
/* 結果を取得します */
$stmt->store_result();
/* 行番号 400 に移動します */
$stmt->data_seek(399);
/* 値を取得します */
$stmt->fetch();
printf ("City: %s Countrycode: %s\n", $name, $code);
/* ステートメントを閉じます */
$stmt->close();
}
/* 接続を閉じます */
$mysqli->close();
?>
例2 手続き型
<?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 Name, CountryCode FROM City ORDER BY Name";
if ($stmt = mysqli_prepare($link, $query)) {
/* クエリを実行します */
mysqli_stmt_execute($stmt);
/* 結果変数をバインドします */
mysqli_stmt_bind_result($stmt, $name, $code);
/* 結果を取得します */
mysqli_stmt_store_result($stmt);
/* 行番号 400 に移動します */
mysqli_stmt_data_seek($stmt, 399);
/* 値を取得します */
mysqli_stmt_fetch($stmt);
printf ("City: %s Countrycode: %s\n", $name, $code);
/* ステートメントを閉じます */
mysqli_stmt_close($stmt);
}
/* 接続を閉じます */
mysqli_close($link);
?>
上の例の出力は以下となります。
City: Benin City Countrycode: NGA
phpnet at stuffonmylaptop dot com ¶
1 year ago
john at innercurmudgeon dot com ¶
5 years ago
mysqli_stmt_data_seek() only works if you have previously called mysqli_stmt_store_result().
