Welcome to WebmasterWorld Guest from 54.159.214.250

Forum Moderators: coopster & jatar k

Looping Tutorial Help (PHP)

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

WebmasterWorld Senior Member 10+ Year Member



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 :-).

Cheers

Richard

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

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



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 Jun 11, 2002 (gmt 0)

10+ Year Member



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

10+ Year Member



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");
while($row=mysql_fetch_row($result)){
do_something_with_row();
}

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).

 

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month