Display Image stored on MySql ??

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dynamic_desi
Forum Newbie
Posts: 8
Joined: Sun Jul 17, 2005 12:42 am

Display Image stored on MySql ??

Post by dynamic_desi »

Hi All,

I have few images stored on MySql server as type blob.

while i try and display the image using echo in a php script, it show junk on the page??

Any suggestions on how i can display an image stored on the database server onto the webpage. I am trying to make a picture gallery.

thanks for your help in advance

dynamic_desi :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you will need a script that can identify and extract out the image. Basically, your page needs to use this script as its image links with some identifier that you can grab the image through.. the script will need to use header() to set the mime-type correctly (and remember to use header() to pass back the size of the image being sent in "content-length")
dynamic_desi
Forum Newbie
Posts: 8
Joined: Sun Jul 17, 2005 12:42 am

Works fine as a seprate script, but not as a module....

Post by dynamic_desi »

Hi,

After trying the header thing which looks like this

Code: Select all

<?php


if(isset($_GET['id']))
{
$id=$_GET['id'];
$connection = mysql_connect('localhost', 'root', 'passwordl');
$dbselect   = mysql_select_db('images');

$query = "select bin_data, filetype from binary_data where id=$id";
$result = mysql_query($query);

$data = mysql_result($result, 0, "bin_data");
$type = mysql_result($result, 0, "filetype");

header("content-type: $type");
echo $data;
}
?>

However if i use the same as a module in this script....it displays junk... :cry:

Code: Select all

function showItems($subcatid)
{
if (!$subcatid || $subcatid=='')
     return false;


$query = "select * from items where subcatid = '$subcatid'";
$result  = mysql_query($query) or die('Error, query failed');
//$row = mysql_fetch_array($result);
$connection = mysql_connect('localhost', 'root', 'password');
$dbselect   = mysql_select_db('images');

$query = "select bin_data, filetype from binary_data where id=8";
$result = mysql_query($query);

$data = mysql_result($result, 0, "bin_data");
$type = mysql_result($result, 0, "filetype");

//header("content-type: $type");
//echo $data;

?>
<table  border="0" cellspacing="1" cellpadding="1">
<?
while ($row = mysql_fetch_array($result))
{
extract($row)
?>
<tr>
<td width="100" height="120" align="center"><?php header("content-type: $type"); echo $data; 
?>
<br><?php echo $row[1]; ?></td>
<td width="430"><?php echo $row[4]; ?><br>$<?php echo $row[2]; ?></td>
</tr>
<?
}
?>
</table>
<?
}
I wonder what am i doing wrong.....any help will be a great favor....

thanks

dynamic desi

feyd | I do not like getting in the habit of fixing people's posts..
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

while I do not know the solution I do know that it would be much easier for people to figure out if you used the [ php ] and [ /php ] tags :-D
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you cannot output images at the same time as HTML... choose one.

Images are sent in web pages via a secondary stream
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

feyd | I do not like getting in the habit of fixing people's posts..
I happen to think you love it. Perhaps I should randomly do it to keep you occupied. :oops:
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I happen to think you love it.
I very much don't. Starting soon, I'll put a limit as to how many times I'll fix someone's posts, after-which they will not recieve my help until such time as they start doing them correctly.
dynamic_desi
Forum Newbie
Posts: 8
Joined: Sun Jul 17, 2005 12:42 am

What if i try this???

Post by dynamic_desi »

Hi,

I think it will be a mess if i try and display binary data to HTML webpage. Now instead of uploading the image to mysql, i am trying to upload it to a folder and i save the path to that uploaded file on the server.

The upload part works fine, plus the query saves the path to the server too. But it has some troubles retreving the image. Seems like i am not saving the path in a right format on the mysql server. I've defined the pathname coloumn as"VARCHAR".

Is that correct??? If not what would be the best way to do it??


Code: Select all

$doc_directory = "/var/www/html/";
$my_file ="images/".$fileName;
1
$copy_path = $doc_directory.$my_file;

if(!copy($tempName, $copy_path))
{
echo "file upload failed";
}

else{ echo"file uploaded at $fileName";}

$query = "insert into product (itemName, itemPrice, subcatid, imagepath, description)".
"values ('$itemName', '$itemPrice', '$subcatid', '$fileName', '$itemdesc')";
That is my upload query.

And here's my retrive query

Code: Select all

$query = "select * from product where subcatid = '$subcatid'";
$result  = mysql_query($query) or die('Error, query failed');
//$row = mysql_fetch_array($result);

?>
<table  border="0" cellspacing="1" cellpadding="1">
<?
while ($row = mysql_fetch_row($result))
{
extract($row)
echo $row[5];
?>
<tr>
<td width="150" height="120" align="center"><?php echo '<img src="/images/$row[5]" width="150" height="150" >';
Please Help!!

This time i've tried using the PHP tags from the menu, i hope it works...lol

dynamic_desi


feyd | this is the final time I fix your posted code. I'd suggest you study how bbcode tags work..
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

varchar is correct.. however, your retrieval seems a bit odd.. Are you sure the image filename is stored in the 6th field returned?
dynamic_desi
Forum Newbie
Posts: 8
Joined: Sun Jul 17, 2005 12:42 am

hi

Post by dynamic_desi »

Hey,

Yes...the retreival is pretty respective to the way i am storing the path name into the database.

Before displaying the image, if i try and use

echo $row[5] ;

just to debug if it returns the correct path name, the entire script crashes.

its driving me nuts...lol

dynamic_desi
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

your echo in the retrieval is using a single quote string. Single quote strings do not parse any variables stored within them. Also, running extract() on a numeric array will do nothing.
Post Reply