473,837 Members | 1,536 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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($qu ery, $conn);

echo "<table width=\"50%\" align=\"center\ ">";
while($row = mysql_fetch_arr ay($result))
{
echo "<tr>";
echo "<td>$row[0]</td>";
echo "<td>$row[1]</td>";
echo "<td>$row[2]</td>";
}
echo "</table>";
mysql_free($res ult);
mysql_close($co nn);

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 3513
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($qu ery, $conn);

echo "<table width=\"50%\" align=\"center\ ">";
while($row = mysql_fetch_arr ay($result))
{
echo "<tr>";
echo "<td>$row[0]</td>";
echo "<td><IMG SRC=\"data:imag e/jpeg;base64," . base64
_encode($row[1]) . "\"></td>";
echo "<td><IMG SRC=\"data:imag e/jpeg;base64," . base64
_encode($row[2]) . "\"></td>";
}
echo "</table>";
mysql_free($res ult);
mysql_close($co nn);
?>

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=12 3">

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($qu ery, $conn);

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


"<a href=\"fetchIma ge.php?imageId= {$row[0]}\"><img
src=\"fetchImag e.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
2078
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, including searches within given price ranges for products? There will be product images too, thumbnails and larger images? All the data will be on a single server, on the same
4
1162
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 there another control I should use to databind? is there a way to databind the imageurl property? I am using VS 2005 but if you know how to do it in 2003 that helps too.
1
1887
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 both in terms of speed and storage for saving images in database (using SQL server). Currently I am using the following algorithms. Question is can I improve this? 'Get length of stream (lengt of file) in bytes InByteCount = loFile.Length() 'Reallocate storage space for an array variable to...
2
1583
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 unable to do so.Please help me I am using C# The code is as // Put user code to initialize the page here int ImgID = System.Convert.ToInt32(Request.QueryString);
0
1434
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
41701
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
1266
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 MySQL Database using the filenames of the images to link them to database records. Please help on how I can go about this. Thanks.
0
1298
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 Server. For this i code: //get the image id from the url string Imagename = DropDownList1.SelectedItem .Text ; //string Imagename = Request.QueryString; //build our query statementworkstation id=SOFTWARE3;packet size=4096;integrated security=SSPI;data source=SOFTWARE3;persist security...
7
2005
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 bunch of images stored in a database as BLOBs. At any given point in time a subset of those images is displayed on- screen. At certains times I want to swap out those on screen with new ones from the database, and do some as seamlessly as possible. So what I've tried to do is first create Image...
3
1709
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)){} //die('ERROR:'.mysql_error());
0
9839
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10871
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10564
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10268
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9396
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5668
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5846
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4474
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4039
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.