Page 1 of 1

mysql_fetch_array($result)

Posted: Sat Feb 22, 2003 10:37 pm
by the-griff
hello i have a page that submits information to the database and then a page to display it and i cant figure out how this loop works

Code: Select all

while ( $myrow = mysql_fetch_array($result) ) {
printf("<table border=0 cellspacing=0 cellpadding=0><tr><td><p>Author : %s</td></tr>
<tr><td><p>Message : %s</p></td></tr>
<tr><td><p>Date : %s</p></td></tr></table><br><hr><br>", $myrow&#1111;"name"], $myrow&#1111;"message"], $myrow&#1111;"time"]);

&#125;
how is it moving to the next row each time theres no "$i++" or anything
thanks for any help


and is there anyway to loop backwards like start at the end and move a row up each time

Posted: Sun Feb 23, 2003 3:14 am
by josa
The first time you call mysql_fetch_array() i fetches the first row in the result set and then moves the cursor to the next row. The second call will return the second row etc, until you've fetched the last row in the set. When the last row is fetched the next call to mysql_fetch_array() will return FALSE and thus break the loop.

If you want to display the rows from the bottom up you'd better rewrite your SQL query so that the result set is reversed. If you insist on letting PHP handle this, which I don't recommend, you could find out how many rows there are in the result set with mysql_num_rows() and then use mysql_result().

/josa