我的代码:
$stmt = $conn->prepare("SELECT tmdb_movies.movie_title, images.image_url
FROM tmdb_movies
JOIN images ON images.images_tmdb_id=tmdb_movies.tmdb_id
GROUP BY tmdb_movies.movie_title,images.image_url");
// 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
for($x=0, $n=count($result); $x < $n; $x++){
echo'
'.$result[$x]["movie_title"].' - <img src="'.$result[$x]["image_url"].'"/>
';
}
示例:如何回应数据
// SOME Stuff Here
The Dark Knight: - <img src="sdfsdfds.jpg"/>
// MORE STUFF HERE
// SOME Stuff Here
The Dark Knight: - <img src="sdfsdfds.jpg"/>
// MORE STUFF HERE// SOME Stuff Here
The Dark Knight: - <img src="sdfsdfds.jpg"/>
// MORE STUFF HERE
我希望如何回应数据
// Some Stuff Here
The Dark Knight - <img src="sdfsdfds.jpg"/> <img src="fdfgfdd.jpg"/> <img src="sdfs.jpg"/>
// More Stuff Here
我正在使用一对多关系SQL表,两个表:
tmdb_movies和图片
答案 0 :(得分:3)
两个选项
For循环中的条件
for($x=0, $n=count($result); $x < $n; $x++){
{
If ($x == 0 )
{then echo $result[$x]["movie_title"];}
echo ' - <img src="'.$result[$x]["image_url"].'"/>';
}
GROUP_CONCAT
SELECT tmdb_movies.movie_title, Group_concat(images.image_url, ', ')
FROM tmdb_movies
JOIN images ON images.images_tmdb_id=tmdb_movies.tmdb_id
GROUP BY tmdb_movies.movie_title,images.image_url;
以上将为您提供单个记录中的数据,使用逗号分隔值,您可以拆分,然后创建标记
我会选择第一个选项