Welcome to Dream.In.Code
Become a PHP Expert!

Join 136,905 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,738 people online right now. Registration is fast and FREE... Join Now!




joining two tables

 
Reply to this topicStart new topic

joining two tables

chriscross
8 Mar, 2008 - 03:12 PM
Post #1

New D.I.C Head
*

Joined: 8 Mar, 2008
Posts: 5

i have two table, one called pages and the other subpages. I'm trying to make the records of subpages show under the record from the table pages with the same id.

CODE
<?

$page = $_GET['page'];








                 $SearchResult=mysql_query("SELECT * FROM pages,subpages WHERE pages.site_id=subpages.site_id AND pages.site_id='25'") or die(mysql_error());
    While($row = mysql_fetch_object($SearchResult)) {
$site_id = $row->$site_id;



$dazzle_id = $row->dazzle_id;
$dazzle_pagname = $row->dazzle_pagename;

            
            
                        
            ?>
          <table width="149" border="0" cellspacing="0" cellpadding="3">
  <tr>
                            <td colspan="2">•<a href="index.php?page=<? echo $dazzle_id; ?>"><? echo $dazzle_pagename; ?></a>'; </td>
                    </tr>
                          <tr>
                            <td width="20"> </td>
                            <td width="117"><?





            echo '•<a href="index.php?subpage='.$row->sub_id.'">'.$sub_name.'</a>';
            
                      
            ?></td>
                    </tr>
                  </table>
                  <? } ?>


I'd greatly appreciate your help.

Thank-you
User is offlineProfile CardPM
+Quote Post

SpaceMan
RE: Joining Two Tables
8 Mar, 2008 - 08:22 PM
Post #2

D.I.C Regular
Group Icon

Joined: 20 Feb, 2003
Posts: 270

first thing , check id is numeric for security.
inner join = both must exist
left join = one an exist

CODE


if(!is_numeric($_GET['page']))
  exit('page id error');


$SearchResult=mysql_query('SELECT * FROM `pages`
inner join `subpages` using(site_id)
WHERE pages.site_id='.$_GET['page']) or die(mysql_error());

User is offlineProfile CardPM
+Quote Post

chriscross
RE: Joining Two Tables
9 Mar, 2008 - 12:41 AM
Post #3

New D.I.C Head
*

Joined: 8 Mar, 2008
Posts: 5

QUOTE(SpaceMan @ 8 Mar, 2008 - 09:22 PM) *

first thing , check id is numeric for security.
inner join = both must exist
left join = one an exist

CODE


if(!is_numeric($_GET['page']))
  exit('page id error');


$SearchResult=mysql_query('SELECT * FROM `pages`
inner join `subpages` using(site_id)
WHERE pages.site_id='.$_GET['page']) or die(mysql_error());



Thanks for your answer, but still, all i get in the output page is:
QUOTE
•';


This means that it is seeing the data but it won't output the it.
I tried using: $dazzle_pagname = $row->pages.dazzle_pagename;
and also $dazzle_pagname = $row->dazzle_pagename;
but nothing seems to work :S

User is offlineProfile CardPM
+Quote Post

chriscross
RE: Joining Two Tables
9 Mar, 2008 - 12:46 AM
Post #4

New D.I.C Head
*

Joined: 8 Mar, 2008
Posts: 5

I managed ok thank-you

but is it possible to have more than 1 row from the table subpage show while only 1 is shown from the table pages?
thanks

This is the output at the moment:
QUOTE
•Home
•who are we
•Home
•where are we


as you can see it it outputing 2 "HOME" i only want 1.

CODE


                 $SearchResult=mysql_query("SELECT * FROM `pages` inner join `subpages` using (site_id) WHERE pages.site_id='25'") or die(mysql_error());
    While($row = mysql_fetch_object($SearchResult)) {
$site_id = $row->$site_id;
            
                        
            ?>
          <table width="149" border="0" cellspacing="0" cellpadding="3">
  <tr>
                            <td colspan="2">•<a href="index.php?page=<? echo $row->dazzle_id; ?>"><? echo $row->dazzle_pagename; ?></a></td>
                    </tr>
                          <tr>
                            <td width="20">&nbsp;</td>
                            <td width="117">





            •<a href="index.php?subpage=<? echo $row->sub_id; ?>"><? echo $row->sub_name; ?></a>
            
                      
            </td>
                    </tr>
                  </table>
                  <? } ?>


<edit>i managed this also thanks anyways

This post has been edited by chriscross: 9 Mar, 2008 - 07:22 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/3/08 09:46PM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month