title: SQL/PHP join products table on multiple images table
body: <p>I have <em>two tables</em> (<strong>products</strong> and <strong>images</strong>). Images contains with none, one, or even more records per product.</p>
  
  <p>I'd like to join <strong>two images</strong> (if available) <strong>to each product</strong> and display them somehow. I'm talking about <strong>products list</strong>, <strong>not a single product</strong>. Here comes the problem.</p>
  
  <p>Let's say I have these tables:</p>
  
  <p>Products:</p>
  
  <pre><code>id     | name
  1      | Lenovo V310
  2      | Adapter 5v
  3      | Mousepad
  4      | Gamepad Logitech
  5      | Nokia 3210
  </code></pre>
  
  <p>Images:</p>
  
  <pre><code>id      | image_name    | product_id
  1       | lenovo1.jpg   | 1
  2       | lenovo2.jpg   | 1
  3       | lenovo3.jpg   | 1
  4       | lenovo4.jpg   | 1
  5       | mousepad1.jpg | 3
  6       | mousepad2.jpg | 3
  7       | gamepad1.jpg  | 4
  8       | gamepad2.jpg  | 4
  9       | nokia1.png    | 5
  10      | nokia2.png    | 5
  11      | nokia3.png    | 5
  12      | nokia4.png    | 5
  </code></pre>
  
  <p>I would like my products (<strong>products list</strong>, <em>not a single item</em>) to be displayed with <strong>two images each</strong>:</p>
  
  <p><strong>Example:</strong> Lenovo V310 + lenovo1.jpg + lenovo2.jpg</p>
  
  <p>I tried it in some ways, but those didn't work. As for example:</p>
  
  <pre><code>    $q = "SELECT products.id, products.name, images.image_name FROM products LEFT JOIN images ON products.id = (SELECT product_id FROM images) LIMIT 6";
      $query = mysqli_query($dbc, $q);
      while ($row = mysqli_fetch_assoc($query)) {
          $id = $row['id'];
          $name = $row['name'];
          $image1 = $row['image_name']; // ain't working also with the query above
          $image2 = $row['image_name']; // how to take the second img????
          ..............
  </code></pre>