mysqli_result::data_seek

mysqli_data_seek

(PHP 5, PHP 7)

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 オブジェクト指向型

<?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 (
$result $mysqli->query($query)) {

    
/* 行番号 400 に移動します */
    
$result->data_seek(399);

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

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

    
/* 結果セットを開放します */
    
$result->close();
}

/* 接続を閉じます */
$mysqli->close();
?>

例2 手続き型

<?php
/* 接続を開きます */
$link mysqli_connect("localhost""my_user""my_password""world");

/* 接続状況をチェックします */ 
if (!$link) {
    
printf("Connect failed: %s\n"mysqli_connect_error());
    exit();
}

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

if (
$result mysqli_query($link$query)) {

    
/* 行番号 400 に移動します */
    
mysqli_data_seek($result399);

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

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

    
/* 結果セットを開放します */
    
mysqli_free_result($result);
}

/* 接続を閉じます */
mysqli_close($link);
?>

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

City: Benin City  Countrycode: NGA

注意

注意:

この関数は、mysqli_store_result() あるいは mysqli_query() によって取得した、 バッファに格納されている結果に対してのみ使用可能です。

参考

add a note add a note

User Contributed Notes 1 note

up
24
kaisellgren at gmail dot com
15 years ago
This is useful function when you try to loop through the resultset numerous times.

For example:

<?php

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

while (
$row = mysqli_fetch_assoc($result))
{
 
// Looping through the resultset.
}

// Now if you need to loop through it again, you would first call the seek function:
mysqli_data_seek($result,0);

while (
$row = mysqli_fetch_assoc($result))
{
 
// Looping through the resultset again.
}

?>
To Top