0

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>";
  }
?>

1 Answer 1

3

Use a JOIN.

SELECT e.*, c.name as competition_name FROM events e LEFT JOIN competitions c on c.id = e.competition_id
Sign up to request clarification or add additional context in comments.

1 Comment

First one worked. I had to change some stuff in the table so it won't overlap. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.