Jump to content

How to display image from blob field??


radiations3

Recommended Posts

I have uploaded my images perfectly in the blob field but i am not able to display the image i used the following code but its not working: NEED HELP

 

<?php

 

$username = "root";

$password = "";

$host = "localhost";

$database = "bakery";

 

@mysql_connect($host, $username, $password) or die("Can not connect to database: ".mysql_error());

 

@mysql_select_db($database) or die("Can not select the database: ".mysql_error());

 

$id = 3 ;

 

if($id==3){

 

 

$query = mysql_query("SELECT img FROM pic WHERE Desc='".$id."'");

$row = mysql_fetch_array($query);

$content = $row['image'];

 

header('Content-type: image/jpg');

echo $content;

 

}

 

?>

Link to comment
https://forums.phpfreaks.com/topic/243646-how-to-display-image-from-blob-field/
Share on other sites

"It's not working" is not a good way to describe a problem. Some more details would be nice. Are you getting a corrupt image error or something?

 

I am not getting any (corrupt or required) image and its only displaying the following message  if i comment the header...

 

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in D:\DBMS\get.php on line 6

 

MY ACTUAL CODE IS FOLLOWING:

 

<?php require_once('Connections/hammad.php'); ?>

 

<?php mysql_select_db("bakery") or die(mysql_error());

$id=3;

$image =  mysql_query("SELECT img FROM pic WHERE Desc='".$id."'");

$image = mysql_fetch_assoc ($image);

$image = $image ['$image'];

 

header("content-type: image/jpg");

 

echo $image;

 

 

?>

After putting or die(mysql_error()); on my select query its displaying following error message please help:

 

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Desc='3'' at line 1

 

 

$image =  mysql_query("SELECT img FROM pic WHERE Desc='".$id."'") or die(mysql_error());

You can't use the word 'desc' in your queries, unless you backtick it. 'desc' is a reserved word for MySQL, so you can order things.

 

SELECT * FROM table ORDER BY id DESC

 

I'd suggest choosing a different column name or surrounding it with backticks:

 

WHERE `Desc`='" . $id . "'

You can't use the word 'desc' in your queries, unless you backtick it. 'desc' is a reserved word for MySQL, so you can order things.

 

SELECT * FROM table ORDER BY id DESC

 

I'd suggest choosing a different column name or surrounding it with backticks:

 

WHERE `Desc`='" . $id . "'

 

Yeah that was a problem but now its displaying following error message:

 

Notice: Undefined index: $image in D:\DBMS\get.php on line 7

 

 

 

<?php require_once('Connections/hammad.php'); ?>

 

<?php mysql_select_db("bakery") or die(mysql_error());

$id=3;

$image =  mysql_query("SELECT img FROM pic WHERE `Desc`='".$id."'") or die(mysql_error());

$image = mysql_fetch_assoc ($image);

$image = $image ['$image'];

 

//header("content-type: image/jpg");

 

echo $image;

 

 

?>

 

 

 

KIndly let me know am i processing the right code for displaying an image from blob???

It looks like you're getting muddled up. In your query you specify: SELECT img FROM pic

By that logic, the blob column is called img

So this line:

$image = $image['$image'];

Makes absolutely no sense because 1) you're referencing the $image variable inside single quotes which is making it into a string and 2) your column is titled img not $image

 

Change that line to:

$image = $image['img'];

It looks like you're getting muddled up. In your query you specify: SELECT img FROM pic

By that logic, the blob column is called img

So this line:

$image = $image['$image'];

Makes absolutely no sense because 1) you're referencing the $image variable inside single quotes which is making it into a string and 2) your column is titled img not $image

 

Change that line to:

$image = $image['img'];

 

Now i am able to display image but only the rubbish (corrupt) content of the image is getting displayed..... I don't know why is it happening I was expecting a JPEG image :( you really helped out to make my code absolutely perfect and thanks for that but i guess  there is a problem with some logic of displaying the image...

Ensure there is no whitespace before and after the opening/closing PHP tags, also try using image/jpeg as your content type. See what happens there... I've never stored images in a database before so I wish you luck.

 

YAAAAAAAAAAAHHHHHHHHHOOOOOOOOOOOOO my problem of uploading the image in database is solved now thanks alot

 

Thank you very much

Final Without any error code

 

 

<?php require_once('Connections/hammad.php'); ?>
<?php 
mysql_select_db("bakery") or die(mysql_error());
$id=addslashes(3);
$image =  mysql_query("SELECT img FROM pic WHERE `Desc`='".$id."'") or die(mysql_error());
$image = mysql_fetch_assoc ($image);
$image = $image ['img'];
header("content-type: image/jpg");
echo $image;
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.