473,657 Members | 2,654 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

display image from mysql(blob data)

11 New Member
HI all,
I am able to upload the image as blob to mysql. but while displaying the image i cant display it properly .
The code used for uploading image to mysql inserts data into mysql table.The uploading code is:
Expand|Select|Wrap|Line Numbers
  1. MYSQL_CONNECT("localhost","root","");
  2.     mysql_select_db("sample");
  3.  
  4.     $data = addslashes(fread(fopen($form_data, "r"), filesize($form_data)));
  5.  
  6.     $result=MYSQL_QUERY("INSERT INTO PHOTO(description,bin_data,filename,filesize,filetype) ".
  7.         "VALUES ('$form_description','$data','$form_data_name','$form_data_size','$form_data_type')");
  8.  
The code used for displaying the image is as follows
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.     MYSQL_CONNECT("localhost","root","");
  3.     mysql_select_db("sample");
  4.  
  5. $id=2;
  6.  
  7.  $query = "select filename,filetype,filesize,bin_data from PHOTO where id=$id";
  8.  
  9. $result = mysql_query($query) or die('Error, query failed');
  10. //$ROW = mysql_fetch_assocc($result);
  11. list($name, $type, $size, $content) = mysql_fetch_array($result);
  12.  
  13. echo '<meta http-equiv="Content-Type" content="img/jpg" />';
  14. echo $content;
  15. exit;
  16.  
  17. ?>
  18.  
This code displays some random characters all over the browser but not the image. I think i have been missing some statements .
Thank you
Jul 4 '08 #1
4 8147
Markus
6,050 Recognized Expert Expert
You have to set the headers properly.

Read this tutorial. (scroll down to the part about downloading.)
Jul 4 '08 #2
pradeepjain
563 Contributor
this might help u

[PHP]$query = "SELECT Doc,Doc1,Doc2,N ame,Name1,Name2 FROM xyz WHERE id = '".$id."'";

$result = mysql_query($qu ery) or die('Error, query failed');
list($Doc,$Doc1 ,$Doc2,$Name,$N ame1,$Name2) = mysql_fetch_arr ay($result);

//header("Content-length: $size");
header("Pragma: public");
header("Expires : 0"); // set expiration time
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
//header("cache-Control: no-cache, must-revalidate"); // HTTP/1.1 changed on 27th july commented this and next line and addedprevious 3 lines for IE caching prob
//header("Expires : Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Content-type: application/pdf");
header("Content-Disposition: attachment; filename=$Name" );
echo $Name;
echo $Doc;
[/PHP]
Jul 4 '08 #3
Markus
6,050 Recognized Expert Expert
this might help u

[PHP]$query = "SELECT Doc,Doc1,Doc2,N ame,Name1,Name2 FROM xyz WHERE id = '".$id."'";

$result = mysql_query($qu ery) or die('Error, query failed');
list($Doc,$Doc1 ,$Doc2,$Name,$N ame1,$Name2) = mysql_fetch_arr ay($result);

//header("Content-length: $size");
header("Pragma: public");
header("Expires : 0"); // set expiration time
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
//header("cache-Control: no-cache, must-revalidate"); // HTTP/1.1 changed on 27th july commented this and next line and addedprevious 3 lines for IE caching prob
//header("Expires : Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Content-type: application/pdf");
header("Content-Disposition: attachment; filename=$Name" );
echo $Name;
echo $Doc;
[/PHP]
He's not trying to download a pdf.
Jul 4 '08 #4
pradeepjain
563 Contributor
Hey really srry ...i did not notice tht,,this is wht wll work for him

[PHP]
error_reporting (E_ALL);
$link = mysql_connect(" localhost", "root", "") or die("Could not connect: " . mysql_error());

mysql_select_db ("xyz") or die(mysql_error ());

$sql = "SELECT Pic FROM abc WHERE ID='".$_GET['Id']."'";
//'".$_GET['Id']."'";

$result = mysql_query("$s ql") or die("Invalid query: " . mysql_error());

header("Content-type: image/jpeg");
$test=mysql_res ult($result,0);

$desired_width = 85;
$desired_height = 110;

$im = imagecreatefrom string($test);
$new = imagecreatetrue color($desired_ width, $desired_height );

$x = imagesx($im);
$y = imagesy($im);

imagecopyresamp led($new, $im, 0, 0, 0, 0, $desired_width, $desired_height , $x, $y);

imagedestroy($i m);
header("cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires : Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header('Content-type: image/jpeg');
imagejpeg($new, NULL, 85);

imagedestroy($n ew);
// echo $test;

mysql_close($li nk);
[/PHP]
Jul 8 '08 #5

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

Similar topics

0
1725
by: owardman | last post by:
Dear All. Informix 9.3 Solaris 9 MySQL 4.1.10 Linux I am trying to migrate blob data from Informix to MySQL. Has anybody managed this successfully and if so how? Thanks in advance
2
4997
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. Thanks
3
5232
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 database and display it in a Image web control dynamically(at runtime). The process after being displayed in the web control, user click insert/add button, it converts the image(jpeg) file to bytes and store it the database with Image or VarBinary...
1
3034
by: sang | last post by:
Hi i am doing my project in Mysql and PHP. My problem is i want to send a Blob field to my email using the Php. That is i am storing Candidates Resume in Blob field(Document format). I want to Choose the Particular Candidates Resume and the send to some Email address.But i want to know how can i get the field from mysql and how to send it to the particular emailid. Please any one help to solve my problem. Thanks and Regards Sang
1
8394
by: jamal8t2 | last post by:
i have inserted blob data in database , but i am not able to access back that data to the browser. i have php script but it does not work proprly. our script is below if ($_REQUEST == 1) { header("Content-type: image/jpeg"); print $bytes; exit (); }
5
1871
by: faizalahmd | last post by:
How to display image in image control , which is retrieved from the data base . using c# in asp.net regards, FAIZAL AHMED.H
1
1863
by: waleedmazhar | last post by:
Hello Everybody Can anybody tell me when to save images in mysql blob and when save image folder and save the image name in database. which is more better saving image into database or save it name into database and image itself into folder.
6
6359
by: phpmagesh | last post by:
Hi I have tried this sample code in my database for file update, but some error occurred. everything is going fine. uploaded the file successfully but, when i download the file which i uploaded in Database, the file is corrupted or flouted. If the uploaded file is image, then the image is not displaying when it is downloaded, So totally there is some minor problem with this code, can any one test and help me please. i just tried...
0
1752
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 <img src="displayimg.php?co=<?php echo $t;?>"> \\$t is some id value i am sending to displayimg.php displayimg.php
0
8394
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8825
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8732
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8605
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1955
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1615
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.