This function can be useful when iterating over the result set to impose
a custom order or rewind the result set when iterating multiple times.
<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
$query = "SELECT Name, CountryCode FROM City ORDER BY Name LIMIT 15,4";
$result = $mysqli->query($query);
/* Iterate the result set in reverse order */
for ($row_no = $result->num_rows - 1; $row_no >= 0; $row_no--) {
$result->data_seek($row_no);
/* Fetch single row */
$row = $result->fetch_row();
printf("City: %s Countrycode: %s\n", $row[0], $row[1]);
}
/* Reset pointer to the beginning of the result set */
$result->data_seek(0);
print "\n";
/* Iterate the same result set again */
while ($row = $result->fetch_row()) {
printf("City: %s Countrycode: %s\n", $row[0], $row[1]);
}
The above examples will output:
City: Acmbaro Countrycode: MEX
City: Abuja Countrycode: NGA
City: Abu Dhabi Countrycode: ARE
City: Abottabad Countrycode: PAK
City: Abottabad Countrycode: PAK
City: Abu Dhabi Countrycode: ARE
City: Abuja Countrycode: NGA
City: Acmbaro Countrycode: MEX