Category: php
Storing Images in Mysql Database
Tutorial Outline:- Creating a Test Mysql Database
- Example php Database Function
- Explanation of Database Function
- Php Script to Load Images in Mysql Database
- Explanation of Image Script
- How to Get Images Out of MySql Database
- PHP Manual "base64_decode()"
- PHP Manual "chunk_split()"
- PHP Manual "convert_uuencode()"
- PHP Manual "RFC 2045 section 6.8"
This tutorial covers a simple way to store images in a mysql database. The example scripts that I provide here are intended to show you how to get a single image into your database. It will be up to you to write other scripts using php loops -- and functions like:
Creating a Test Mysql Database
This tutorial includes a fictitious database and table. Lets call the database test_imgs with a single table called pictures.
| id | pics | ext | gender |
| 1 | retergfghfghterhgcfnt hy6e5ghjwtjthj |
jpg | female |
- "id" is primary, key, not null, and auto_increment
- "pics" blob not null
- "ext" is varchar(4) not null
- "gender" is varchar(7) not null
- Mysql defaults for everything else.
- Create a directory "test" for the scritps
- Practice script "test.php"
Example Php Database Function
First let's write a function to connect to the Mysql database
Explanation of Database Function
I named my function "database connect" for obvious reasons, then passed the following arguments:
- $db_host -- Database Host
- $db_user -- Database User Name
- $db_pw -- Database Password
- $db_name -- Database Name
- &$db_selected -- Passed by reference
- &$connection -- Passed by reference
Arguments 1 through 4 need no explanation. Five and six were passed by reference which allows me to retrieve the variable values outside the function. Comment out the "die()" functions when you are not troubleshooting your code.
Php Script to Load Images in Mysql Database
Now lets get down to the task of storing images in a mysql database.
Explanation of Image Script
Notice that I used "fopen(), fread(), and fclose()" to get the contents of "testpic.php" and store it in a variable -- $img.
Next, I take $img and run it through "base64_encode().Read about base64 here.
Finally, after a little SQL magic, I place the encoded data from my test picture into the database. Viola! Now I have a jpeg image stored in mysql. Make note that I made a point to record the file extension along with the picture itself. I will be using that information when I am ready to get images out of my sql database.
how to upload images and files php Upload Images to Webserver
HTML Form PHP Function
# UPLOAD FILE
function upload_file(){
echo '
passing data from one form to another form php Passing data from one form to another form
To pass php data from a form and then catch that data into another form one way to accomplish this task is to intermi
how to turn variable value into variable name Variable Variables in PHP
Every so often when you are writing a php file, we all run into a situation where we would like for the name of the
string that is inside
how to make money online php Making Money on the Internet
Sign up for this series of free money making articles
Name:
register globals long arrays php Security, php.ini, register_globals and register_long_arrays
Author: D.Shaun Morgan
Versions and Skill Level
PHP Version - PHP 5x

Leave a Comment
store_images_mysql_php.php
Thanks
Re: Admin
A simple way to do this is to create a table for for products and include field in that table that stores teh URL or file location of the picture for that product. This will allow you to store the images in the filesystem instead of in the database. Just pass along the file location to the html for images -- img src --. You could also add a field for the image's thumbnail location or take care of it in your php script.
how I can store image in DB and recieve from DB and show it?
thanks.
Re: Admin
Images and Database
Can you tell me how to insert more then one images in database?and how to store them in database? need a row for each file?
Thanks.
Re: Admin
If you use the image methods found at mrarrowhead.com/index.php?page=store_images_mysql_php.php , you should be able to incorporate some type of loop such a for() or while() or do{} while(), etcetera.
when we select year and area and click submit we should get the image acording to those files how connect the images according to the fields when i select
show the
Re: Admin
Please explain a little more in depth about your problem. Are you having trouble getting the images into or out of the database, or are you haveing trouble writing a program that can return the exact images that you are requesting?