Name

mysql_data_seek( )

Synopsis

mysql_data_seek(connection, row)

Use this in conjunction with the mysql_fetch_row( ) function to change the current row being fetched to the one specified in the second argument of this function. The connection identifier is given as the first argument. The function returns true if it’s successful; it returns false if it’s unsuccessful.

...
$sql_stmnt = "SELECT wrid, clientid, description
              FROM workreq";
$results = mysql_query($sql_stmnt);
$count = mysql_num_rows($results);
mysql_data_seek($results, $count - 6);
$row = mysql_fetch_row($results);
while($row = mysql_fetch_object($results)) {
  print "WR-" . $row->wrid . " Client-" . $row->clientid .
         " - " . $row->description .  "\n";
}
...

In this script excerpt, the SQL statement is selecting the work request identification numbers for all rows in the table. The results set is stored in $results. Using mysql_num_rows() function, the number of rows is determined and placed in the $count variable. To be able to display only the last five work requests, the script calls mysql_data_seek( ). The results set is given as the first argument. In order to get the first row of a results set, the offset would be set to 0: so if a results set contains only one row, the row count of 1 less 1 would need to be given as the second argument of mysql_data_seek( ). For the example here, to get the last five records of the results set, the number of rows is reduced by six to move the pointer to the row before the fifth-to-last ...

Get MySQL in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.