I have total 3 tables in PHPMyAdmin:
Movies:
tmdb_moviesCast:
cast. Foreign key iscast_tmdb_idfor tabletmdb_moviescolumntmdb_idscreenshotGenres:
genres. Foreign key isgenres_tmdb_idfor tabletmdb_moviescolumntmdb_idscreenshot
I want to output the following fields:
movie_titlecolumn fromtmdb_moviestableall
genres_namerows fromgenrestableall
cast_namerows fromcasttable.
What is the smallest command to select and echo data? I am not familiar with the join command in SQL.
This is my current code
<?php
$stmt = $conn->prepare("SELECT tmdb_movies.*, GROUP_CONCAT(genres_name) as genres_name
FROM tmdb_movies JOIN genres ON tmdb_movies.tmdb_id=genres.cast_tmdb_id
GROUP BY tmdb_movies.cast_tmdb_id, tmdb_movies.tmdb_id, tmdb_movies.movie_title");
// Then fire it up
$stmt->execute();
// Pick up the result as an array
$result = $stmt->fetchAll();
// Now you run through this array in many ways, for example
foreach ($result as $row) {
print "".$row["movie_title"]." ".$row["genres_name"] ." ".$row["cast_name"] ."<br/>";
}
?>
Explainations :
GROUP_CONCAT => Concat fields from different row in one field
JOIN => you make two table correspond by their key
GROUP BY => group data per those fields
I've done the query I could without actually seeing your tables, so you might want to adapt it if it doesn't work on first try.