Select more then 1 table

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

Post Reply
nite4000
Forum Contributor
Posts: 209
Joined: Sun Apr 12, 2009 11:31 am

Select more then 1 table

Post by nite4000 »

Hey everyone.. its been a while but I am back for a lil help.

I have 2 tables

Packackage
id,name

I have a plans table
id,package_id,plan,amount,duration

I want to display the package name then below that have it show the plan name,amount and duration that goes with that package name

EX I have packages called Alpha,Beta
I then have 3 plans in plan table and 2 of them have the id of the Alpha package and other with the Beta.

It should come out like this

Alpha
Plan 1 ect...
plan 2 ect

Beta
Plan 1 ect

What I have so far is listed below.

Code: Select all

<table width="396">
						
						 <?php $q=mysql_query("Select * from packages")or die (mysql_error());
						 	 while($pack=mysql_fetch_array($q,MYSQL_ASSOC)) {

						 $r=mysql_query("select * from plans")or die(mysql_error());
	 while($plan=mysql_fetch_array($q,MYSQL_ASSOC)) {
	
	 echo'<tr> <td>'.$pack['plan_name'].'</td></tr>
	 <tr><td>'.$plan['name'].'</td><td>'.$plan['amount'].'</td><td>'.$plan['rate'].'</td><td>'.$plan['length'].'</td></tr>';
	}
	}
	?>						
						
						
						
					  </table>
If anyone can give some help I would appriciate it.

Thanks
marty pain
Forum Contributor
Posts: 105
Joined: Thu Jun 11, 2009 5:32 am
Location: Essex

Re: Select more then 1 table

Post by marty pain »

You need to look into SQL JOINs

I think what you need is this, but do some reading to check, I haven't used them for a while.

Code: Select all

$query = "SELECT package.name AS packageName,
                             plan.name AS planName,
                             plan.duration,
                             plan.amount
                FROM package INNER JOIN plan ON (package.id = plan.package_id)";
Post Reply