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
princeofvegas
Forum Newbie
Posts: 11 Joined: Wed Jun 30, 2010 1:21 am
Post
by princeofvegas » Wed Jun 30, 2010 1:47 pm
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.
Jade
Forum Regular
Posts: 908 Joined: Sun Dec 29, 2002 5:40 pm
Location: VA
Post
by Jade » Wed Jun 30, 2010 1:49 pm
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
Post
by princeofvegas » Wed Jun 30, 2010 1:58 pm
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)) {
}
}
Jade
Forum Regular
Posts: 908 Joined: Sun Dec 29, 2002 5:40 pm
Location: VA
Post
by Jade » Wed Jun 30, 2010 4:36 pm
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
Post
by William Manley » Wed Jun 30, 2010 5:28 pm
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.
Jade
Forum Regular
Posts: 908 Joined: Sun Dec 29, 2002 5:40 pm
Location: VA
Post
by Jade » Thu Jul 01, 2010 8:03 am
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.
Payton
Forum Commoner
Posts: 33 Joined: Sun Dec 06, 2009 4:03 pm
Post
by Payton » Thu Jul 01, 2010 1:18 pm
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.