Reputation: 4462
Up until now I have just been using one table in a MySQL query, but now I am using two tables in a query to return only users' favourites, i.e.:
$query1="SELECT tracks.id, favourites.track FROM tracks, favourites WHERE tracks.id = favourites.track";
$result=mysql_query($query1) or die(mysql_error());
I am wondering how then I would refer to columns in rows in just the tracks table to construct variables from this query. Previously I used:
while ($row = mysql_fetch_array($result)) {
$title = $row['title'];
...etc
}
But obviously this needs adapting to refer to just the rows in the tracks table, but how do I do this?
Upvotes: 1
Views: 79
Reputation: 5464
You can do the same using:
$query1="SELECT T.id as Tid, F.track , T.title as Title FROM tracks T inner join favourites F ON T.id = F.track";
$result = mysql_query($query1);
while ($row = mysql_fetch_object($result))
{
$title = $row->Title;
$tid = $row->Tid;
/*...etc...*/
}
Upvotes: 1