Connecting Tech Pros Worldwide Forums | Help | Site Map

Load image from mysql DB

Familiar Sight
 
Join Date: Jan 2008
Posts: 199
#1: Oct 12 '09
I store images using one of my web pages. Next I want to load image from the db and pass the value to ajax page. Other values got correctly. But didn't get image.This is my code.
Expand|Select|Wrap|Line Numbers
  1. php page. When user click on select link It pass values to ajax page
  2. echo  "<td><a href='javascript:void(0);' onclick=\"test('$VehicleNo','$Year','$Other','Image'); return false;\">Select</td>";
  3.  
  4. This is my ajax code.
  5. function LookupVehicle(VehicleNo,Year,Other,Image){
  6.  
  7.     document.getElementById('VehicleNo').value=VehicleNo;
  8.     document.getElementById('userfile').value=Image;
  9.     document.getElementById('Year').value=Year;
  10.     document.getElementById('Other').value=Other;
  11.  
  12. }
  13.  
Could someone help me?

Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,669
#2: Oct 12 '09

re: Load image from mysql DB


what does the variable $image contain?
Familiar Sight
 
Join Date: Jan 2008
Posts: 199
#3: Oct 12 '09

re: Load image from mysql DB


It has blob type of data. I'm inserting image like this.
Expand|Select|Wrap|Line Numbers
  1. <form enctype="multipart/form-data" action="insert.php" method="post" name="changer">
  2. <input name="MAX_FILE_SIZE" value="102400" type="hidden">
  3. <input name="image" accept="image/jpeg" type="file">
  4. <input value="Submit" type="submit">
  5.  
  6.  
  7. // Make sure the user actually 
  8. // selected and uploaded a file
  9. if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) { 
  10.  
  11.       // Temporary file name stored on the server
  12.       $tmpName  = $_FILES['image']['tmp_name'];  
  13.  
  14.       // Read the file 
  15.       $fp      = fopen($tmpName, 'r');
  16.       $data = fread($fp, filesize($tmpName));
  17.       $data = addslashes($data);
  18.       fclose($fp);
  19.  
  20.  
  21.       // Create the query and insert
  22.       // into our database.
  23.       $query = "INSERT INTO tbl_images ";
  24.       $query .= "(image) VALUES ('$data')";
  25.       $results = mysql_query($query, $link);
  26.  
  27.       // Print results
  28.       print "Thank you, your file has been uploaded.";
  29.  
  30. }
  31. else {
  32.    print "No image selected/uploaded";
  33. }
  34.  
  35. // Close our MySQL Link
  36. mysql_close($link);
  37. ?>  
  38.  
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,669
#4: Oct 12 '09

re: Load image from mysql DB


Quote:

Originally Posted by ghjk View Post

It has blob type of data.

that explains it. do you know how HTML displays images?
Familiar Sight
 
Join Date: Jan 2008
Posts: 199
#5: Oct 12 '09

re: Load image from mysql DB


No. This is the first time I'm trying to load images from the DB.
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,669
#6: Oct 12 '09

re: Load image from mysql DB


HTML (and thus Javascript) does not display images at all. it merely provides a link to the image, the remainder is left up to the browser software.
Familiar Sight
 
Join Date: Jan 2008
Posts: 199
#7: Oct 13 '09

re: Load image from mysql DB


found. We can load images like this.
Expand|Select|Wrap|Line Numbers
  1.  $link = mysql_connect("localhost", "root", "xxx") or die("Could not connect: " . mysql_error());
  2.  
  3.         // select our database
  4.         mysql_select_db("xxx") or die(mysql_error());
  5.  
  6.         // get the image from the db
  7.         $sql = "SELECT Image FROM xxx WHERE Id=21";
  8.  
  9.         // the result of the query
  10.         $result = mysql_query("$sql") or die("Invalid query: " . mysql_error());
  11.  
  12.         // set the header for the image
  13.         header("Content-type: image/jpeg");
  14.       echo  mysql_result($result, 0);
  15.  
  16.  
  17.         // close the db link
  18.         mysql_close($link);
  19.  
Reply