NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming
Reload this Page Subqueries for each row? (php+mysql)

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.

Advanced Search


Closed Thread
 
LinkBack Thread Tools
Old 10-27-2005, 03:32 PM THREAD STARTER               #1 (permalink)
New Member
Join Date: Oct 2005
Posts: 6
murder2 is an unknown quantity at this point
 



Question Subqueries for each row? (php+mysql)


I want to do a query, and for each row in that query, do another query based on an attribute value (foreign key) in that row. I can't get it to work, and I am wondering if this is possible??
????: NamePros.com http://www.namepros.com/programming/135476-subqueries-for-each-row-php-mysql.html

code looks something like this
Code:
$query = "SELECT `id`, `name`, `foreign_key` FROM `the_table`";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
while ($row = mysql_fetch_assoc($result)) {
  .....
  $query2 = "SELECT `this`, `that` FROM `table2` WHERE `thing` = $row['foreign_key']";
  $result2 = mysql_query($query2) or die('Query failed: ' . mysql_error());
  while ($row2 = mysql_fetch_assoc($result2)) {
   .......
  }
  mysql_free_result($result2);
}
any comments, hints or tips on how to solve this, please?
murder2 is offline  
Old 10-27-2005, 03:37 PM   #2 (permalink)
RJ
NamePros Webmaster


 
RJ's Avatar
Join Date: Feb 2003
Posts: 12,930
RJ Has achieved greatnessRJ Has achieved greatnessRJ Has achieved greatnessRJ Has achieved greatnessRJ Has achieved greatnessRJ Has achieved greatnessRJ Has achieved greatnessRJ Has achieved greatnessRJ Has achieved greatnessRJ Has achieved greatnessRJ Has achieved greatness
 



Find Marrow Donors! Cystic Fibrosis Parkinson's Disease
What error are you getting?

I see:
WHERE `thing` = $row['foreign_key']"

If foreign_key is not strictly a numerical value, you need to at least put 'apostrphes' around it.
__________________
@DomainBuyer facebook
RJ is offline  
Old 10-27-2005, 05:13 PM   #3 (permalink)
Senior Member
 
Crusader's Avatar
Join Date: Aug 2003
Location: Canada
Posts: 1,257
Crusader is just really niceCrusader is just really niceCrusader is just really niceCrusader is just really nice
 



You should note that doing it the way you're doing now will result in a large amount of queries. I highly suggest you use an object oriented function to save yourself some server load. Loops $i++ are the key.
Crusader is offline  
Old 10-28-2005, 07:23 AM THREAD STARTER               #4 (permalink)
New Member
Join Date: Oct 2005
Posts: 6
murder2 is an unknown quantity at this point
 




foreign_key is an integer yes.

And I am aware of that this is not an perfect solution, but the first query will only retur a couple of rows, so in this specific case there's no great efficiency-saver in solving this otherwise..
????: NamePros.com http://www.namepros.com/showthread.php?t=135476

But I solved it, I just made a new variable before the query2
Code:
$num = $row['foreign_key']
$query2 = .... WHERE `thing` = $num
And it worked just the way I wanted it to.
murder2 is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


Liquid Web Smart Servers  
All times are GMT -7. The time now is 07:48 AM.

Managed Web Hosting by Liquid Web
Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger