truegilly Posted March 5, 2007 Share Posted March 5, 2007 Hi people got a quick one here for you, been playing for a while with the mysql_fetch_array function and would like to be able to iterate through the return values rather that pick out certain ones like, $row[7] or $row['rank']. heres my code.... //perform query $query = "SELECT RAFPerId FROM personnel WHERE job !='Station Commander' AND job !='Squadron Commander'"; $result = mysql_query($query); while ($row = mysql_fetch_array($result, MYSQL_NUM)) { $RafSqdCdrId = $row[0]; } as you can see im returning the first result with $row[$i]. Is there a way i can do this like when using a For loop and $i and the iterator, $row ? i have been trying but to little success. thanks for any help !! Truegilly Link to comment https://forums.phpfreaks.com/topic/41286-iterating-through-mysql_fetch_array/ Share on other sites More sharing options...
boo_lolly Posted March 5, 2007 Share Posted March 5, 2007 fetch_array doesn't really work that way. in a while loop, you will be able to call each column, one row at a time. of course, that depends on what your sql query looks like... for your sql query, it looks like you've only chosen to select one column 'RAFPerId'. so, the only thing you can do in your while loop will be the variable $row['RAFPerId']. you can't call on the variable using $row[$i] like you would in a for loop. Link to comment https://forums.phpfreaks.com/topic/41286-iterating-through-mysql_fetch_array/#findComment-200056 Share on other sites More sharing options...
truegilly Posted March 5, 2007 Author Share Posted March 5, 2007 Thanks ! ill have a play Truegilly Link to comment https://forums.phpfreaks.com/topic/41286-iterating-through-mysql_fetch_array/#findComment-200070 Share on other sites More sharing options...
mbtaylor Posted March 5, 2007 Share Posted March 5, 2007 My preferred method is: $query = mysql_query ("SELECT * FROM some_table"); while ($row = mysql_fetch_assoc ($query)) { foreach ($row as $var => $value) { print ("$var = $value<br />"); } } That will print out all elements from your query. You can also use the same to make your associative array elements into variables like: $query = mysql_query ("SELECT * FROM some_table"); while ($row = mysql_fetch_assoc ($query)) { foreach ($row as $var => $value) { $$var = $value; } } Link to comment https://forums.phpfreaks.com/topic/41286-iterating-through-mysql_fetch_array/#findComment-200077 Share on other sites More sharing options...
boo_lolly Posted March 5, 2007 Share Posted March 5, 2007 there is no need to add a foreach loop inside a while loop... they do the exact same thing... you're just using up more resources. Link to comment https://forums.phpfreaks.com/topic/41286-iterating-through-mysql_fetch_array/#findComment-200117 Share on other sites More sharing options...
mbtaylor Posted March 6, 2007 Share Posted March 6, 2007 No they arnt. The while loop is looping through the database rows, the for loop is looping through the current database row. Link to comment https://forums.phpfreaks.com/topic/41286-iterating-through-mysql_fetch_array/#findComment-200668 Share on other sites More sharing options...
Azu Posted March 6, 2007 Share Posted March 6, 2007 there is no need to add a foreach loop inside a while loop... they do the exact same thing... you're just using up more resources. I'm pretty sure the foreach is so that if there is more then 1 row he is searching for in the query, it will display all of them, instead of just one. Link to comment https://forums.phpfreaks.com/topic/41286-iterating-through-mysql_fetch_array/#findComment-200671 Share on other sites More sharing options...
mbtaylor Posted March 6, 2007 Share Posted March 6, 2007 there is no need to add a foreach loop inside a while loop... they do the exact same thing... you're just using up more resources. I'm pretty sure the foreach is so that if there is more then 1 row he is searching for in the query, it will display all of them, instead of just one. The while loop will loop through all rows. What I posted was a solution to what truegilly was wanting to do. I personally use the same to give me a quick idea of all the elements being pulled in from the db. The same could be achieved with print_r which I guess may be more efficient I dont know but less flexible certainly. Link to comment https://forums.phpfreaks.com/topic/41286-iterating-through-mysql_fetch_array/#findComment-200680 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.