views:

37

answers:

1

I've got two tables from which I need to extract information, but the data from the second table depends on the information I get from the first one. Is there an easy way to handle this?

<?php
  mysql_connect('localhost', 'root', 'root') or die(mysql_error());
  mysql_select_db('stadium') or die(mysql_error());

  $result = mysql_query("SELECT * FROM events");

  $result2 = mysql_query("SELECT name FROM competitions WHERE id='$row[competition_id]' ");

  while($row = mysql_fetch_array($result)) {
    echo "<tr id=\"" . $row['id'] . "\"> \n<td>" . $row['name'] . "</td>";
    echo "<td>" . $row['competition_id'] . "</td>";
    echo "<td>" . $row['date'] . "</td></tr>";
  }
?>
+3  A: 

Use a JOIN.

SELECT e.*, c.name as competition_name FROM events e LEFT JOIN competitions c on c.id = e.competition_id
Karsten
First one worked. I had to change some stuff in the table so it won't overlap. Thanks!
Norbert