Reply
nesting queries
Old 03-27-2005, 03:37 PM nesting queries
Junior Talker

Posts: 4
is it possible to nest queries?

i want this code to go through a database and select each distinct entry in a column of a table(column=Category, table=Link_Info). When it pull eachs one of these categories, I want it to run another query for each one of the distinct values in the column.

the code below works to pull each distinct value
PHP Code:
<?php
include("XXXXX.php");

  
$connection mysql_connect($host,$user,$password)
       or die (
"couldn't connect to server");
  
$db mysql_select_db($database,$connection)
       or die (
"Couldn't select database");
  
$query "SELECT DISTINCT Category FROM Link_Info ORDER BY Category";
  
$result mysql_query($query)
       or die (
"Couldn't execute query.");

  
/* create form containing selection list */
 
  
while ($row mysql_fetch_array($result))
  {
     
extract($row);
     echo 
"$Category<BR>\n";
  }
 
?>
and this code below will display the information that I want

PHP Code:
<?php

include("XXXXX.php");
 
 
$connection mysql_connect($host$user$password)
  or die (
"oops, can't reach server");
 
$db mysql_select_db($database$connection)
      or die (
"oops, can't reach database");
  
/* Display results in a table: ArtsHumanities */  
  
$query_ArtsHumanities "SELECT * from Link_Info WHERE Category='Arts & Humanities' ORDER BY Link_Display";
  
$result mysql_query($query_ArtsHumanities)
       or die (
"Couldn't execute query.");

  echo 
"<p class='categorytitle'>Arts &amp; Humanities<a name='Arts'></a></p>";
  echo 
"<table>";
    while (
$row mysql_fetch_array($result))
  {
     
extract($row);
           echo 
"<tr> \n
           <td><a href='$Link_URL' target='blank'>$Link_Display</a></td></tr>\n
     <tr><td class='link_description'><ul><li>$Link_Description</li></td></tr></tr>\n"
;
     
     echo 
"<tr><td></td></tr>\n";
  }
  echo 
"</table><br>\n";

I tried to combine them into the code below, but i'm not getting any results returned. all that i am seein is a blank page(except for the html that is written outside of the php).

PHP Code:
<?php
include("XXXXX.php");

  
$connection mysql_connect($host,$user,$password)
       or die (
"couldn't connect to server");
  
$db mysql_select_db($database,$connection)
       or die (
"Couldn't select database");
  
$query "SELECT DISTINCT Category FROM Link_Info ORDER BY Category";
  
$result mysql_query($query)
       or die (
"Couldn't execute query.");

  
/* create form containing selection list */
 
while ($row mysql_fetch_array($result))
  {
     
$query_category "SELECT * from Link_Info WHERE Category='$Category' ORDER BY Link_Display";
  
$result mysql_query($query_category)
       or die (
"Couldn't execute query.");

  echo 
"<p class='categorytitle'>$Category<a name='Arts'></a></p>";
  echo 
"<table>";
    while (
$row mysql_fetch_array($result))
  {
     
extract($row);
           echo 
"<tr> \n
           <td><a href='$Link_URL' target='blank'>$Link_Display</a></td></tr>\n
     <tr><td class='link_description'><ul><li>$Link_Description</li></td></tr></tr>\n"
;
     
     echo 
"<tr><td></td></tr>\n";
  }
  echo 
"</table><br>\n";
  }
  
?>
any ideas about why this isn't working, or better ways to do what i'm trying to do?

thanks for the help.
artist-ink is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
     
Old 03-27-2005, 06:35 PM
Experienced Talker

Posts: 36
The problem is the second while() where you are overwriting the $row variable from the first while(). What exactly are you trying to do anyway ? If you need to solve problems this way then there is a flaw in your logic or table design. But from what I can gather you want to order your results by two columns ? You can do that: order by columnA, columnB
tress is offline
Reply With Quote
View Public Profile
 
Old 03-28-2005, 07:27 AM
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
The way I see it, you have a categories table, and a link_info table, and you want to echo out all the links, keeping them in their categories. I would select all the links at once, joined with their category information, and sort by category. Then I would loop over the resulting list, echo each link in turn, and check to see when we change to a new category in the list, at which point echo the right stuff to display the category etc.
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';
Scribble Pad MOD for phpBB (aka MSN handwriting for forums)
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Old 03-28-2005, 05:14 PM
Junior Talker

Posts: 4
thanks for the input everyone. with some help, i figured out what i need to do. see below:


PHP Code:
while ($row mysql_fetch_array($result))
 {
    
$Category $row['Category'];
 
$query_category "SELECT * from Link_Info WHERE Category='$Category' ORDER BY Link_Display";
   
$result2 mysql_query($query_category)
      or die (
"Couldn't execute query.");

  echo 
"<p class='categorytitle'>$Category<a name='$Category'></a></p>";
  echo 
"<table>";
   while (
$row mysql_fetch_array($result2)) 
artist-ink is offline
Reply With Quote
View Public Profile
 
Old 04-19-2008, 02:52 PM Re: nesting queries
Junior Talker

Posts: 4
Name: dragon
cool!!!!!!
__________________
ZRevolution | DbzForce | Dbz
dbzanime is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to nesting queries
 

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

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




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.12701 seconds with 13 queries