Jump to content

Archived

This topic is now archived and is closed to further replies.

rizzah00

newbie help

Recommended Posts

I have two tables in my database

-owners

-teams

 

in my owners table i have fields for email user_id name and team_id

in my teams table i have fields for team_name and team_id

 

I want to do a query that will replace the owners team_id with the teams team_name, i have linked everything correctly, what would the query be? how would i go about displaying this on a page with php?

Share this post


Link to post
Share on other sites


select o.user_id, t.team_name from owners u, teams t

WHERE o.team_id = t.team_id;

 

Would join the two tables correctly together. If you have entered an owner which have a nonexisting team_id (i.e. the team_id number does not exist in the teams table) - then this owner will NOT be returned...

 

If you want them to come back as well you must do a left join:

 


select o.user_id, t.team_name from owners u LEFT JOIN  teams t ON o.team_id = t.team_id;

 

Here all owners will be returned having a team_name of NULL if it is not defined in the teams table...

 

P.

Share this post


Link to post
Share on other sites

okay i finally got everything set to where i want it i tweaked some code to try to get another cell from the query and i get this error

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/styckx/public_html/maddenworld/owners.php on line 25

 

Here\'s my code


<?php  



mysql_connect("localhost", "user", "pass");  

mysql_select_db("database");  

$query = "select o.email, t.name, o.aim, t.location, from owners o, teams t WHERE o.team_id = t.team_id"; 

 

$result = mysql_query($query);  

while($row = mysql_fetch_array($result)){  





     echo "{$row[\'location\']}";

  echo "nbsp;";

  echo "{$row[\'name\']}";

  echo " "; 

  echo "{$row[\'email\']}"; 

  echo " ";

  echo "{$row[\'aim\']}";

     echo "<br>";



 

}  

?> 

 

any help would be appreciated

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.