Run query inside a loop statement

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
princeofvegas
Forum Newbie
Posts: 11
Joined: Wed Jun 30, 2010 1:21 am

Run query inside a loop statement

Post by princeofvegas »

Basically what I am trying to do is this. I have results that I am fetching from a query and displaying on my site. I want to be able to take one of the results from that query and do another query on a seperate table for results that will be displayed in the same loop.

What I have:

Code: Select all

while($searchrow = mysql_fetch_array($searchresult)){
	$business_id = $searchrow['business_id'];
}
What I want:

Code: Select all

while($searchrow = mysql_fetch_array($searchresult)){
	$business_id = $searchrow['business_id'];
		$bizsearch = mysql_query("SELECT * FROM businesses WHERE business_id='$business_id'");
		while($bizrow = mysql_fetch_array($bizsearch)){
		$business_name = $bizrow['business_name'];
		$business_phone = $bizrow['business_phone'];
}
I hope I am not asking the impossible and have made it clear what I am looking for. Thank you in advance for any help.
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: Run query inside a loop statement

Post by Jade »

Other than missing a parenthesis to end the first loop I don't see anything wrong with this. Are you getting an error?
princeofvegas
Forum Newbie
Posts: 11
Joined: Wed Jun 30, 2010 1:21 am

Re: Run query inside a loop statement

Post by princeofvegas »

The parentheses was there, I must not have copied it. I actually just got it working with this:

Code: Select all

while($searchrow = mysql_fetch_array($searchresult)){
	$biz_info_query = mysql_query("SELECT * FROM businesses WHERE business_id={$searchrow['business_id']}");
  		while ($bizrow = mysql_fetch_array($biz_info_query)) {
		}
}
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: Run query inside a loop statement

Post by Jade »

It really should be:

Code: Select all

while($searchrow = mysql_fetch_array($searchresult)){
        $biz_info_query = mysql_query("SELECT * FROM businesses WHERE business_id='" . $searchrow['business_id'] . "'");
                while ($bizrow = mysql_fetch_array($biz_info_query)) {
                        //do stuff
                }
}

William Manley
Forum Newbie
Posts: 4
Joined: Wed Jun 30, 2010 3:46 pm

Re: Run query inside a loop statement

Post by William Manley »

Why do two separate queries at all (probably more depending on number of search results.) You can do a JOIN statement easily. I'd write it for you if I could see the query your using for $searchresults.
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: Run query inside a loop statement

Post by Jade »

It depends on your table structure and what you intend to do with your results... although I agree, most times you can just use joins.
User avatar
Payton
Forum Commoner
Posts: 33
Joined: Sun Dec 06, 2009 4:03 pm

Re: Run query inside a loop statement

Post by Payton »

Are you doing anything to filter the search query? I suggest mysql_real_escape_string. You'll probably want to do more to filter it, though.
Post Reply