This isn't caused by your query. This is caused by your looping and many queries.
Since you execute this query and it only retrieves one record:
PHP Code:
$qry= mysql_query("SELECT DISTINCT user_id FROM title_views WHERE title_id = '$trimmed' and prog_id = '$prog_id' ")
or die(mysql_error());
$row = mysql_fetch_array($qry);
echo $row['user_id'] . "<br />";
If any other of the queries created by the use of the foreach loop pull the same user_id, then these are considered unique as the query is unique.
If the tables were properly normalized, a query could be written that simply joins as necessary.
Without it, you need to capture $row['user_id'] in an array, and write a array_unique call to remove duplicates after the foreach (possibly the while, depending on at what level the userid is considered unique - programs or title views). You can also change the where clause to use an IN and provide it with all the title_id's required.