Jump to content

help with query


rizzah00

Recommended Posts

hello, i\'m quering the database

here is my code


<?php



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

mysql_select_db("database");

$query = "select r.id, r.week, r.awayteam, r.hometeam, t.team_id FROM results r, teams t";

$result = mysql_query($query); 

$numrows = mysql_num_rows($result); 

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

echo "<tr>";

  echo "<td>";

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

     echo "-";

     echo "</td>";

     echo "<td>";

  echo "week";

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

     echo "</td>";

  echo "<td>";

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

     echo "</td>";

  echo "<td>";

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

     echo "</td>";

}

?>

I get the query results and right now there are only two entries into that table, yet whenever i query the database i get like 30 some results but its just the two that are in the database alternating any help would be appreciated...

Link to comment
Share on other sites

You have to join the two tables results and teams

 

like

 

WHERE t.team_id = r.team_id

 

or something similar...

 

I don\'t see why you get team_id anyway (since awayteam and hometeam probably are team ids anyway ?)

 

But if you insist, then you must join the two tables in a WHERE clause or do a straight JOIN... (rtfm on syntax).

 

P.

Link to comment
Share on other sites

Heheh.

 

He\'s gonna have a bitch of a time with this.

 

He has nowhere to link teamID. He has home team and away team in an un-normalised table you cannot use OR statements on a JOIN (where teamID = hometeam or teamID = awayteam.....).

 

3 options:

 

1. Get MySQL 4 so you can do UNIONs

 

2. Do 2 queries that INSERT INTO a temp table on hometeam, then awayteam, then SELECT from the temp table.

 

3. Normalise your tables. Add table team_match (team_matchID, hometeam, awayteam) then do complex queries linking team, match and team_match.

Link to comment
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.