PHP Scripts

Retrieve a JPG image from a MySQL database

The easiest way to deal with images is to store just the path in a database, and have the images online.

However, there are occassions when it's necessary to store the JPG as a blob in the database itself.
This simple script retrieves the data and displays as an image.

Let's assume you have a database, with a table called 'pix'
The table needs 4 fields or columns:
id: int(11), Primary, auto-increment
pics: blob
ext: varchar(4)
name: varchar(50)

The include "conn.php" is a standard connect to your database with name, password etc.
Copy the code and below and call it show.php

The useage is:
show.php?n=thumbname, eg, show.php?n=mypic1

Note: to include the retrieved images in a webpage, the useage is:
$tn3="<img src=show.php?tn=$tn";
echo "$tn3>";


<?php

$tn=$_GET["n"];
include "conn.php";

//select the picture using the id
$query = "select * from pix where name='$tn'";
//execute the query
$result = @MYSQL_QUERY($query);
//get the picture data which will be binary
$data = @MYSQL_RESULT($result,0,"pics");
//get the picture type. It will change according to file extension it may be either gif or jpg
$type = @MYSQL_RESULT($result,0,"extension");
//send the header of the picture we are going to send
Header( "Content-type: .jpg");
$tn2 = base64_decode($data.$type);

echo $tn2;

?>



Store a JPG in MySQL database

Back