Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

mysqli
php
web development
communitycreator

What is PHP mysqli_data_seek() function?

NDUKWE CHIDERA K.

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

mysqli provides a function that starts the query return row from an nth row as indicated. This can be done using mysqli_data_seek().

The mysqli_data_seek() function will cause the result pointer in your query to be adjusted to an arbitrary row in the result-set. With this, you can point to a particular row and start to read your result from there.

Why not use SQL OFFSET

To achieve what is described above, we can also use the OFFSET clause to start out fetching from a certain row. The difference here is that with the OFFSET clause, the query result skipped the first few rows as indicated in the offset.

With mysqli_data_seek() all the results set as indicated from the query are available but you only want to fetch from a particular row downwards.

You can free up the result set later in your script. All the result set returned from the query will be available to you for use.

Syntax

mysqli_data_seek(result,offset);

Parameters

  • result: This is the query result to be chopped.
  • offset: an integer indicating the row where you wish to start fetching contents

Below is a diagrammatic representation of what is obtainable using the mysqli_data_seek() function:

Code

In the code below:

  • A database connection was created.
  • A select query was made and the result was saved in the variable $result.
  • This result was checked for existence. In this case, the result set will be chopped off till the row number 14, leaving it with row 15 downwards.
  • The results that are left were fetched and displayed on the screen.
  • The seek was ended using mysqli_free_result() function and the database connection closed.
<?php
$dbcon=mysqli_connect("localhost","user","password","dbname");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// write a SELECT statement
$sqlset="SELECT names,age FROM students ORDER BY age";
//query the database with the SELECT statement.
$result=mysqli_query($dbcon,$sqlset)
// check that the query worked and run some codes
if ($result)
{
// Seek to row number 15
mysqli_data_seek($result,14);
// Fetch row
$row=mysqli_fetch_row($result);
printf ("names: %s age: %s\n", $row[0], $row[1]);
/* Free result set so that you can now get all result
available and ready for use again.*/
mysqli_free_result($result);
}
// close database connection
mysqli_close($dbcon);
?>

RELATED TAGS

mysqli
php
web development
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring