By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,066 Members | 1,841 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,066 IT Pros & Developers. It's quick & easy.

trying to display image from mysql database

P: n/a
Hello Group,

Perhaps you can help me. I have a mysql db, that holds images.
Images are encoded using base64_decode/encode, etc. Image data seems
fine.
I have a view.php page that is supposed to show the image, and an
image.php page that accesses
the database, retrives the image data, and then (theoretically) prints
the decoded data to the page.
below is the view.php page code: problem area the img tag src no
worky.

What am i missing (having stolen the idea for this from the web!!!!
thanks,
eric
/************************************************** *****************/
<html>
<head>
<title>Get The Image</title>
<link rel="stylesheet" type="text/css" href="css/csssmall.css" />
</head>
<body>
<center><img src="testimage/Sunset.jpg" width="300" border="1"
alt="Image of Sunset"></center>
<p class="para-color"><?php echo 'Beautiful Image' ?></p>
<br>
<center><img src="image.php?img=3" width="200" border="1" alt="Image of
Thelma Todd"></center>
<p class="para-color"><?php echo '\'Nother Beautiful Image' ?></p>
</body>
</html>
/************************************************** ****************/

below is the image.php code:
/************************************************** ********************
<?php
$dbhost = 'localhost';
$dbuser = 'auser';
$dbpass = 'apassword';
$dbcnx = @mysql_connect($dbhost,$dbuser,$dbpass);

if (!$dbcnx)
{
echo( "connection to database server failed!");
exit();
}
if (! @mysql_select_db("portfolio") )
{
echo( "Image Database Not Available!" );
exit();
}
?>
<?php
$img = $_REQUEST["img"];
?>

<?php
$result = @mysql_query("SELECT * FROM images WHERE id=" . $img . "");

if (!$result)
{
echo("Error performing query: " . mysql_error() . "");
exit();
}
while ( $row = @mysql_fetch_array($result) )
{
$imgid = $row["id"];
$encodeddata = $row["sixfourdata"];
$title = $row['title'];
}
?>
<?php
//echo $encodeddata;print??
echo base64_decode($encodeddata);
//echo $prefix . $encodeddata;
?>
/************************************************** ***********************

Dec 9 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
eholz1 wrote :
Hello Group,

Perhaps you can help me. I have a mysql db, that holds images.
Images are encoded using base64_decode/encode, etc. Image data seems
fine.
I have a view.php page that is supposed to show the image, and an
image.php page that accesses
the database, retrives the image data, and then (theoretically) prints
the decoded data to the page.
below is the view.php page code: problem area the img tag src no
worky.

What am i missing (having stolen the idea for this from the web!!!!
thanks,
eric
/************************************************** *****************/
<html>
<head>
<title>Get The Image</title>
<link rel="stylesheet" type="text/css" href="css/csssmall.css" />
</head>
<body>
<center><img src="testimage/Sunset.jpg" width="300" border="1"
alt="Image of Sunset"></center>
<p class="para-color"><?php echo 'Beautiful Image' ?></p>
<br>
<center><img src="image.php?img=3" width="200" border="1" alt="Image of
Thelma Todd"></center>
<p class="para-color"><?php echo '\'Nother Beautiful Image' ?></p>
</body>
</html>
/************************************************** ****************/

below is the image.php code:
/************************************************** ********************
<?php
$dbhost = 'localhost';
$dbuser = 'auser';
$dbpass = 'apassword';
$dbcnx = @mysql_connect($dbhost,$dbuser,$dbpass);

if (!$dbcnx)
{
echo( "connection to database server failed!");
exit();
}
if (! @mysql_select_db("portfolio") )
{
echo( "Image Database Not Available!" );
exit();
}
?>
<?php
$img = $_REQUEST["img"];
?>

<?php
$result = @mysql_query("SELECT * FROM images WHERE id=" . $img . "");

if (!$result)
{
echo("Error performing query: " . mysql_error() . "");
exit();
}
while ( $row = @mysql_fetch_array($result) )
{
$imgid = $row["id"];
$encodeddata = $row["sixfourdata"];
$title = $row['title'];
}
?>
<?php
//echo $encodeddata;print??
echo base64_decode($encodeddata);
//echo $prefix . $encodeddata;
?>
/************************************************** ***********************
Why do you always open/close the PHP tags? There's no use for it.

The matter in here is that you forgot to tell your browser that the data you're
sending are not plain text or PHP rendering, but a picture.
Do it by writing :

header('Content-Type: image/jpeg');
echo base64_decode($encodeddata);

--
Naixn
http://fma-fr.net
Dec 9 '06 #2

P: n/a
eholz1 wrote:
Hello Group,

Perhaps you can help me. I have a mysql db, that holds images.
Images are encoded using base64_decode/encode, etc. Image data seems
fine.
And I'm curious why your data is base64 encoded rather than simply
stored as a binary blob.
base64 is quite inefficient (takes up a lot more storage space).

Dec 9 '06 #3

P: n/a
Hello bkdotcom
do I have to encode/decode the image data if I use a "binary blob"?,

tnx,

eholz1

BKDotCom wrote:
eholz1 wrote:
Hello Group,

Perhaps you can help me. I have a mysql db, that holds images.
Images are encoded using base64_decode/encode, etc. Image data seems
fine.

And I'm curious why your data is base64 encoded rather than simply
stored as a binary blob.
base64 is quite inefficient (takes up a lot more storage space).
Dec 9 '06 #4

P: n/a
Hello naixn,

I removed the "extra" php tags (that is what I get from copying someone
else's code!)
and now the image displays. thanks. I will look at blob vs. base64

Thanks to all,

eholz1

naixn wrote:
eholz1 wrote :
Hello Group,

Perhaps you can help me. I have a mysql db, that holds images.
Images are encoded using base64_decode/encode, etc. Image data seems
fine.
I have a view.php page that is supposed to show the image, and an
image.php page that accesses
the database, retrives the image data, and then (theoretically) prints
the decoded data to the page.
below is the view.php page code: problem area the img tag src no
worky.

What am i missing (having stolen the idea for this from the web!!!!
thanks,
eric
/************************************************** *****************/
<html>
<head>
<title>Get The Image</title>
<link rel="stylesheet" type="text/css" href="css/csssmall.css" />
</head>
<body>
<center><img src="testimage/Sunset.jpg" width="300" border="1"
alt="Image of Sunset"></center>
<p class="para-color"><?php echo 'Beautiful Image' ?></p>
<br>
<center><img src="image.php?img=3" width="200" border="1" alt="Image of
Thelma Todd"></center>
<p class="para-color"><?php echo '\'Nother Beautiful Image' ?></p>
</body>
</html>
/************************************************** ****************/

below is the image.php code:
/************************************************** ********************
<?php
$dbhost = 'localhost';
$dbuser = 'auser';
$dbpass = 'apassword';
$dbcnx = @mysql_connect($dbhost,$dbuser,$dbpass);

if (!$dbcnx)
{
echo( "connection to database server failed!");
exit();
}
if (! @mysql_select_db("portfolio") )
{
echo( "Image Database Not Available!" );
exit();
}
?>
<?php
$img = $_REQUEST["img"];
?>

<?php
$result = @mysql_query("SELECT * FROM images WHERE id=" . $img . "");

if (!$result)
{
echo("Error performing query: " . mysql_error() . "");
exit();
}
while ( $row = @mysql_fetch_array($result) )
{
$imgid = $row["id"];
$encodeddata = $row["sixfourdata"];
$title = $row['title'];
}
?>
<?php
//echo $encodeddata;print??
echo base64_decode($encodeddata);
//echo $prefix . $encodeddata;
?>
/************************************************** ***********************

Why do you always open/close the PHP tags? There's no use for it.

The matter in here is that you forgot to tell your browser that the data you're
sending are not plain text or PHP rendering, but a picture.
Do it by writing :

header('Content-Type: image/jpeg');
echo base64_decode($encodeddata);

--
Naixn
http://fma-fr.net
Dec 9 '06 #5

P: n/a
eholz1 wrote:
Hello Group,

Perhaps you can help me. I have a mysql db, that holds images.
Images are encoded using base64_decode/encode, etc. Image data seems
fine.
I have a view.php page that is supposed to show the image, and an
image.php page that accesses
the database, retrives the image data, and then (theoretically) prints
the decoded data to the page.
below is the view.php page code: problem area the img tag src no
worky.

What am i missing (having stolen the idea for this from the web!!!!
thanks,
eric
/************************************************** *****************/
<html>
<head>
<title>Get The Image</title>
<link rel="stylesheet" type="text/css" href="css/csssmall.css" />
</head>
<body>
<center><img src="testimage/Sunset.jpg" width="300" border="1"
alt="Image of Sunset"></center>
<p class="para-color"><?php echo 'Beautiful Image' ?></p>
<br>
<center><img src="image.php?img=3" width="200" border="1" alt="Image of
Thelma Todd"></center>
<p class="para-color"><?php echo '\'Nother Beautiful Image' ?></p>
</body>
</html>
/************************************************** ****************/

below is the image.php code:
/************************************************** ********************
<?php
$dbhost = 'localhost';
$dbuser = 'auser';
$dbpass = 'apassword';
$dbcnx = @mysql_connect($dbhost,$dbuser,$dbpass);

if (!$dbcnx)
{
echo( "connection to database server failed!");
exit();
}
if (! @mysql_select_db("portfolio") )
{
echo( "Image Database Not Available!" );
exit();
}
?>
<?php
$img = $_REQUEST["img"];
?>

<?php
$result = @mysql_query("SELECT * FROM images WHERE id=" . $img . "");

if (!$result)
{
echo("Error performing query: " . mysql_error() . "");
exit();
}
while ( $row = @mysql_fetch_array($result) )
{
$imgid = $row["id"];
$encodeddata = $row["sixfourdata"];
$title = $row['title'];
}
?>
<?php
//echo $encodeddata;print??
echo base64_decode($encodeddata);
//echo $prefix . $encodeddata;
?>
/************************************************** ***********************
I have never like storing images in databases - I generally have a special
directory where I have images that are named numberically or sometimes some type
of unique_id. Then I store the the decription and filename only in the database
and use some sort of server-side processing to "display" the appropriate image.

Requires much less space in the database and much less strain on the db engine
to retrieve that much information - especially if you need to do the same thing
hundreds of times a day.

--
Michael Austin.
Database Consultant
Dec 9 '06 #6

P: n/a
eholz1 wrote:
Hello bkdotcom
do I have to encode/decode the image data if I use a "binary blob"?,

tnx,

eholz1

BKDotCom wrote:
>>eholz1 wrote:
>>>Hello Group,

Perhaps you can help me. I have a mysql db, that holds images.
Images are encoded using base64_decode/encode, etc. Image data seems
fine.

And I'm curious why your data is base64 encoded rather than simply
stored as a binary blob.
base64 is quite inefficient (takes up a lot more storage space).

No.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Dec 10 '06 #7

P: n/a
Michael Austin wrote:
eholz1 wrote:
>Hello Group,

Perhaps you can help me. I have a mysql db, that holds images.
Images are encoded using base64_decode/encode, etc. Image data seems
fine.
I have a view.php page that is supposed to show the image, and an
image.php page that accesses
the database, retrives the image data, and then (theoretically) prints
the decoded data to the page.
below is the view.php page code: problem area the img tag src no
worky.

What am i missing (having stolen the idea for this from the web!!!!
thanks,
eric
/************************************************** *****************/
<html>
<head>
<title>Get The Image</title>
<link rel="stylesheet" type="text/css" href="css/csssmall.css" />
</head>
<body>
<center><img src="testimage/Sunset.jpg" width="300" border="1"
alt="Image of Sunset"></center>
<p class="para-color"><?php echo 'Beautiful Image' ?></p>
<br>
<center><img src="image.php?img=3" width="200" border="1" alt="Image of
Thelma Todd"></center>
<p class="para-color"><?php echo '\'Nother Beautiful Image' ?></p>
</body>
</html>
/************************************************** ****************/

below is the image.php code:
/************************************************** ********************
<?php
$dbhost = 'localhost';
$dbuser = 'auser';
$dbpass = 'apassword';
$dbcnx = @mysql_connect($dbhost,$dbuser,$dbpass);

if (!$dbcnx)
{
echo( "connection to database server failed!");
exit();
}
if (! @mysql_select_db("portfolio") )
{
echo( "Image Database Not Available!" );
exit();
}
?>
<?php
$img = $_REQUEST["img"];
?>

<?php
$result = @mysql_query("SELECT * FROM images WHERE id=" . $img . "");

if (!$result)
{
echo("Error performing query: " . mysql_error() . "");
exit();
}
while ( $row = @mysql_fetch_array($result) )
{
$imgid = $row["id"];
$encodeddata = $row["sixfourdata"];
$title = $row['title'];
}
?>
<?php
//echo $encodeddata;print??
echo base64_decode($encodeddata);
//echo $prefix . $encodeddata;
?>
/************************************************** ***********************


I have never like storing images in databases - I generally have a
special directory where I have images that are named numberically or
sometimes some type of unique_id. Then I store the the decription and
filename only in the database and use some sort of server-side
processing to "display" the appropriate image.

Requires much less space in the database and much less strain on the db
engine to retrieve that much information - especially if you need to do
the same thing hundreds of times a day.
It's not that much different in storage space, and access isn't all the
much different.

I find there can be advantages to storing in the database, as well as
disadvantages. I use them sometimes, and other times not.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Dec 10 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.