mysqli_data_seek

How to reuse a single query result without calling the server twice: mysqli_data_seek, and its PDO equivalent with a CURSOR_SCROLL cursor.

In one of my projects, a complex system with dozens of screens written in PHP combined with HTML, I was quite often required to use a single query result several times while printing the page, in a non-consecutive way. That is, I had to loop over all the result records for one element, and then again for another element. For example, on a users page I printed user cards containing their personal details: name, address, phone and so on.

And at the bottom of the users page I printed a slider containing the users’ pictures, with each picture serving as an anchor link to the user’s card.

So it turns out that both the card details and the user picture details are pulled from the result of the same query (the user details and the user picture are stored in the same table, the users table). Of course I didn’t make a second server call for the same query. So what did I do?

The solution: mysqli_data_seek

The answer is mysqli_data_seek.

The mysqli_data_seek function lets you point to a specific result row, so that even after I have gone through all the rows, I can go back and point to the result of the first row again. Of course, if needed, you can also point to the result of any other specific row.

The function takes two parameters:

  1. A variable holding the query result.
  2. A number specifying the row number to point to. The number must be between 0 and the number of existing rows minus 1 (counting starts from zero).

For example:

PHP
<?php
mysqli_data_seek( $sql, 0 );
?>

An extended example

In more detail, here is an example of printing a list of user names in one list, and then printing the users’ picture data in a list element. To do this, we fetched the user details from the users table with an SQL query, printed the user names, pointed back to the result of the first row, and printed data from the result of the query that fetched the user details once more, this time the user’s picture.

PHP
<?php
$sql = mysqli_query(
  $conn,
  "SELECT *
  FROM users"
);
?>

<ul>
  <?php while($r = mysqli_fetch_object($sql)){ ?>
    <li>
      <?php echo $r->user_name; ?>
    </li>
  <?php } ?>
</ul>

<?php mysqli_data_seek( $stuff, 0 ); ?>
<ul>
  <?php while($r = mysqli_fetch_object($sql)){ ?>
    <li>
      <img src="<?php echo $r->user_url_img; ?>" alt="<?php echo $r->user_alt_img; ?>">          
    </li>
  <?php } ?>
</ul>

Update: the PDO equivalent

Update: some time has passed, and these days when writing PHP queries I no longer use mysqli but PDO. I looked for the PDO equivalent of mysqli_data_seek: how can you move the pointer back to the start of the query result list, after getting the query results, in order to reuse them?

In PDO the default cursor is PDO::CURSOR_FWDONLY, and it affects the cursor so that it cannot go back and point to result 0, as we saw with mysqli_data_seek, which does allow going back and pointing to result 0. So you need to set up a different cursor, the CURSOR_SCROLL cursor – a cursor that allows scrolling. For example:

PHP
<?php
$db->prepare($query, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
?>

Using the query result will look like this:

PHP
<?php
$row0 = $result->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_ABS, 0);
?>

Search and commands