cant display image from mysql db - php

I am having trouble displaying images from my db, when i show it without the header i get the image data but when i give it the header('Content-type: image/jpeg'); nothing is displayed.
in my db i have the image column as a longblob.
$host='******';
$user='******';
$pass='******';
$db='******';
$tbl_name='******';
//connect to db
mysql_connect($host, $user, $pass);
mysql_select_db($db);
$id=addslashes($_REQUEST['id']);
$image=mysql_query("SELECT * FROM $tbl_name WHERE id='$id'");
$image=mysql_fetch_assoc($image);
$image=$image['image'];
header('Content-type: image/jpeg');
echo "<img src='$image' />";
?>`

Remove the img tag. You want to output the raw data only:
echo $image;
Note that addslashes() is not adequate protection against SQL injection. You would have to use mysql_real_escape_string() (or intval()).

Related

How can I load a picture in a img tag from bytea field, using PHP?

how can I load a picture in a img tag from bytea field, using php? I´m using postgresql database
What I have tried:
<?php
// Connect to the database
$con_qupos = pg_connect('host=192.168.60.254 dbname=dbqupos user=postgres password=12345') or die(pg_last_error());
// Get the bytea data
$res = pg_query('select foto from "schSuperCristian".articulo_foto where codigo_referencia='."'8711600305960'");
$raw = pg_fetch_result($res, 'foto');
// Convert to binary and send to the browser
header('Content-type: image/jpeg');
echo pg_unescape_bytea($raw);
?>

Image retrieved in PHP is not displaying properly

I am having trouble with displaying images with php. I have tried this question, but it's still not displaying properly.
I have a PHP file that is handling the HTML format, the code for the image part is as follows:
while($row = mysql_fetch_array($result)) {
$partNo = $row['partNo'];
echo "<tr>";
echo '<td> <img src="getThumb.php?id =\''. $partNo .'\' width="50" height="50" /> </td>';
My getThumb.php is as follows:
<?php
$id = $_GET['id'];
$con = mysql_connect("localhost", "root", "root") or die("Unable to connect to server");
mysql_select_db("bmworld.mu");
$sql = "SELECT thumbnail FROM part WHERE partNo=$id" or die ("Could not fetch thumbnail");
$result = mysql_query("$sql");
$row = mysql_fetch_object($result);
mysql_close($con);
header("Content-type: image/jpeg");
echo $row['thumbnail'];
?>
The results is as follows:
http://postimg.org/image/xqcq16ucf/
My image is of type 'blob' saved in MySQL, and not a path to the image folder. I want to retrieve that blob from MySQL, and display it directly from the retrieved blob instead of a path to the image, just like the other columns of data from my database table. How can I do this?
There are following problems may be in your code:-
your image path is not correct (what you are saving in your database).
Either you are saving your image directly with blob data-type and at the time retrieval you face some problem.
For the first problem try to hardcode path of your image and check, if working then your path saving to databsae is not correct and you have to correct.
For second problem please check this link:PHP display image BLOB from MySQL. It will definitely help you. Thanks.
are u sure that getThumb.php has good file location?
<img src="getThumb.php?id =\''. $partNo .'\' width="50" height="50" />

Displaying PNG images from MySQL

I'm trying to display an image from a MySQL database and my code works fine if the image is a JPG, but if I modify the content-type to display a PNG, it doesn't work...
How do I make it work for PNG??
<?php
// configuration
require("../includes/config.php");
header('Content-type: image/JPG');
$username = "xxxxx";
$password = "*****";
$host = "localhost";
$database = "database";
#mysql_connect($host, $username, $password) or die("Can not connect to database: ".mysql_error());
#mysql_select_db($database) or die("Can not select the database: ".mysql_error());
$query = mysql_query("SELECT * FROM images");
while($row = mysql_fetch_array($query))
{
echo $row['image'];
}
?>
Also, is it possible to display the image along with its name and description?
Thanks!
If you don't mind not supporting older browsers, you can use data urls to display your images and a description http://en.wikipedia.org/wiki/Data_URI_scheme.
<figure>
<img src="data:image/png;base64,<?php echo base64_encode($row['image']); ?>" alt="Your alternative text" />
<figcaption>Some more description</figcaption>
</figure>
Saving images in a database is not very useful in almost all cases. You should be carefull with upper and lower letters in the mimetype, see http://de.selfhtml.org/diverses/mimetypen.htm (it's german, but you will be able to read the list). And as a last advice - look at the mysqli_* functions http://www.php.net/manual/en/book.mysqli.php.
// Edit: Just an idea, but if you have multiple images in the database your image might be broken, because you just put them all into one image. This will not work! With your code, you can only display one image at once.

Displaying images from a mysql database

i am relatively new to php. I have a problem with displaying images in my browser ( google chrome) after retrieving blob data from mysql database.
Basically the following code works when the slashes are added in front of the echo at the bottom. However i have followed other online tutorials and the tutor has been able to display their images without the use of slashes whilst i am unable to get the image up. I just wondered what the standard rule is? Another thing to add - when i do fail to get the images up in the browser i get instead a ting thumbnail. I would really appreciate if anybody could tell me how to reliably display images. The site i wish to create is just about images. So its kind of fundamental. Thanks a lot in advance for your time.
<?php
$conn = mysql_connect ("localhost","root","arctic123");
$db = mysql_select_db ("user_images", $conn);
if(!$db) {
echo mysql_error();
}
$q = "SELECT * FROM images";
$r = mysql_query ("$q",$conn);
if($r) {
while($row = mysql_fetch_array($r)) {
//echo $row ['username'];
//echo "<br>";
header ("Content-type: image/jpeg");
echo $row ['logo'];
//echo "<br>";
}
}else{
echo mysql_error();
}
?>
You can't have header after any output in your code: http://php.net/manual/en/function.header.php
Best practice is to upload images to a directory and just store the image's path/file name in the database. Also makes it easier to manipulate the image, e.g. create different sizes and thumbnails with PHP. And it takes about four times less the disk space...
Hope you are not storing your images on MySQL, please if you do, desist from that detrimental act because it is going to make your database unnecessarily heavy...
I would recommend not storing your images in a database. While it is technically possible, it has serious performance concerns. Best practice would be to designate a folder for the images, then access them directly. If you'd like the filtering and sorting ablities of MySQL, then replace the BLOB column that currently stores the image data with a VARCHAR containing the file name.
<?php
$conn = mysql_connect ("localhost","root","arctic123");
$db = mysql_select_db ("user_images", $conn);
$imgdir = "/path/to/image/directory/";
if(!$db) {
echo mysql_error();
}
$q = "SELECT * FROM images";
$r = mysql_query ("$q",$conn);
if($r) {
while($row = mysql_fetch_array($r)) {
echo $row['username'];
echo "<br>";
echo "<img src='" . $imgdir . $row['logo'] . "' />";
echo "<br>";
}
}else{
echo mysql_error();
}
?>

PHP will generate jpg images from BLOB but not from LONGBLOB in MySQL table

The following script has been outputting images from a BLOB column in MySQL for a good time now:
<?
$dbname="usr_web5_1";
$db = new mysqli($dbhost, $dbuser, $dbpwd, $dbname);
$sql = "SELECT foto, fototype, modified FROM `Komplettverzeichnis_extras` WHERE `WPID`= ?";
$stmt = $db->prepare($sql);
$stmt->bind_param("s",$_GET['id']);
if (!$stmt->execute()) $error=true;
$stmt->bind_result($file,$filetype,$modified);
$stmt->fetch();
if (!isset($filetype)) $filetype = "image/jpeg";
if (!$error) {
header("Content-type: $filetype;");
header("Last-Modified: $modified;");
} else {
echo "Error";
echo $db->error;
}
echo $file;
$stmt->close();
$db->close();
}
?>
I have tried both altering the column to LONGBLOB and replacing it by a new LONGBLOB colument; the images will not show. This is regardless wether I try to show the images already in place or I try and upload new ones.
The closest problem I found is here (german). For altering the column I used
ALTER TABLE `Komplettverzeichnis_extras` CHANGE `foto` `foto` LONGBLOB DEFAULT NULL
Obviously I am willing to provide more information if necessary.
I use phpMyAdmin in version 3.5.2.2, PHP 5.3.17, and mysql client API version (is that the mysql version?) 5.1.63.
Thanks in advance!
Okay I figured out the problem:
For a BLOB, the mySQLi Methods for strings will work that are used in my code as you can see above. This is not the case for Longblob. After the execute() and before the bind_result(), a store_result() is necessariy. Also, When saving, I cannot simply use bind_param(). Instead, the following construction using a NULL has to be used:
if (!$stmt = $db->prepare($sql)) printf($db->error);
$null = NULL;
$stmt->bind_param("bsi",$null,$type,$id);
$stmt->send_long_data(0,$data);
if (!$stmt->execute()) printf($db->error);
$stmt->close();
To cut it short, the following ressource helped me out:
Oswald#Work
Thank you all anyway for your efforts!

Categories