Training, Open Source computer languages

PerlPythonMySQLTclRubyC & C++LuaJavaTomcatPHPhttpdLinux

Search our site for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Display an image from a MySQL database in a web page via PHP

There's lots of clever scripts around to tell you how to get images in and out of databases, but nothing simple to show you the principles of including such an image via (say) PHP in a web page. So here goes.

you need TWO URLs - you need the HTML page that's going to contain the image, AND you need a second URL - PHP in my example - that's going to retrieve the image and feed it out as part of the page. You CANNOT feed out both the HTML and the image from the same http request.

Here's a sample HTML page we're going to include an image in:

<html>
<head>
<title>Demo of Database Image in a page</title>
</head>
<body>
Here is your picture:<br>
<img src=picscript.php?imname=potwoods><br>
Example by Well House Consultants
</body>
</html>


Then you need the PHP script - called picscript.php in the same directory in my example:

<?php
mysql_connect("localhost","wellho","xxxxxxx");
mysql_select_db("wellho");
$image = stripslashes($_REQUEST[imname]);
$rs = mysql_query("select * from im_library where filename=\"".
addslashes($image).".jpg\"");
$row = mysql_fetch_assoc($rs);
$imagebytes = $row[imgdata];
header("Content-type: image/jpeg");
print $imagebytes;
?>


If you want to try it out, then the HTML page is here.

Want to take this further?

As a next step, I would validate the connection after the mysql_connect, and check that an image really had been returned by the mysql_query ... substituting a default image if not.

We host a complete working example of image upload, save to database, select from database and display all via a PHP script. See article on the subject

There's a forum discussion here.
(written 2006-11-22 16:48:22)

 
Associated topics are indexed under
H113 - Using MySQL Databases in PHP Pages
H999 - Additional PHP Material
S154 - MySQL - Designing an SQL Database System
H113 - Using MySQL Databases in PHP Pages
H309 - PHP - Maps, Graphics and Geographics

Back to
Global, Superglobal, Session variables - scope and persistance in PHP
Previous and next
or
Horse's mouth home
Forward to
Bratton and Edington new town, Wiltshire
Some other Articles
Snagging
Winter at Well House Manor - Open Houses
Swipe cards for hotel rooms - Security issues
Bratton and Edington new town, Wiltshire
Display an image from a MySQL database in a web page via PHP
Global, Superglobal, Session variables - scope and persistance in PHP
Autumn leaves in Wiltshire - Potterne
Clustering, load balancing, mod_rewrite and mod_proxy
Course Joining package - updated
A tale of a wee wall
1893 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

© WELL HOUSE CONSULTANTS LTD., 2008: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 0800 043 8225 or 01225 708225 • FAX: 0845 8382 405 or 01225 707126 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho