title: Retrieve and display images from MySQL database (with condition)
body: <p>I'm having a problem on developing my website..</p>
  
  <p>First, I have 2 tables:</p>
  
  <p>1- <strong>Products</strong>: contains info. about the products &amp; 2- <strong>Images</strong>: which contains product images.</p>
  
  <p>The idea is that : I want to insert an image or two for a specified product and display that image for that product only..</p>
  
  <p>In the mean time, I can insert images into the database and I can also display them.. but my issue is that when I try to do display.. the code retrieves all images from <strong>Images</strong> table..</p>
  
  <p><strong>so my question:</strong> how can I display image/s for only one product .. not all images in that table? I'm confused in how to make this condition.. need ur help guys  :?  </p>
  
  <p>Here's my code:</p>
  
  <p><strong>File1: imageUpload.php</strong></p>
  
  <pre><code>&lt;?php
  
  if(count($_FILES) &gt; 0) {
  if(is_uploaded_file($_FILES['userImage']['tmp_name'])) {
  mysql_connect("localhost", "root", "");
  mysql_select_db ("myDB");
  $imgData =addslashes(file_get_contents($_FILES['userImage']['tmp_name']));
  $imageProperties = getimageSize($_FILES['userImage']['tmp_name']);
  
  $sql = "INSERT INTO Images(imageType ,imageData, product_id)
      VALUES('{$imageProperties['mime']}', '{$imgData}','{$_POST['memids']}')";
  $current_id = mysql_query($sql) or die("&lt;b&gt;Error:&lt;/b&gt; Problem on Image Insert&lt;br/&gt;" .        
  
      mysql_error());
  if(isset($current_id)) {
  header("Location: listImages.php");
  }
   }
  }
  ?&gt;
  
  
  &lt;HTML&gt;
  &lt;HEAD&gt;
  &lt;TITLE&gt;Upload Image&lt;/TITLE&gt;
  &lt;link href="imageStyles.css" rel="stylesheet" type="text/css" /&gt;
  &lt;/HEAD&gt;
  &lt;BODY&gt;
  &lt;form name="frmImage" enctype="multipart/form-data" action="" method="post"   
  class="frmImageUpload"&gt;
  &lt;label&gt;Upload Image File:&lt;/label&gt;&lt;br/&gt;
  &lt;input name="userImage" type="file" class="inputFile" required/&gt;
  &lt;input type="submit" name="submit" value="Submit" class="btnSubmit" /&gt;&lt;br&gt;&lt;br&gt;
  
  &lt;/form&gt;
  &lt;/div&gt;
  
  &lt;/BODY&gt;
  &lt;/HTML&gt;
  </code></pre>
  
  <p><strong>File2: listImages.php</strong></p>
  
  <pre><code>&lt;?ob_start()?&gt;
  
  &lt;?php
  
  
  $conn = mysql_connect("localhost", "root", "");
  mysql_select_db("myDB");
  $sql = "SELECT imageId FROM Images
        ORDER BY imageId DESC"; 
  $result = mysql_query($sql);
  ?&gt;
  
  
  &lt;HTML&gt;
  &lt;HEAD&gt;
  &lt;TITLE&gt;List BLOB Images&lt;/TITLE&gt;
  &lt;link href="imageStyles.css" rel="stylesheet" type="text/css" /&gt;
  &lt;/HEAD&gt;
  &lt;BODY&gt;
  &lt;?php
  while($row = mysql_fetch_array($result)) {
  ?&gt;
  &lt;img src="imageView.php?image_id=&lt;?php echo $row["imageId"]; ?&gt; "width="200" /&gt;&lt;br/&gt;
  &lt;?php       
  }
  mysql_close($conn);
  ?&gt;
  
  &lt;/BODY&gt;
  &lt;/HTML&gt;
  &lt;?ob_flush()?&gt;
  </code></pre>
  
  <p><strong>File3: imageView.php</strong></p>
  
  <pre><code>&lt;?php
  
  $conn = mysql_connect("localhost", "root", "");
  mysql_select_db("myDB") or die(mysql_error());
  if(isset($_GET['image_id'])) {
      $sql = "SELECT imageType,imageData FROM Images WHERE imageId=" . $_GET['image_id'];
      $result = mysql_query("$sql") or die("&lt;b&gt;Error:&lt;/b&gt; Problem on Retrieving Image BLOB&lt;br/&gt;" . mysql_error());
      $row = mysql_fetch_array($result);
      header("Content-type: " . $row["imageType"]);
      echo $row["imageData"];
  }
  mysql_close($conn);
  ?&gt;
  </code></pre>