Yesterday, I ran into a problem that seems to stem from an unusual
behavior of mysql_fetch_row() and mysql_fetch_assoc(). Specifically, I
have a query that selects all of the id's of fields matching certain
criteria, and I want to then fetch the complete results of a certain
subset of those fields, depending on the current result page and number of
records to show per page. The original code looked like this:
mysql_data_seek($this->query, ($this->page - 1) * $this->limit);
for ($i = 0; $i < $this->limit; $i++) {
$result = mysql_fetch_assoc($this->query);
// The get() method fetches the complete record
$this->results[] = $this->get($result[$this->key]);
}
Unfortunately, this did not work as expected. This either returned the
same row every time, or, if I wrote the loop differently, returned one
result and then quit. It appears that after calling mysql_data_seek,
mysql_fetch_assoc does not advance the result pointer. However, I couldn't
find this documented anywhere.
My temporary solution also ran into PHP's apparent lack of a C-style break
or Perl-style last statement. Does such a thing exist, or am I correct in
having to write something like:
for ($i = 0; $i < $this->limit; $i++) {
// blah blah blah
// Force break out of loop
if ($row = mysql_num_rows($this->query) {
$i = $this->limit;
}
}
Thanks in advance--
Jesse S. Bangs ja****@u.washington.edu
http://students.washington.edu/jaspax/
http://students.washington.edu/jaspax/blog