Jump to content


Photo

newbie help


  • Please log in to reply
4 replies to this topic

#1 rizzah00

rizzah00
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 08 August 2003 - 06:10 AM

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?

#2 pallevillesen

pallevillesen
  • Members
  • PipPipPip
  • Advanced Member
  • 135 posts
  • LocationDenmark

Posted 08 August 2003 - 08:10 AM

select o.user_id, t.team_name from owners u, teams tWHERE 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.
Palle Villesen, www.birc.dk [br]Bioinformatics Research Center

#3 rizzah00

rizzah00
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 08 August 2003 - 03:00 PM

much thanks, ive left mysql for a year now, trying to get everything to come back :)

#4 rizzah00

rizzah00
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 08 August 2003 - 04:06 PM

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 "&nbsp;";    echo "{$row[\'email\']}";    echo "&nbsp;";   echo "{$row[\'aim\']}";      echo "<br>";   }  ?>

any help would be appreciated

#5 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 09 August 2003 - 05:24 PM

Remove the comma from between t.location and from.

hth
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users