Hi i have developed a photo gallery in php and now i want to add comment section to those photos in the gallery.Actually i have table to store the comments regarding the particular image.I'm able to retrive the recent comments for the image,but the problem here is i'm not getting the previous comments posted by users for the same image.I'm getting the comments of all the photos into an array using image id which links to the id of the image in another table like

img_id comment
------- --------
1 nice picture
1 super picture
1 awesome picture

id file_name
--- ----------
1 flower.jpg

in this case i'm able to retrive only the last comment i.e., "awesome picture". but i want to get all the comments displayed for the same picture. Any help...!

Edited by Layerphp: n/a

6 Years
Discussion Span
Last Post by pritaeas

it's like this

for($i=0; $i<count;$i++){
$query="select * from table1 where img_id=(select id from table2 where filename="")";
/*here i'm getting the results from tht table1 to an array but now how to get them out from the array accroding to img_id*/


Are you trying to get all comments for one filename? If so, do you have the filename in a variable? Then you can do something like this:

$filename = 'flower.jpg';
$query = "SELECT * FROM table1 t1, table2 t2 WHERE t1.id=t2.img_id AND t1.file_name='$filename'";
$result = mysql_query($query);
if ($result) {
  while ($row = mysql_fetch_array($result)) {
    echo $row['comment'] . '<br/>';
else {
  // no comments

Edited by pritaeas: n/a


yeah the query is working fine and i'm getting the comment for the image but that is only recent one,i want the previous comments for the same image to be displayed which are stored in the database...!


Finally see the problem:

select * from table1 where img_id in (select id from table2 where filename='...')

Use IN instead of =

Edited by pritaeas: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.