Name

mysql_row_seek( )

Synopsis

MYSQL_ROW_OFFSET mysql_row_seek(MYSQL *result, 
                                MYSQL_ROW_OFFSET offset)

Use this to move the pointer of a result set to the row given as the second argument of the function. The pointer given must use the MYSQL_ROW_OFFSET structure. Use a function such as mysql_row_tell( ) to determine the offset in the proper format.

...
 MYSQL_ROW_OFFSET special_location;
 while((row = mysql_fetch_row(result)) != NULL)
   {
    if(strcmp(row[1], "1000") =  = 0)
      {
       special_location = mysql_tell_row(result);
       continue;
      }
    if(!mysql_more_results(mysql))
      {
       mysql_row_seek(result, special_location);
       printf("%s (%s) \n", row[1], row[0]);
       break;
      }
    printf("%s (%s) \n", row[1], row[0]);
   }
...

In this example, a list of clients has been retrieved, but the developer wants the row with a client identification number of 1000 to be displayed last. So, an if statement is used to check for the special record. When it finds the row for which it’s looking, the mysql_row_tell( ) function is used to make a note of the point in the results set in which it was found. The remainder of the while statement in which the row is to be printed is then skipped. Using the mysql_more_results( ) function, another if statement watches for the end of the results set. If it determines that there are no more rows in the results set to print, it will move the pointer back to the special client using the mysql_row_seek( ) function and the pointer saved with mysql_row_tell( ), print out that particular row’s data, and then ...

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.