mysql_fetch_array($result)

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
the-griff
Forum Newbie
Posts: 1
Joined: Sat Feb 22, 2003 10:37 pm

mysql_fetch_array($result)

Post 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
josa
Forum Commoner
Posts: 75
Joined: Mon Jun 24, 2002 4:58 am
Location: Sweden

Post 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
Post Reply