Prev | Current Page 657 | Next

W. Jason Gilmore

"Beginning PHP and MySQL: From Novice to Professional"


Its prototype follows:
boolean sqlite_next(resource result)
584 CHAPTER 22 ?–  SQLITE
Moving the Result Set Pointer Backward
The sqlite_rewind() function moves the result set pointer back to the first row,
returning FALSE if no rows exist in the result set and TRUE otherwise. Its prototype
follows:
boolean sqlite_rewind(resource result)
Moving the Result Set Pointer to a Desired Location
The sqlite_seek() function moves the pointer to a desired row number, returning
TRUE if the row exists and FALSE otherwise. Its prototype follows:
boolean sqlite_seek(resource result, int row_number)
Consider an example in which an employee of the month will be randomly
selected from a result set consisting of the entire staff:
$sqldb = sqlite_open("corporate.db");
$results = sqlite_query($sqldb, "SELECT empid, name FROM employees");
// Choose a random number found within the range of total returned rows
$random = rand(0,sqlite_num_rows($results)-1);
// Move the pointer to the row specified by the random number
sqlite_seek($results, $random);
// Retrieve the employee ID and name found at this row
list($empid, $name) = sqlite_current($results);
echo "Randomly chosen employee of the month: $name (Employee ID: $empid)";
sqlite_close($sqldb);
?>
This returns the following (this shows only one of three possible outcomes):
Randomly chosen employee of the month: Ray Fox (Employee ID: 3)
One point of common confusion that arises in this example regards the starting
index offset of result sets.


Pages:
645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669