Category:php

How to get images out of mysql database with php and use them on my webpages

Retrieving Images From Mysql Database With php

Author: D.Shaun Morgan

  • PHP Version - PHP 5x
  • Mysql Version - 5
Tutorial Outline:

Download Test Scripts

  1. Creating a Test Mysql Database
  2. Php Script to Load Images in Mysql Database
  3. Method 1 - Php Script to fetch images from mysql database
  4. Method 2 - Php Script to fetch images from mysql database
  5. Example of calling php script with img HTML tag
  6. Explanation of Image Script
  1. Create folder called "test"
  2. Create files "test01.php, test02.php, test03.php, test04.php & db_connection.php"
  3. Copy/Save test image into "test folder" a folder called "test"
See Also:
  1. How to store images in MySql database

This tutorial expands on another database/image how-to How to Store Images in Mysql Database. In the Images In tutorial I showed you the basic steps to opening a file (your image file) and reading it into a variable ($img), converting that file using php's base64_encode() function, and sticking that file into a Mysql database. Now, you will learn how to bring those images back out of a Mysql database, and render them to a web browser.

This Tutorial will cover two different ways to fetch and render images from a MySQL database

Create a mysql database with a table matching (Database Example)

CREATE TABLE `pictures` ( `id` int(8) NOT NULL AUTO_INCREMENT, `pic` blob NOT NULL, `ext` varchar(4) NOT NULL, `gender` varchar(7) NOT NULL, PRIMARY KEY (`id`), KEY `id` (`id`) )

Database Description

  1. Database name is "test_imgs"
  2. Table name is pictures
  3. "id" is primary, key, not null, and auto_increment
  4. "pic" blob not null
  5. "ext" is varchar(4) not null
  6. "gender" is varchar(7) not null
  7. I will assume English mysql defaults for everything else.
  8. Practice script "test.php"

Database Example

id pic ext gender
1 retergfghfghterhgcfnt
hy6e5ghjwtjthj
jpg female

Now - Create a directory on your web server and call it "test"

Create a file named "test01.php" in the"test" directory, and copy the Uploading Images Script to it.

The Uploading Images Script, originated in another php tutorial about Putting Images in a Mysql Database.

Uploading Images Script - test01.php

Copy test image intotest folder.

Test Image

testpic.jpg test image 1

Create a file named "test02.php", copy the contents of the PHP Image Rendering Script to it, then save it to the test directory.

PHP Image Rendering Script

Method 1# - rendering database images with php and MySQL.

This method determines what type of image is retrieved from the database, and uses the php header() function to setup and render the images.

PHP Image Render Script - Method 1 - test02.php

Method 2# - Image Rendering

Create a file named "test03.php" copy the Second Method Script to it. (See below)

Second Method Script - test03.php

Create a file named " db_connection.php" copy the Database Connection Parameters below

Database Connection Parameters

Create a file named "test04.php" copy the HTML for test_04.php (See below)

HTML for test_04.php

Explanation of Scripts

After building the database, and uploading all the example files and images to your server:

1. Navigate and execute test01.php script

testpic.jpg will be uploaded to your MySql server with an id of "1".

Picture Table

2. Click Method 1 to view your image. This particular script sends headers and render the jpg image via php script.

3. Next - Hit the Browser Back Button and click Method 2.

Notice that on the test04.php page, I inserted src="test_03.php" in the img tag. Doing this causes the test_03.php script to execute; the img tag sets up the headerfor theweb-browser.

Author: D.Shaun Morgan