Question

First of all, I'd just want to say that I'm sorry for the poor title. I'm really struggling with explaining the problem I'm facing in just a short sentence.

I have a table called actors which contains an aID (primary key) and an aName and some more stuff. I also have a table called videos which contains vID (primary key) and other data. The third and last table I have is called connections and that one contains a primary key, a cVideoID and a cActorID.

Let's say I have created a video and when I navigate to the video *www.example.com/video.php?v=primary_key* I'd like to print out all actors who are in that movie (actor1, actor2, actor3 and actor4). Therefor I created the connections-table to keep track of all movie-actor connections. For every movie I create I connect the actors with the movie.

I thought that I could do something like this:

<?php
    $result2 = mysql_query("SELECT `actors`.`aID`, `actors`.`aName` FROM `actors` WHERE `connections`.`cVideoID` = {$_get['v']}");
    while($actors = mysql_fetch_array($result2))
    {
        echo "<a href='actor.php?id={$actors['aID']}>{$actors['aName']}</a> ';
    }
?>

But it seems like that's not working. Any ideas?

Was it helpful?

Solution

What you want to do, is select all of the entries from connections, where the video ID is the selected video. Then, you JOIN on the actors table, to get all of the information about the actors that were found.

Example: Get all of the actor's names for a specific video ID:

SELECT a.aName 
FROM connections c 
  LEFT JOIN actors a 
  ON a.aID = c.aID 
WHERE c.vID = 1;

SQL Fiddle

OTHER TIPS

What you need here is a join. A normal left join works like this:

LEFT JOIN [name of table] [name of table you will want to use] 
       ON ([where statements searching for the right columns to join]) 

So your query will look something like this:

   SELECT a.aID, a.aName 
     FROM `connections` c 
LEFT JOIN `author` a ON (c.cActorID=a.aID) 
    WHERE c.cVideoID=[id of the video]

Now first of all you say the database you want to catch the columns aID and aName from the table a next you use the FROM statement to "import" the connections table as "c". You then load the author table and make it accessible as "a" (see the select statement a.[...]) and you also say that it should join the two tables ON every c.cActorID=a.aID and in the end you make a where statement to declare you are only searching for videos with the c.cVideoId=[id]

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top