wepnop Posted March 4, 2011 Share Posted March 4, 2011 Now i use a very complex normal for, how i can use a foreach? Quote Link to comment https://forums.phpfreaks.com/topic/229560-foreach-for-iterating-mysql-result-or-other-alternatives/ Share on other sites More sharing options...
Muddy_Funster Posted March 4, 2011 Share Posted March 4, 2011 could you try again, and maybe give us some more info? What do you want to do? Quote Link to comment https://forums.phpfreaks.com/topic/229560-foreach-for-iterating-mysql-result-or-other-alternatives/#findComment-1182700 Share on other sites More sharing options...
wepnop Posted March 4, 2011 Author Share Posted March 4, 2011 could you try again, and maybe give us some more info? What do you want to do? Mmm, i you dont need more info, i think. I just want to iterate a mysql result in a foreach, or in a batter way than this: # Itera en totes les ciutats for ($i=0; $i<=mysql_num_rows($result)-1; $i++) { $object = mysql_fetch_object($result); if (mysql_num_rows($result)-1 == $i) { break; } mysql_data_seek($result, $i+1); } Quote Link to comment https://forums.phpfreaks.com/topic/229560-foreach-for-iterating-mysql-result-or-other-alternatives/#findComment-1182705 Share on other sites More sharing options...
Muddy_Funster Posted March 4, 2011 Share Posted March 4, 2011 while ($row = mysql_fetch_assoc($result)){ //do what you want for each row returned } is the normal way of doing this, so long as by iterate you are reffering to running through the returned result set from the query, and not to repeating the query it's self to obtain a specific result set. You see, it's still a bit ambiguous. Quote Link to comment https://forums.phpfreaks.com/topic/229560-foreach-for-iterating-mysql-result-or-other-alternatives/#findComment-1182708 Share on other sites More sharing options...
wepnop Posted March 4, 2011 Author Share Posted March 4, 2011 while ($row = mysql_fetch_assoc($result)){ //do what you want for each row returned } is the normal way of doing this, so long as by iterate you are reffering to running through the returned result set from the query, and not to repeating the query it's self to obtain a specific result set. You see, it's still a bit ambiguous. Thats what i want. That and the way of use foreach for the same task. Quote Link to comment https://forums.phpfreaks.com/topic/229560-foreach-for-iterating-mysql-result-or-other-alternatives/#findComment-1182711 Share on other sites More sharing options...
PFMaBiSmAd Posted March 4, 2011 Share Posted March 4, 2011 Sorry to jump in, but I'm glad you finally shared what you are doing, because the code you are using now is very very inefficient - 1) It is re-evaluating mysql_num_rows() every pass through the for() statement, 2) It is re-evaluating mysql_num_rows() inside the loop to break from the loop, but that is pointless because that is what the loop is already doing, 3) It is using mysql_data_seek() inside the loop, but that is pointless because mysql_fetch_object() advances the result pointer. You can replace all that with the following that just about every php/mysql book or tutorial would have shown - while($object = mysql_fetch_object($result)){ } Quote Link to comment https://forums.phpfreaks.com/topic/229560-foreach-for-iterating-mysql-result-or-other-alternatives/#findComment-1182712 Share on other sites More sharing options...
wepnop Posted March 4, 2011 Author Share Posted March 4, 2011 Sorry to jump in, but I'm glad you finally shared what you are doing, because the code you are using now is very very inefficient - 1) It is re-evaluating mysql_num_rows() every pass through the for() statement, 2) It is re-evaluating mysql_num_rows() inside the loop to break from the loop, but that is pointless because that is what the loop is already doing, 3) It is using mysql_data_seek() inside the loop, but that is pointless because mysql_fetch_object() advances the result pointer. You can replace all that with the following that just about every php/mysql book or tutorial would have shown - while($object = mysql_fetch_object($result)){ } I learned that way in a host that haved a very old PHP, at minium php4. And as far i know, removing data seek, etc, make break the all thing. Anyway i will use the while or the foreach, if exist. Quote Link to comment https://forums.phpfreaks.com/topic/229560-foreach-for-iterating-mysql-result-or-other-alternatives/#findComment-1182713 Share on other sites More sharing options...
wepnop Posted March 4, 2011 Author Share Posted March 4, 2011 any ideas for using the forech? Quote Link to comment https://forums.phpfreaks.com/topic/229560-foreach-for-iterating-mysql-result-or-other-alternatives/#findComment-1182726 Share on other sites More sharing options...
Muddy_Funster Posted March 4, 2011 Share Posted March 4, 2011 kind of - but it's non-standard practice, and you would need to be barking to use it in a normal scenrio. $result_set = array(); $resultset = mysql_fetch_assoc($result); foreach ($result_set as $field => $value){ //do what you want to do } using this kind of loop here is so bizzare I don't even know if that will actualy work Quote Link to comment https://forums.phpfreaks.com/topic/229560-foreach-for-iterating-mysql-result-or-other-alternatives/#findComment-1182730 Share on other sites More sharing options...
PFMaBiSmAd Posted March 4, 2011 Share Posted March 4, 2011 There's no way to use a foreach() loop because a foreach() loop iterates over an array. Queries don't return an array, they return a result resource that must be accessed using mysql_fetch_xxxxx() or mysql_result() statements. Quote Link to comment https://forums.phpfreaks.com/topic/229560-foreach-for-iterating-mysql-result-or-other-alternatives/#findComment-1182731 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.