472,347 Members | 2,370 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,347 software developers and data experts.

PHP, MySQL and images stored in database

Hello,

I have a problem that I have not been able to solve, even after
searching the web. I have stored couple of images in the MySQL database
and I am trying to get them displayed on a web page with PHP.

Now, before we go any further, I want to make it clear that I am very
aware of the current debate about storing images in the database. I
understand both sides, however this is something I want to do for fun,
as well as good learning.

Now that that is out of the way, I will outline my problem. I deposited
the images into MySQL using phpMyAdmin, which of course wrote the images
in using hex. I, then, wrote a simple script(SELECT * from webImages)
that seems to pull the images out of the database, but the it displays
them in the BINARY format. My question to you is, what do I need to
change in script in order to get the images displayed as images?
The relevant code and database design are below.

Table design is this:

(id INT(3) NOT NULL auto_increment PRIMARY KEY,
thumb blob NOT NULL,
image mediumblob NOT NULL) Type=MyISAM;

The code is the plain code for getting anything out of the database,
error checking omitted:

$conn = mysql_connect($host, $user, $pass);
mysql_select_db($somedb, $conn);

$query = "SELECT * FROM webImages";
$result = mysql_query($query, $conn);

echo "<table width=\"50%\" align=\"center\">";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>$row[0]</td>";
echo "<td>$row[1]</td>";
echo "<td>$row[2]</td>";
}
echo "</table>";
mysql_free($result);
mysql_close($conn);

Last thing, I did try to use base64_decode() function, but I ended not
getting anything displayed.
Thanks in advance,
Srdjan
Jul 17 '05 #1
3 3370
You can't do that... You'd be outputting the binary data right
into a text based web page....

If you output an image from a PHP script then thats all you can
output, you can mix image(binary) and html(text).

// Connect to database.
// Fetch image data.
// Close database connection.
header( "Content-Type: image/jpeg" ); // If it was a jpeg
echo $row[1];

Actually i lied, you "can" output both image data and HTML from
one script, in one go. You need to encode the image on a <img
src="data:image/jpeg;base64," . $image_encoded > ...

So in your case.

<?
$conn = mysql_connect($host, $user, $pass);
mysql_select_db($somedb, $conn);

$query = "SELECT * FROM webImages";
$result = mysql_query($query, $conn);

echo "<table width=\"50%\" align=\"center\">";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>$row[0]</td>";
echo "<td><IMG SRC=\"data:image/jpeg;base64," . base64
_encode($row[1]) . "\"></td>";
echo "<td><IMG SRC=\"data:image/jpeg;base64," . base64
_encode($row[2]) . "\"></td>";
}
echo "</table>";
mysql_free($result);
mysql_close($conn);
?>

But why you would want to do that is beyond me. Im really not
sure what the benifit is. It could infact be a very bad idea.

----------------------------------------------
Posted with NewsLeecher v2.0 Beta 4
* Binary Usenet Leeching Made Easy
* http://www.newsleecher.com/?usenet
----------------------------------------------

Jul 17 '05 #2
Srdjan Pejic wrote:
[snip]
... My question to you is, what do I need to
change in script in order to get the images displayed as images?
write a script that takes an imageId as parameter, selects image from
DB, sends a content-type header, and finally sends the raw imagedata.

In your html, you would have an image-reference like:
<img src="fetchImage.php?imageId=123">

The relevant code and database design are below.
Table design is this:

(id INT(3) NOT NULL auto_increment PRIMARY KEY,
thumb blob NOT NULL,
image mediumblob NOT NULL) Type=MyISAM;
add a "type" column, which you get from $_FILES[]['type'] and send to
the browser as:
Header("Content-Type: $type");

It is important, but not vital if you only store jpg/gif/png images.
Atleast IExplorer seems to autodetect the type on images.

Perhaps you also would save the original filename, and/or a short
description.

The code is the plain code for getting anything out of the database,
error checking omitted:

$conn = mysql_connect($host, $user, $pass);
mysql_select_db($somedb, $conn);

$query = "SELECT * FROM webImages"; SELECT id FROM webImages $result = mysql_query($query, $conn);

echo "<table width=\"50%\" align=\"center\">";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>$row[0]</td>";


"<a href=\"fetchImage.php?imageId={$row[0]}\"><img
src=\"fetchImage.php?imageId={$row[0]}&thumb=1\"></a>"

or whatever. Once you got the script to retrieve the image, you just
treat it as any other file on the server.
/Bent
Jul 17 '05 #3
Thank you both for replying. To answer your question, Nemon, the reason
I am doing it purely for research. I wanted to find an alternative
solution to storing images for a website and I thought that storing them
in the database would make things easier. I am still undecided whether
this is a good solution or not. I am going to try both approaches to see
which one works best, both ease of programming and performance wise. In
the end, I just may stick with what seems to be the most common solution
to this problem and that is storing the filepath to an image instead.
Again thanks for all your help.

Srdjan
Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: PR | last post by:
Searchable product images + prices database? What system should I use to put a database on the WEB which shows images and allows searches,...
4
by: Vonnie | last post by:
I have images stored in SQL (not paths or urls) I need to show them on webpages but the image control only seems to have a imageurl property. Is...
1
by: Al | last post by:
Hi, I need to store literally thousands if images and scanned document in the Database. I would like to know what is the most efficient algorithm...
2
by: Anitha | last post by:
Hi All, How to retrieve images stored in Access database. I am storing images(jpeg) as OleObject. I want display them on my web page. I am...
0
by: ashishbaral | last post by:
Hi, I am having some images stored in an access database as OLE objects. I want to display them in a JSP page. how to do this ? Please Help !!
4
by: kardon33 | last post by:
I just installed Mysql for php and Apache on Windows, Where are the mysql db stored when i create them.
1
by: CodedSammy | last post by:
Hello. I am developing a solution in Visual Basic which will require me to display images stored in a disk files along with the content of my...
0
by: punitshrivastava | last post by:
Hi to All Senior's, I am working in Asp.net with C#. I am newbie in C#. So i want to fetch mutiple images from database .Database i am using is Sql...
7
by: Keith Hughitt | last post by:
Hi all, I am having trouble preloading images in a javascript application, and was wondering if anyone had any suggestions. Basically I have a...
3
by: Hataf | last post by:
Hi! I am having a problem in displaying images from database. This the code gallery.php <?php session_start(); if(isset($_SESSION)){}...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...

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.