How to Display All the Images Stored Inside a Database

How to display all the images stored inside a database

From what i understand from your post is that uploading and storing isn't a problem, but showing the images is. That's probably because you're using vars that are not set, so no results kan be found in the database. If i misunderstood let me know.

<?php
// No ID
$image = mysql_query("SELECT * FROM images ORDER BY id DESC");
?>

Also look at what Prof83 says. Ignore my post if your script works with just one image.

Last but not least, if you're using different filetypes, also echo the correct MIME format in the header.

Update
I combined both answers.

Edit your loop:

<?php
while($row = mysql_fetch_assoc($image))
{
echo '<img src="img.php?id='.$row["id"].'">';
}
?>

Create a page name img.php

<?php
$query = mysql_query("SELECT image FROM images WHERE id = ".$_GET['id']);
$row = mysql_fetch_assoc($query);
header("Content-type: image/jpeg");
echo $row['image'];
?>

display images stored in SQL database

The following link may helps you.

PHP- Image upload to MySQL

set blob type for data column.

How can I store and retrieve images from a MySQL database using PHP?

First you create a MySQL table to store images, like for example:

create table testblob (
image_id tinyint(3) not null default '0',
image_type varchar(25) not null default '',
image blob not null,
image_size varchar(25) not null default '',
image_ctgy varchar(25) not null default '',
image_name varchar(50) not null default ''
);

Then you can write an image to the database like:

/***
* All of the below MySQL_ commands can be easily
* translated to MySQLi_ with the additions as commented
***/
$imgData = file_get_contents($filename);
$size = getimagesize($filename);
mysql_connect("localhost", "$username", "$password");
mysql_select_db ("$dbname");
// mysqli
// $link = mysqli_connect("localhost", $username, $password,$dbname);
$sql = sprintf("INSERT INTO testblob
(image_type, image, image_size, image_name)
VALUES
('%s', '%s', '%d', '%s')",
/***
* For all mysqli_ functions below, the syntax is:
* mysqli_whartever($link, $functionContents);
***/
mysql_real_escape_string($size['mime']),
mysql_real_escape_string($imgData),
$size[3],
mysql_real_escape_string($_FILES['userfile']['name'])
);
mysql_query($sql);

You can display an image from the database in a web page with:

$link = mysql_connect("localhost", "username", "password");
mysql_select_db("testblob");
$sql = "SELECT image FROM testblob WHERE image_id=0";
$result = mysql_query("$sql");
header("Content-type: image/jpeg");
echo mysql_result($result, 0);
mysql_close($link);

How to get images stored in a database and display them on a HTML page using Flask?

I know this isnt the answer you're looking for, but storing the actual images on disk, and only retrieving the filename or location from the database also works.

flask can return this image very easily using

        return flask.redirect(flask.url_for('PATH', filename='YOURIMAGE.png'), code=301)

I guess you have a specific reason to store the images in a database, but maybe this will help you anyway. Note this doesnt return html with the image embedded, but the image only.

How to display all the images stored in my MVC folder and their path is in database table using MVC razor?

Here is my Model.I have Added a list Property to takes list of images in the folder ("~/Content/Files")

    public class Prop 
{
public string Fileimg { get; set; }
public IEnumerable<string> Images { get; set; }

}

Here is my ActionResult of controller to Display List of images in folder ("~/Content/Files")

  public ActionResult List(Prop obj)
{
obj.Images=Directory.EnumerateFiles(Server.MapPath("~/Content/Files/"))
.Select(fn => "~/Content/Files/" + Path.GetFileName(fn));
var objfile = new DirectoryInfo(Server.MapPath("~/Content/Files/"));
var file = objfile.GetFiles("*.*");
return View(obj);
}

Here is My View to loop all the images and display all in the view

@model imageMVC.Models.Prop

<table style="border: solid">
<tr>
<th>IMAGES</th>
</tr>
@foreach (var img in Model.Images)
{
<tr>
<td>
<img src="@Url.Content(img)"/>
</td>

</tr>
}
</table>

All the images in that folder are set in the Model property Images and I am Looping all the images and Displaying it in view.



Related Topics



Leave a reply



Submit