Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Looping Tutorial Help (PHP)

3:54 pm on Jun 10, 2002 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Dec 6, 2001
votes: 0

Hello I am looking for a tutorial on how to loop until EOF.

I have no problem getting one row from the db with:

<?php printf(mysql_result($pResult,0,"UserID"));?>

I have tried to loop, but with no success, I did however manage to grind the server to a halt as my script produced and infinite loop :-).



4:25 pm on June 10, 2002 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lorax is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Mar 31, 2002
votes: 4

There are a number of ways to accomplish this here's one example:

$num_rows = mysql_num_rows($result);
for ($i = 0; $i <= $num_rows; $i++) {
print mysql_fetch_array($result);

1:42 pm on June 11, 2002 (gmt 0)

Preferred Member

10+ Year Member

joined:Apr 17, 2002
votes: 0

Here is one ukgimp its not the one I was looking for but i am not at home and dont have my bookmarks.
3:53 pm on June 11, 2002 (gmt 0)

Preferred Member

10+ Year Member

joined:June 7, 2002
votes: 0

The functions mysql_fetch_row(), mysql_fetch_array() and mysql_fetch_object() return one row from the result, and then move the pointer on. If there are no more rows to fetch, it returns false. This means you can you use a very simple while loop:

$result=mysql_query("SELECT * FROM sometable");

This will automatically terminate when the last row has been processed.

The difference between mysql_fetch_row() and mysql_fetch_array() is that the first returns the results in a numeric array ($row[0], $row[1] etc), while the latter returns a the results an array containing both numeric and associative keys ($row["name"], $row["email"] etc). mysql_fetch_object() returns an object ($row->name, $row->email etc).


Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members