473,403 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Load image from mysql DB

250 100+
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?
Oct 12 '09 #1
6 4744
Dormilich
8,658 Expert Mod 8TB
what does the variable $image contain?
Oct 12 '09 #2
ghjk
250 100+
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.  
Oct 12 '09 #3
Dormilich
8,658 Expert Mod 8TB
@ghjk
that explains it. do you know how HTML displays images?
Oct 12 '09 #4
ghjk
250 100+
No. This is the first time I'm trying to load images from the DB.
Oct 12 '09 #5
Dormilich
8,658 Expert Mod 8TB
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.
Oct 12 '09 #6
ghjk
250 100+
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.  
Oct 13 '09 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Alper Adatoz | last post by:
Hi, i have a little problem. i hope u guys give me a clear solution (: db: mssql i just want to put jpeg file to the image field at the mssql db. and after that i want to call it back..
0
by: Donald Tyler | last post by:
Then the only way you can do it that I can think of is to write a PHP script to do basically what PHPMyAdmin is trying to do but without the LOCAL in there. However to do that you would need to...
1
by: Ray in HK | last post by:
What are the differences between LOAD DATA INFILE and LOAD DATA LOCAL INFILE ? I found some web hosting company do not allow using LOAD DATA INFILE but allow LOAD DATA LOCAL INFILE. The reason...
3
by: Christopher Mouton | last post by:
We regularly make drive images of our entire server and store it at a backup site. To be honest I have never fully understood how the imaging programs deal with open files. My question is will the...
10
by: John Smith | last post by:
I know that uploading an image to a database has been covered, oh, about 3 trillion times. However, I haven't found anything covering uploading to a MySQL database with .net. Please don't...
3
by: nsh | last post by:
mailing.database.mysql, comp.lang.php subject: does "LOAD DATA" EVER work?!? I've tried EVERYTHING! version info: my isp is running my web page on a linux box with php ver. 4.4.1 according to...
3
by: Michael martin | last post by:
I'm trying to create a picture database for a site that I'm working on and I would like to do the following: - (main goal) Have a php script load an entire directory of photos into my mysql...
3
by: Me Alone | last post by:
Hello: I am trying to edit some C code I found in "The definitive guide to using, programming, and administering MySQL" by Paul DuBois. This C client program connects and then segfaults when...
1
by: edfialk | last post by:
Hi all, I'm desperately trying to get a simple mysql connection working in php 4.3.9 and I think I have a doozy for you guys. First of all, I didn't set up ANY of this system, I'm just working...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.