-2

I have 2 tables: "media" and "rating"

I need to retrieve all data from both tables where "media" has a title, description and a image. "rating" only stores the media id and a vote (1 - 5).

My problem is that when i try to retrieve data and the "rating" table is empty then no data is shown and if there are more votes in the "rating" table than in the "media" table then it show duplicates of the "media".

<?php
$query  = "SELECT * ";
$query .= "FROM media, rating ";
$query .= "WHERE media.id = media_fkey";
$result = mysqli_query($link, $query);
if (!$result) {
    die("Database query failed.");
}

?>

I hope that this makes sense. :-)

Thank you in advance!

// René

  • possible duplicate of [SQL getting data from multiple tables](http://stackoverflow.com/questions/7693181/sql-getting-data-from-multiple-tables) – 19greg96 Oct 23 '14 at 10:45

1 Answers1

1

try this

$query = "SELECT * FROM media AS m JOIN rating AS r ON m.id = r.media_fkey"

$result = mysqli_query($link, $query);

if (!$result) {
    die("Database query failed.");
}

or you can use the field name with object names other than *

SELECT m.id, m.data, r.media_fkey......
Arun
  • 3,640
  • 7
  • 44
  • 87