Jump to content

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?

Link to comment
https://forums.phpfreaks.com/topic/851-newbie-help/
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.

Link to comment
https://forums.phpfreaks.com/topic/851-newbie-help/#findComment-2833
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

Link to comment
https://forums.phpfreaks.com/topic/851-newbie-help/#findComment-2841
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.