Retrieving Image File from MySQL using PHP

Many people ask me about this.

Database example:
database name = “test”
table name = “image”
table column 1 = id, data type = int, special attributes = auto_increment, primary key
table column 2 = image, data type = blob

this is the sql script:

use test;
create table image (id int(11) NOT NULL auto_increment, image blob, PRIMARY KEY(id));

this is the php script:

<?
header(”Content-type: image/jpeg”); // act as a jpeg file
mysql_connect(”your_host”,”your_db_username”,”your_db_password”);
mysql_select_db(”test”); // use the ‘test’ database

$query = “select image from image where id = $id”;
$result = mysql_db_query(”test”,$query);

$row = mysql_fetch_array($result);
$img = $row['image'];

echo (”$img”);
?>

There you go, you will see the image that you store in MySQL database using blob data type.
Enjoy! :D


About this entry