6 Replies - 1042 Views - Last Post: 05 February 2010 - 10:29 AM Rate Topic: -----

#1 sikas  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 18-January 10

retrieving image from DB

Post icon  Posted 30 January 2010 - 06:54 AM

Hi, I`ve inserted an image in a database using the following code:

<?php

$data = fopen("image.bmp",'r');

$conn = mysql_connect("localhost","user","pass");
$dbselect =  mysql_select_db("mydb", $conn);

if(!$conn)
	die("ERROR: " . mysql_error());
	
if(!$dbselect)
	die("ERROR: " . mysql_error());

$query = "INSERT INTO testimg(img) VALUES ('$data')";

if(mysql_query($query,$conn))
	echo "SUCCESS!!";
else
	die("ERROR: " . mysql_error());

mysql_close($conn);

?>



Now, when I try to retrieve the image I used the following code:

<?php

$query = "SELECT * FROM images WHERE id='1';";
$result = mysql_query($query,$conn);
$row = mysql_fetch_array($result);

if($result)
{
	echo "DONE!!<br>";
	$bitmap_img_string = @gzuncompress(pack('H*', $image_data)); 
	$im = imagecreatefrombmpstring($bitmap_img_string); 
	header('Content-Type: image/png'); 
	imagepng($im); 
	imagedestroy($im); 
	$img = $row['images'];
	echo "<img src=$img></img>";
}
else
{
	echo "FAILED!!<br>";
	die("ERROR: " . mysql_error());
}

?>



The image is not shown!! ...

anyone know how can I make it appear??

The image is in the format (Bitmap BMP) ...

Is This A Good Question/Topic? 0
  • +

Replies To: retrieving image from DB

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6066
  • View blog
  • Posts: 23,526
  • Joined: 23-August 08

Re: retrieving image from DB

Posted 30 January 2010 - 07:17 AM

This is what happens when you cut and paste code without making any attempt at actually understanding what it's doing.

What's this?
$bitmap_img_string = @gzuncompress(pack('H*', $image_data));

There's no $image_data in your code...of course you wouldn't know that because you're suppressing errors/warnings/notices through the use of the @ symbol on gzuncompress.

You should be checking $result BEFORE you attempt to fetch the data.
Was This Post Helpful? 0
  • +
  • -

#3 sikas  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 18-January 10

Re: retrieving image from DB

Posted 31 January 2010 - 06:52 AM

ok, I found another simpler code that actually worked ...

but it doesn`t work with BMPs ... it only work with JPEGs ...

<?php 
	error_reporting(E_ALL); 
	$my_table  = 'imageMySQL'; 
	$my_image  = 'campus.jpg'; 
	$my_server = 'localhost'; 
	$my_user   = 'vcamp'; 
	$my_pwd	= 'miuvcamp'; 
	$my_name   = 'test'; 
	$my_link   = mysql_connect($my_server, $my_user, $my_pwd) or exit('Could not connect (' . mysql_errno() . '): ' . mysql_error()); 
	$my = mysql_select_db($my_name, $my_link) or exit('Could not select database (' . mysql_errno() . '): ' . mysql_error());  
	$image = file_get_contents("$my_image"); 
	//mysql_query("DROP TABLE IF EXISTS $my_table"); 
	//mysql_query("CREATE TABLE $my_table (image MEDIUMBLOB)"); 
	//mysql_query("INSERT INTO $my_table (image) VALUES ('".addslashes($image)."')"); 
	$row = mysql_fetch_array(mysql_query("SELECT image FROM $my_table")); 
	//mysql_query("DROP TABLE IF EXISTS $my_table"); 
	header("Content-type: image/jpg"); 
	print $row['image'];  
?>



now I want to use a similar code with C# ... can this be done dealing with BMPs or JPEGs??

This post has been edited by sikas: 31 January 2010 - 06:53 AM

Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3000
  • View blog
  • Posts: 10,393
  • Joined: 08-August 08

Re: retrieving image from DB

Posted 31 January 2010 - 07:22 AM

Why are you storing the image in a database? That's just going to slow your system down. You should be storing the path to the image.
Was This Post Helpful? 0
  • +
  • -

#5 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4353
  • View blog
  • Posts: 12,160
  • Joined: 18-April 07

Re: retrieving image from DB

Posted 31 January 2010 - 05:41 PM

I think you should be listening to CTphpnwb, they are right on the money with this. :)
Was This Post Helpful? 0
  • +
  • -

#6 sikas  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 18-January 10

Re: retrieving image from DB

Posted 01 February 2010 - 08:30 AM

about system speed, it`s not a problem with me ... I`m writing a C# application that will make a connection to the database every now and then, so there will be no overload on the system I guess ... and also, the images that I will store are not a lot ... I think they will be around 10 images ...
Was This Post Helpful? 0
  • +
  • -

#7 cam888  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 64
  • Joined: 20-February 09

Re: retrieving image from DB

Posted 05 February 2010 - 10:29 AM

Still, it's just better practice to store the images in a directory, and the paths in a database. Especially if you wanted to edit the images, it would be awful doing it via a database.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1