Jump to content

Displaying images from database.


katsu

Recommended Posts

Hi

 

I'm doing a web application that stores pictures in a database, I don't have a idea how to echo it in a dynamically generated table from the database. I tried several ways:

 

1.- Using an external php that loads the image, something like this :

<?php
define("DBSERVER", "server");
define("DBUSER", "user");
define("DBPASS", "pass");
define("DBNAME", "db");

$database= new mysqli(DBSERVER,DBUSER, DBPASS,DBNAME);


$query = $database->query("select * from pictures where id = 1");
$data = $query->fetch_assoc();
header('Content-Type: image/jpeg');
echo $data['pic1'];

 

Then, in the table, I put <img src="image1.php" /> and it works on opera, but it doesn't on firefox or internet explorer

 

2.- Loading it with ajax dynamically, image didn't show up

 

3.-  like the first option, but it echos the img tag with the image from the db like this :

<?php
define("DBSERVER", "server");
define("DBUSER", "user");
define("DBPASS", "pass");
define("DBNAME", "db");

$database= new mysqli(DBSERVER,DBUSER, DBPASS,DBNAME);


$query = $database->query("select * from pictures where id = 1");
$data = $query->fetch_assoc();

echo $data['pic1'];

 

 

I know I can echo an image adding the header('Content-Type: image/jpeg'); in the php code I've done it before, but I want it to show them inside a content table.

 

I appreciate any help

Regards,

Link to comment
https://forums.phpfreaks.com/topic/196428-displaying-images-from-database/
Share on other sites

I prefer storing the images in a folder and simply putting the path/name in the db; however...

 

echo "<IMG SRC='" . $var_containing_pic_name . "'>";

That is exactly how I do it.  I just put the filename in the database and echo the filename inside the path to the file.  Thtat way you don't have to worry about the path becuase that could change

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.