Jump to content

Foreach for iterating mysql result, or other alternatives


wepnop

Recommended Posts

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

}	

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :shrug:

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.