There are two tables: s_products and s_images

s_products contains information about the product and its id ;
In s_images contains information about the image to the product.
They are related by id , ( s_products - id , s_images - product_id ).

How can I in one cycle pull out product information from s_products , with the condition WHERE visible = 1 , and the name of the picture (in the s_images the name column is filename ) to output the picture directly to the page?

Tried to pull out like this, nothing happened:

  $qr_result = mysql_query("SELECT s_products.*, s_images.* FROM s_products, s_images WHERE s_products.id=s_images.product_id") or die(mysql_error()); While($data = mysql_fetch_array($qr_result)){ echo $data['name']; echo $data['filename']; } 

It doesn't work that way.

  • select p. *, i.filename from s_products as p inner join s_images as i on p.id = i.product_id where p.visible = 1; maybe so try? - DanielOlivo

1 answer 1

Try this:

  $qr_result = mysql_query("SELECT name, filename FROM s_products, s_images WHERE id = product_id AND visible = 1") or die(mysql_error()); While($data = mysql_fetch_array($qr_result)){ echo $data['name']; echo $data['filename']; }