2

I have a database that has information on actors, roles, and movies. On an HTML page I have the user input the first name and last name of an actor.

This form is then submitted to my PHP page where I look through my database, and provide the user with a 3 column html table with the following information: Movie Name the actor has worked in, Role the actor played, and year of movie.

All these information are in separate database tables. Examples of what each table looks like is below:

actor table contains:

'id', 'first_name', 'last_name', and 'gender' stored in the following way - (933,'Actor','Name','M')

role table contains:

'actor_id', 'movie_id', 'role stored in the following way - (16844,10920,'Role')

movies table contains:

'id', 'name', 'year', 'rank' stored in the following way - (306032,'Snatch.',2000,7.9)

I have to look through and correlate all the data into a table. This is what I have so far:

$sql ="SELECT id, first_name, last_name FROM actors";
$result = mysql_query($sql);
while($row=mysql_fetch_array($result)) {
  if ($row['first_name'] == $firstname && $row['last_name'] == $lastname) {
    $actorID = $row['id'];
    echo "$actorID<br>";
  }

}//end while

$sql2 = "SELECT actor_id, movie_id, role FROM roles";
$result2 = mysql_query($sql2);
while ($row=mysql_fetch_array($result2)) {
  if ($row['actor_id'] == $actorID) {
    $movieID = $row['movie_id'];
    $actRole = $row['role'];
    echo "$movieID <br>";
    echo "$actRole <br>";
  }
} //end while

$sql3 = "SELECT id, name, year FROM movies";
$result3 = mysql_query($sql3);
while ($row=mysql_fetch_array($result3)) {
  if ($row['id'] == $movieID) {
    $movieName = $row['name'];
    $movieYear = $row['year'];
    echo "$movieName <br>";
    echo "$movieYear <br>";
  }
} //end while

echo '
  <table>
      <thead>
      <tr>
        <th> Movie Name </th>
        <th> Actor Role </th>
        <th> Release Date </th> 
      </tr>
      </thead>
      <tbody>
      <td>'. $movieName .'</td>
      <td>'. $actRole. '</td>
      <td>'. $movieYear. '</td>
      </tbody>
  </table>'; 


?>

My solution works -- just a mediocre way of doing it

JustAskingThings
  • 295
  • 1
  • 3
  • 10
  • You **really** should not be writing code that relies on `mysql_` functions anymore. The MySQL extension has been deprecated for years and is about to be dropped in the upcoming PHP7 release, which can be release any time now. Also see [Why shouldn't I use mysql_* functions in PHP?](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). On an up-to-date server, this code has a life span of about a month, after which it will **stop working**. – Oldskool Nov 20 '15 at 23:43

1 Answers1

0

You don't have assign the variables $firstname and $lastname. Apart from that your if condition is wrong true every time.

if ($row['first_name'] = $firstname && $row['last_name'] = $lastname) {

Should be:

if ($row['first_name'] == $firstname && $row['last_name'] == $lastname) {

Also check what you want to do with the echo $row['first_name'][0];

Note that mysql_* functions are deprecated so you better use mysqli or PDO.

EDIT:

You can select all actors that have play in a movie using the following query. You can adjust to take only the information you need changing SELECT clause or using WHERE.

$sql = "
    SELECT aa.id AS actor_id, aa.first_name, aa.last_name, cc.name, cc.year
    FROM actor AS aa
    INNER JOIN role AS bb
    ON aa.id = bb.actor_id
    INNER JOIN movies AS cc
    ON cc.id = bb.movie_id";

EDIT 2: (from comments)

You can use the following code:

$conn = mysqli_connect("localhost", "db_username", "your_password", "your_database");
$query = "
    SELECT aa.id AS actor_id, aa.first_name, aa.last_name, cc.name AS movie_name, cc.year AS release_year
    FROM actor AS aa
    INNER JOIN role AS bb
    ON aa.id = bb.actor_id
    INNER JOIN movies AS cc
    ON cc.id = bb.movie_id";
$result = mysqli_query($conn, $query);

echo '
    <table>
        <thead>
            <tr>
                <th>Actor id</th>
                <th>Firstname</th>
                <th>Lastname</th>
                <th>Movie name</th>
                <th>Release date</th>
            </tr>
        </thead>
        <tbody>';
while($row = mysqli_fetch_array($result)) {
    echo '<td>'.$row['actor_id'].'</td>';
    echo '<td>'.$row['first_name'].'</td>';
    echo '<td>'.$row['last_name'].'</td>';
    echo '<td>'.$row['movie_name'].'</td>';
    echo '<td>'.$row['release_year'].'</td>';
}
echo '
        </tbody>
    </table>';
Kostas Mitsarakis
  • 4,772
  • 3
  • 23
  • 37