-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é

1

1 Answer 1

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......
Sign up to request clarification or add additional context in comments.

1 Comment

@VanDerPrygel: with pleasure :)

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.