My query look like this at the moment:
$result = mysql_query("SELECT * FROM promo JOIN items ON promo.id = items.promo_id WHERE promo.id='$promo_id'") or die('Error : ' . mysql_error());
while($row = mysql_fetch_array($result))
{
echo "<p>".$row['title']."</p>";
echo "<p>".$row['description']."</p>";
echo "<p>".$row['filename']."</p>";
}
I’d like to end up with the following:
-
Title of Promo 1
-
This is Promo 1 Description, isn’t it great?!
-
filename_1.jpg
-
filename_2.jpg and however many files are linked to this promo listed…
However, based on the query above I get:
-
This is Promo 1 Description, isn’t it great?!
-
filename_1.jpg
-
This is Promo 1 Description, isn’t it great?!
-
filename_2.jpg
So it repeats (I can sort of see why) and doesn’t pull in the ‘title’ of the promo at all! Can anyone recommend whether the problem is with my JOIN or my PHP? Many thanks for your help 🙂
Probably description and filename appears because these names exists on only one of the tables but title is on both tables.
If you use the query:
The names of the fields are need to be in the way table.field . it’s very in useful to expecify the names of the fields you need to get to help you and to fetch only the data you need and not all the table. This makes the method faster to retrive the rows and prevent problems with the names.
Also you can specify the name of a column wit the sentence “AS”
Example:
And you get a table like (i setted imaginary data on it):
Tips:
If you got more than one item per promo all data is repeated on each row.
If you don’t have any item for the promo the promo is does not appears into the list.