title: store image into mysql DB with php and display
body: <p>Having big time problems in displaying an image out of my mysql database</p>
  
  <p>I'm storing it in a longblob type</p>
  
  <p>when the image is displayed i get a broken image icon</p>
  
  <p>here is code for storing image</p>
  
  <pre><code>if(isset($_FILES['image']) &amp;&amp; $_FILES['image']['size'] &gt; 0 &amp;&amp; isset($_POST['photoName']))
  {
      //temporary file name
      // Temporary file name stored on the server
      $tmpName = $_FILES['image']['tmp_name'];
      $imageType = $_FILES['image']['type'];
  
      // Read the file
      $fp = fopen($tmpName, 'r');
      $data = fread($fp, filesize($tmpName));
      $data = addslashes($data);
      fclose($fp);
  
  
      $sql="INSERT INTO photos (photoName, caption, photoData, photoType, userName)
          VALUES
          ('$_POST[photoName]','$_POST[caption]','$tmpName','$imageType', '$currentUser')";
  
      //For debugging purposes
      if(!mysqli_query($con,$sql))
      {
          die('Error: ' . mysqli_error($con));
      }
      else
      {
          echo "Your Image has been Added";
      }   
  }
  </code></pre>
  
  <p>then printing the image</p>
  
  <pre><code>if(isset($_POST['usersImage'])){
          //code to show images
          $user = $_POST['usersImage'];
          $sql = "SELECT * FROM `photos` WHERE userName = '$user'";
          $result = mysqli_query($con,$sql);
          while($row = mysqli_fetch_array($result)) 
          {
              switch ($row['photoType']) {
                  case 'image/jpeg':
  
                  echo "&lt;tr&gt;";  
                  echo '&lt;img src="data:image/jpeg;base64,'. base64_encode($row['photoData'])."\"&gt;&lt;/td&gt;";   
                  echo "&lt;/tr&gt;"; 
                      //echo '&lt;img src="image&gt;' .$row['photoData']. '.jpeg'.'&lt;/p&gt;';
                      //echo '&lt;p id="caption"&gt;'.$row['caption'].' &lt;/p&gt;';
                  break;
              }           
          }
      }
  </code></pre>
  
  <p>as you can see my latest attempt was to use base64 encoding to print out the image
  my previous try was the commented out code</p>