|
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 PagesH999 - Additional PHP MaterialS154 - MySQL - Designing an SQL Database SystemH113 - Using MySQL Databases in PHP PagesH309 - PHP - Maps, Graphics and Geographics
Some other Articles
SnaggingWinter at Well House Manor - Open HousesSwipe cards for hotel rooms - Security issuesBratton and Edington new town, WiltshireDisplay an image from a MySQL database in a web page via PHPGlobal, Superglobal, Session variables - scope and persistance in PHPAutumn leaves in Wiltshire - PotterneClustering, load balancing, mod_rewrite and mod_proxyCourse Joining package - updatedA 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).
|
|