[advanced search]
 

Go Back   NamePros.com > Discussion > Web Design & Development > Programming

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


Closed Thread
 
LinkBack Thread Tools
Old 10-27-2005, 02:32 PM   #1 (permalink)
New Member
 
Join Date: Oct 2005
Posts: 6
12.00 NP$ (Donate)

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??

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, 02:37 PM   #2 (permalink)
RJ
NamePros Founder

Administrator

 
Join Date: Feb 2003
Location: Bay Area, CA
Posts: 13,173
104,201.68 NP$ (Donate)

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
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.
RJ is offline  
Old 10-27-2005, 04:13 PM   #3 (permalink)
Senior Member
 
Crusader's Avatar
 
Join Date: Aug 2003
Location: Canada
Posts: 1,293
1,264.40 NP$ (Donate)

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.
__________________

Near Fantastica
| Matthew Good - Vancouver
>> Do you Frawlik? <<
Crusader is offline  
Old 10-28-2005, 06:23 AM   #4 (permalink)
New Member
 
Join Date: Oct 2005
Posts: 6
12.00 NP$ (Donate)

murder2 is an unknown quantity at this point


Thumbs up

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..

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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Site Sponsors
Advertise your business at NamePros

All times are GMT -7. The time now is 03:39 AM.


Powered by: vBulletin® Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0
Template-Modifications by TMS
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85