Display Images from mySQL Binary Data

This tutorial will show you how to display your images (binary data) which has been saved in your mySQL database.
It’s very very simple.. so.. simple…. (read also: Storing Images/Binary Files to mySQL in PHP)

First of all we must connect to our database

PHP Code:
<?php
$username
= "";
$password = "";
$host = "localhost";
$database = "";

$username = “”; – It’s your database’s username
$password = “”;
– It’s your database’s password
$host = “localhost”;
– It’s your database’s host, usually it’s localhost but it can be something else also
$database = “”;
– It’s your database

Now let’s connect to the database

PHP Code:
@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());

@mysql_connect($host, $username, $password) or die(“Can not connect to database:”.mysql_error()); – This connects to your database

@mysql_select_db($database) or die(“Can not select the database: “.mysql_error());
– This will select your database

Now let’s get our id

PHP Code:
$id = $_GET['id'];

It will get a id from an URL. blabla.com

So.. your id will be 3 and it will show an image which id is 3

PHP Code:
if(!isset($id) || empty($id)){
die(
"Please select your image!");
}else{

if(!isset($id) || empty($id)){ – If this ID in your url is empty like ?id= of if it’s not even set
die(“Please select your image!”);
– lets display an error
}else{
– But if it is set let’s continue with showing our image

PHP Code:

$query = mysql_query("SELECT * FROM tbl_images WHERE id='".$id."'");
$row = mysql_fetch_array($query);
$content = $row['image'];

$query = mysql_query(“SELECT * FROM tbl_images WHERE id='”.$id.”‘”); – Now let’s select the blob from the table where id is $id (for example: 3)

$row = mysql_fetch_array($query); – Let’s gather our info about image which id is $id into one variable

$content = $row[‘image’]; – Get’s the blob from our table
Now let’s display our image

PHP Code:
header('Content-type: image/jpg');
echo
$content;
}

header(‘Content-type: image/jpg’); – This tells to the browser and to the server that this file will be a jpg file

echo $content; – This will display our blob..

} – Ends else
Okay.. now here’s our full script.

PHP Code:
<?php

$username

= "";
$password = "";
$host = "localhost";
$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());

$id = $_GET['id'];

if(!isset(

$id) || empty($id)){
die(
"Please select your image!");
}else{

$query = mysql_query("SELECT * FROM tbl_images WHERE id='".$id."'");
$row = mysql_fetch_array($query);
$content = $row['image'];

header('Content-type: image/jpg');
echo
$content;

}

?>

It works perfectly

Enjoy… 🙂     If you have questions then please feel free to ask

Andi Setiawan

Loving husband - Caring Father - Slap bet Commissionaire (I wish) find my complete profile on: http://andisetiawan.com/about-me http://facebook.com/andiim3 - http://gplus.to/andiim3 - http://andiim3.com

Share
Published by
Andi Setiawan

Recent Posts

Dedicated Blog for Dobby and Luna

Dobby and Luna have been part of our lives for the past years. They have…

2 years ago

Demo Site For My Projects

In the year 2021, I've completed some paid projects. Some of them are more technical,…

2 years ago

GlassTime – Glassmorphism WordPress Theme

Based on the GlassTime Bootstrap template, here I present glassmorphism WordPress Theme free to download.This…

3 years ago

GlassTime : Glassmorphism Responsive HTML5 Template

This HTML5 template is using Glassmorphism UI design language which is now trending for 2021…

3 years ago

Daftar Desa, Kecamatan, dan Kode Pos Buleleng – Bali

KecamatanKode POSBanjar81152Buleleng81119Busungbiu81154Gerokgak81155Kubutambahan81172Sawan81171Seririt81153Sukasada81161Tejakula81173Klik pada nama kecamatan untuk melihat daftar desa masing-masing. Sumber: kodepos.andiim3.com

3 years ago

Ninja diganti ADV

Ada yang bilang "semuanya akan mati matic pada akhirnya". Bener aja, sekarang tuntutan transportasi sehari-hari…

4 years ago

This website uses cookies.