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

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP mysqli_stmt_data_seek() Function Usage and Example

PHP MySQLi Reference Manual

The mysqli_stmt_data_seek() function searches for any line in the statement result set.

Definition and Usage

This function accepts a statement object and an integer value as parameters, and searches for the specified line (if any) in the given statement result set. Make sure the result set has been stored before calling this function (using mysqli_stmt_data_seek()).

Syntax

mysqli_stmt_data_seek($stmt);

Parameter

Serial NumberParameters and Description
1

stmt(required)

This represents the prepared statement object.

2

offset(required)

This is an integer value representing the required row (must be between 0 and the total number of rows in the result set).

Return value

The PHP mysqli_stmt_data_seek() function returns no value.

PHP version

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

Online example

The following example demonstratesmysqli_stmt_data_seek()Usage of the function (procedural style)-

<?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
   $stmt = mysqli_prepare($con, "SELECT); * FROM myplayers);
   //Execute statement
   mysqli_stmt_execute($stmt);
   //Bind the values in the result to variables
   mysqli_stmt_bind_result($stmt, $id, $fname, $lname, $pob, $country);
   //Store the result
   mysqli_stmt_store_result($stmt);
   //Move search
   mysqli_stmt_data_seek($stmt, 2);
   mysqli_stmt_fetch($stmt);
   print("Id:  " . $id . "\n");
   print("fname:  " . $fname . "\n");
   print("lname:  " . $lname . "\n");
   print("pob:  " . $pob . "\n");
   print("country:  " . $country . "\n");
   print("\n");
   //End statement
   mysqli_stmt_close($stmt);
   //Close connection
   mysqli_close($con);
?>

Output result

Create table.....
Insert record.....
Id: 3
fname:  Kumara
lname:  Sangakkara
pob:  Matale
country:  Srilanka

Online example

In object-oriented style, the syntax of this function is$stmt-> data_seek();。Here is an example of this function in object-oriented style;

<?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);");
   print("Create table.....\n");
   $stmt  =  $con ->  prepare("SELECT * FROM  Test");
   //Execute statement
   $stmt->execute();
   //Bind variables to result set
   $stmt->bind_result($name,  $age);
   $stmt->store_result();
   //Move search
   $stmt->data_seek(2);
   $stmt->fetch();
   print("Name:  " . $name . "\n");
   print("Age:  " . $age . "\n");
   //End statement
   $stmt->close();
   //Close connection
   $con->close();
?>

Output result

Create table.....
Name:  Sarmista
Age: 27

PHP MySQLi Reference Manual