My images stored in directory, but path of the image is stored in Mysql table, how can i display that images using php
Are the images stored in a location that is accessible from the internet?
If they are, you simply create an URL from the path that is stored in the database, and use that URL in an img
element on your page.
If they are not, you need a script which serves the image to your clients. Create a PHP script, e.g. serveimage.php
. In your pages, use this page in the img
element, and pass it a image id:
<img src="serveimage.php?id=3" />
In serveimage.php, look up the image in the database and output the contents of the image.
Simple just Fetch record from database as usual we do.
As u say path is in database then just give image path fetched from database in image source.
<?php
$yourquery = "select image_path from table";
$result = mysql_query($yourquery );
$imgSrc = mysql_fetch_array($result);
foreach($imgsrc as $path)
{
$imgSrc = $path;//Retrieve image path.
echo "<img src='$imgSRc' />";
}
?>
Steps:
$row
<img src="<?php print $row[image_field] ?>" alt="" />
tag.I won't paste any code since everyone else has shown you.
One thing you have to watch out for is the image path stored in the database and the actual image path you need to use for the image src so it can be found.
For example, if an image path in the database is:
2011/myimage.jpg
but the actual URL path is:
/images/2011/myimage.jpg
Then don't forget to update the path before outputting.
It's kind of obvious but there are several gotchas depending on the case.