English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Basic PHP Tutorial

Advanced PHP Tutorial

PHP & MySQL

PHP Reference Manual

Usage and examples of PHP mysqli_data_seek() function

PHP MySQLi Reference Manual

The mysqli_data_seek() function adjusts the result pointer to an arbitrary row in the result set.

Definition and Usage

PHP result object (belongs to mysqli_result class) represents the MySQL result returned by SELECT or DESCRIBE or EXPLAIN query.

mysqli_data_seek()The function's purpose is to accept a result object and an integer value representing the offset as parameters, and move the data of the given result object to the specified row.

Syntax

mysqli_data_seek($result, $offset);

Parameter

Serial NumberParameters and Description
1

result(必需)

This is the identifier representing the result object.

2

offset(必需)

This is an integer value representing the field offset. The range must be between 0 and the total number of rows - 1 between.

Return value

PHP mysqli_data_seek() function returns a boolean value, if successful, thenTRUE; if failed, thenFALSE.

PHP version

This function was initially introduced in PHP version5introduced and can be used in all higher versions.

Online examples

The following example demonstratesmysqli_data_seek()Usage of the function (procedural style), finding the row number in the result set 2 Data:

<?php
   $con = mysqli_connect("localhost", "root", "password", "mydb");
   mysqli_query($con, "CREATE TABLE myplayers(ID INT, First_Name VARCHAR(255), Last_Name VARCHAR(255), Place_Of_Birth VARCHAR(255), Country VARCHAR(255))");
   print("Create table.....\n");
   mysqli_query($con, "INSERT INTO myplayers values(1, 'Sikhar', 'Dhawan', 'Delhi', 'India')");
   mysqli_query($con, "INSERT INTO myplayers values(2, 'Jonathan', 'Trott', 'CapeTown', 'SouthAfrica')");
   mysqli_query($con, "INSERT INTO myplayers values(3, 'Kumara', 'Sangakkara', 'Matale', 'Srilanka')");
   print("Inserting record.....\n");
   //Retrieve the content of the table
   $res = mysqli_query($con, "SELECT * FROM myplayers");
   //Found the second row
   mysqli_data_seek($res, 1);
   //Get the content of the row
   $row = mysqli_fetch_row($res);
   print_r($row);
   //End statement
   mysqli_free_result($res);
   //Close connection
   mysqli_close($con);
?>

Output result

Create table.....
Inserting record.....
Array
(
    [0] => 2
    [1] => Jonathan
    [2] => Trott
    [3] => Cape Town
    [4] => South Africa
)

Online examples

In object-oriented style, the syntax of this function is$result->data_seek;.Here is an example of this function in an object-oriented style, looking for the row number in the result set 3 Data:

<?php
   //Establish connection
   $con = new mysqli("localhost", "root", "password", "mydb");
   $con -> query("CREATE TABLE Test(Name VARCHAR(255), AGE INT)");
   $con -> query("insert into Test values('Raju', 25),('Rahman', 30),('Sarmista', 27)");
   $con -> query("insert into Test values('Mohan', 28),('Raghav', 35),('Devika', 30)");
   print("Create table.....\n");
   $stmt = $con -> prepare("SELECT * FROM Test WHERE Name in(?, ?, ?, ?)
   $stmt -> bind_param("ssss", $name1, $name2, $name3, $name4);
   $name1 = 'Raju';
   $name2 = 'Rahman';
   $name3 = 'Raghav';
   $name4 = 'Devika';
   //Execute statement
   $stmt->execute();
   //Search result
   $res = $stmt->get_result();
   //Found the row3row
   $res->data_seek(2);
   //Get the content of the row
   $row = $res->fetch_row();
   print_r($row);
   //End statement
   $stmt->close();
   //Close connection
   $con->close();
?>

Output result

Create table.....
Array
(
    [0] => Raghav
    [1] => 35
)

PHP MySQLi Reference Manual