如果这根本没有任何意义,我很抱歉。我创建了一个名为“filmdetails.php”的PHP文件,它将列出给定电影的所有细节:
<?php
require_once 'dbcon_sakila.php';
$film_id = $_GET['filmid'];
$sql = 'SELECT title, description, release_year, rental_duration, rental_rate, length, replacement_cost, rating, special_features, film.last_update, category.name, actor.first_name, actor.last_name
FROM film, film_category, category, film_actor, actor
WHERE film.film_id = film_category.film_id
AND category.category_id = film_category.category_id
AND film.film_id = film_actor.film_id
AND film_actor.actor_id = actor.actor_id
AND film.film_id = ?';
$stmt = $link->prepare($sql);
$stmt->bind_param('i', $film_id);
$stmt->bind_result($title, $description, $release_year, $rental_duration, $rental_rate, $length, $replacement_cost, $rating, $special_features, $last_update, $category_name, $first_name, $last_name);
$stmt->execute();
while ($stmt->fetch()) {
echo '<p>Title: ' . $title . '</p>';
echo '<p>Description: ' . $description . '</p>';
echo '<p>Release year: ' . $release_year . '</p>';
echo '<p>Rental duration: ' . $rental_duration . '</p>';
echo '<p>Rental rate: ' . $rental_rate . '</p>';
echo '<p>Length: ' . $length . '</p>';
echo '<p>Replacement cost: ' . $replacement_cost . '</p>';
echo '<p>Rating: ' . $rating . '</p>';
echo '<p>Special features: ' . $special_features . '</p>';
echo '<p>Last updated: ' . $last_update . '</p>';
echo '<p>Categories: ' . $category_name . '</p>';
echo '<p>First name: ' . $first_name . '</p>';
echo '<p>Last name: ' . $last_name . '</p>';
}
?>
问题在于,当我选择一部特定的电影(在我的浏览器中)时,它会重复自己,但电影中的不同演员。 例如:
Title: ALLEY EVOLUTION
Description: A Fast-Paced Drama of a Robot And a Composer who must Battle a Astronaut in New Orleans
Release year: 2006
Rental duration: 6
Rental rate: 2.99
Length: 180
Replacement cost: 23.99
Rating: NC-17
Special features: Trailers,Commentaries
Last updated: 2006-02-15 05:03:42
Categories: Foreign
First name: KARL
Last name: BERRY
Title: ALLEY EVOLUTION
Description: A Fast-Paced Drama of a Robot And a Composer who must Battle a Astronaut in New Orleans
Release year: 2006
Rental duration: 6
Rental rate: 2.99
Length: 180
Replacement cost: 23.99
Rating: NC-17
Special features: Trailers,Commentaries
Last updated: 2006-02-15 05:03:42
Categories: Foreign
First name: JUDE
Last name: CRUISE
依旧......
如何防止这种情况?我真的需要制作更多的SELECT语句吗?
答案 0 :(得分:1)
因为您在电影和演员之间存在一对多关系,所以您将获得多个结果。每个Actor的一条记录。这是RDBMS的本质。
解决方案是从您的查询中取出actor
表格,并在While
循环中仅使用actor
查询film_id
表格(您可以需要在初始查询中返回)。然后,您可以在actor
查询的结果中单独循环,并将它们添加到您的页面中。
我在PHP上生锈了,但它看起来像是:
<?php
require_once 'dbcon_sakila.php';
$film_id = $_GET['filmid'];
$sql = 'SELECT film_id, title, description, release_year, rental_duration, rental_rate, length, replacement_cost, rating, special_features, film.last_update, category.name
FROM film, film_category, category
WHERE film.film_id = film_category.film_id
AND category.category_id = film_category.category_id
AND film.film_id = ?';
$stmt = $link->prepare($sql);
$stmt->bind_param('i', $film_id);
$stmt->bind_result($title, $description, $release_year, $rental_duration, $rental_rate, $length, $replacement_cost, $rating, $special_features, $last_update, $category_name);
$stmt->execute();
while ($stmt->fetch()) {
echo '<p>Title: ' . $title . '</p>';
echo '<p>Description: ' . $description . '</p>';
echo '<p>Release year: ' . $release_year . '</p>';
echo '<p>Rental duration: ' . $rental_duration . '</p>';
echo '<p>Rental rate: ' . $rental_rate . '</p>';
echo '<p>Length: ' . $length . '</p>';
echo '<p>Replacement cost: ' . $replacement_cost . '</p>';
echo '<p>Rating: ' . $rating . '</p>';
echo '<p>Special features: ' . $special_features . '</p>';
echo '<p>Last updated: ' . $last_update . '</p>';
echo '<p>Categories: ' . $category_name . '</p>';
$actorsql = 'SELECT actor_id, first_name, last_name
FROM film_actor, film
WHERE film_actor.actor_id = actor.actor_id
AND film_actor.film_id = '.$film_id.';';
$actorstmt = $link->prepare($actorsql);
$actorstmt->bind_param('i', $actor_id);
$actorstmt->bind_result($actor_id, $first_name, $last_name);
while ($actorstmt->fetch()) {
echo '<p>Actor: ' . $first_name . ' ' . $last_name . '</p>';
}
}
?>
再次......生锈的PHP,但这是基本的想法。这将打印电影细节,然后贯穿电影中的演员并在电影信息下逐一打印。
答案 1 :(得分:0)
在SQL语句末尾使用GROUP BY film_actor.actor_id