title: How to upload an image to a database using php script?
body: <p>here is the code for my upload.. but it doesn't work.. I've used a file_get_contents function.. 
      
      
           Upload image</p>
  
  <pre><code>&lt;/head&gt;
  &lt;body&gt;
      &lt;form action="upload1.php" method="POST" enctype="multipart/form-data"&gt;
      File: 
      &lt;input type="file" name="image"/&gt; 
      &lt;input type="submit" value="Upload image" /&gt;
  
      &lt;/form&gt;
  
  &lt;?php 
  
  //connect to the database
  $con = mysql_connect("localhost","root", "");
  if(!$con)
   {
   die('Could not connect to the database:' . mysql_error());
   echo "ERROR IN CONNECTION";
  }
  
  mysql_select_db("imagedatabase", $con);
  
  
  //file properties
  
   echo $file = $_FILES['image']['tmp_name']; 
   echo '&lt;br /&gt;';
  
   if(!isset($file))
  echo "Please select an image";
  
  else
  {
  $image = file_get_contents($_FILES['image']['tmp_name']);
  echo $image_name = addslashes($_FILES['image']['name']); echo '&lt;br \&gt;';
  echo $image_size = getimagesize($_FILES['image']['tmp_name']);
  
  if($image_size == FALSE)
      echo "That's not an image";
      else
  {
          $insert = mysql_query("INSERT INTO images (image) VALUES    ($image)",$con);
  if(!$insert)
      echo "Problem uploding the image. Please check your database";  
  else 
  {
      $last_id = mysql_insert_id();
      echo "Image Uploaded. &lt;p /&gt; Your image: &lt;p /&gt;&lt;img src=display.php?        id=$last_id&gt;";
      }
  }
  
  }
  mysql_close($con);
  ?&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>and the code for the retrieve/display goes this way..</p>
  
  <pre><code>&lt;?php
   //connect to the database
   mysql_connect("localhost","root", "") or die(mysql_error());
   mysql_select_db("mydb") or die(mysql_error());
  
   //requesting image id
  
   $id = addslashes($_REQUEST['id']);
  
  $image = mysql_query("SELECT * FROM images WHERE id = $id");
  $image = mysql_fetch_assoc($image);
  $image = $image['image'];
  
  header("Conten-type: image/jpeg");
  
   echo $image;
  
  
   mysql_close($connect);
   ?&gt;
  </code></pre>
  
  <p>I've created a database named 'imagedatabase' and a table </p>