473,395 Members | 1,641 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,395 software developers and data experts.

display image from mysql stored in blob format

hi
i am first time using blob files .When i use the blob file to diaplay data it is shown in binary format and i want to diplay that in image format.i am using the following code to extract image and show it.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.      $mysql=mysql_connect("localhost", "root", "");
  3.     mysql_select_db("skm") or die(mysql_error());
  4.  
  5.     $sql = "SELECT emp_code,emp_mail,emp_picture FROM emp";
  6.  
  7.  
  8.     $result = mysql_query($sql) or die("Invalid query: " . mysql_error());
  9.  
  10.  
  11.  
  12.     echo "<table><tr>  <th>Emp Code</th>  <th>Emp Name</th> </tr>";
  13.  
  14.  
  15.             while($row=mysql_fetch_array($result)) 
  16.         {
  17.             echo "<tr>";
  18.  
  19.  
  20.             echo "<td>{$row['emp_code']}</td>";
  21.             echo "<td>{$row['emp_mail']}</td>";
  22.             echo "<td>";
  23.             $image=($row['emp_picture']);
  24.             header("Content-type: image/jpg");
  25.             echo $image;
  26.             echo " </td>";
  27.             echo "</tr>";
  28.          }
  29.  
  30.     echo "</table>";
  31.  
  32.     mysql_close($mysql);
  33.  
  34.  
  35. ?>
Please help me to solve the problem
May 15 '10 #1
1 5217
Atli
5,058 Expert 4TB
Hey.

The problem is that you are trying to print raw image data into a HTML page. That doesn't work. - To be able to accomplish what you are trying to do, you need to understand how HTML displays images: in short, it doesn't display them at all. It simply inserts <img> tags where an image is supposed to be displayed, and produces a location where the browser can fetch the image.

This is how HTML handles images:
Expand|Select|Wrap|Line Numbers
  1. <img src="image1.jpeg" alt="Image 1">
  2. <img src="image2.jpeg" alt="Image 2">
So to display an image from a database into a HTML document you need two documents: one for the HTML and one to fetch and print the image. The HTML should include <img> tags for each image that needs to be displayed, linking to the second file, passing along the ID or name of the image to be displayed.

Each <img> tag should look something like this:
Expand|Select|Wrap|Line Numbers
  1. <img src="fetchImage.php?id=1" alt="Image 1">
Where the "id=1" indicates the ID of the image to be displayed.

The "fetchImage.php" script then uses that ID to fetch the image, set the proper headers and print the data. Nothing else can be printed, or the image will be corrupted. It essentially mimics an image; acting no different than if an actual image file had been requested.

P.S.
There is a way to inline the data into the tag, but I am ignoring that method on purpose. It is unreliable at the moment and tends to increase bandwidth usage way beyond the old-school method (due to the fact that binary data needs to be encoded in Base64, increases it's size by ~40%). - If you want to know the details about that method, see data URI schema on Wikipedia.
May 16 '10 #2

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

Similar topics

2
by: Olav Tollefsen | last post by:
How can I display photo images stored in a SQL Server 2000 databases in a DataList or DataGrid without having to write the images to files first? Olav
2
by: RedSouljaz | last post by:
Hi, How to display image that was saved in database ms sql server 2000 into picture box. The field type that I use in database is Images I can save to database but cannot show from database. ...
3
by: den 2005 | last post by:
Hi everyone, Here is code working on..Trying to insert record with a column with Image or VarBinary datatype in sql database from a existing jpeg image file, then retrieve this image from...
2
by: bshumsky06 | last post by:
Hi, I am trying to create stored procedures in MySQL. Does anyone have a suggestion on a good IDE to use. I have been working with the MySQL Query Browser but it generates errors very often and...
1
by: WebNewbie | last post by:
Hi, I am new to using mysql and there isn't any tutorials online on that shows how to create mysql stored procedure for paging purposes. Thus, I read tutorials on creating stored proc that were...
0
by: dimitri pater | last post by:
---------- Forwarded message ---------- From: dimitri pater <dimitri.pater@gmail.com> Date: Sep 5, 2007 9:13 PM Subject: Re: StringIO MySQL data blob Image problem To: Tim Golden...
7
by: alexseow | last post by:
Query.asp <%@ LANGUAGE="VBSCRIPT" %> <!-- #include file="../../includes/dbconn.asp"--> <% dim MyRs, sqlstr, MyConn Response.Expires = 0 Response.Buffer = TRUE Response.Clear
7
pradeepjain
by: pradeepjain | last post by:
hii, i have stored the images from form to database in blob format.now i have a necessity to retrieve image from database and store it in a folder ..how to do this.. thanks, ...
3
by: nudma | last post by:
Hi everyone!! I m trying to display jpg image from ms-access database on C# web page. Image is stored as OLE object under "img_stream" variable in database. Here is my code: String connStr =...
0
by: Abhilash Etikala | last post by:
I am having a problem in displaying the image. Image is stored perfectly but not able to retrieve and display...i tried to knowledge but still not able to get it.. my code is: display.php ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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.