How to store images directly in the MySQL database _ MySQL

Source: Internet
Author: User
Overview: 1. create a new database in MySQL. 2. an example program storing files. 3. an example program on how to access a file. Create a new database in MySQL. First, you must create a new database in your MySQL database. we will store those binary files in this database. In this example, I will use the following

Overview:

1. create a new database in MySQL.

2. an example program storing files.

3. an example program on how to access a file.

Create a new database in MySQL.

First, you must create a new database in your MySQL database. we will store those binary files in this database. In this example, I will use the following structure. to create a database, you must perform the following steps:

Go to the MySQL controller;

Enter the command "create database binary_data ;"

Enter the command "use binary_data ;"

Enter the following command:

     Create table binary_data (id INT (4) not null AUTO_INCREMENT primary key, description CHAR (50), bin_data LONGBLOB, filename CHAR (50), filesize CHAR (50 ), filetype CHAR (50); "(note: The row cannot be broken)

If there is no accident, the database and table should be created.

An example of how to store files. with this example, you can transmit files to the database through Html forms.

Store. php3 is referenced below:

     
     ?> Store binary data into SQL Database  
     This file has the following Database ID:$ Id"; MYSQL_CLOSE ();} else {// otherwise, the form for storing new data is displayed?>
       

If you execute this program, you will see a simple Html form, click browse to select a file, and then click submit.

After a file is uploaded to the web server, the program will tell you the ID of the file you just uploaded. remember this ID and it may be used later.

Example program on how to access a file

     
     // Call method: getdata. php3? Id =
     
      
If ($ id) {// you may need to adjust the host name, user name, and password: @ MYSQL_CONNECT ("localhost", "root", "password "); @ mysql_select_db ("binary_data"); $ query = "select bin_data, filetype from binary_data where id = $ id"; $ result = @ MYSQL_QUERY ($ query ); $ data = @ MYSQL_RESULT ($ result, 0, "bin_data"); $ type = @ MYSQL_RESULT ($ result, 0, "filetype"); Header ("Content-type: $ type "); echo $ data ;};?>
     

The program must know the file to be accessed, and you must take the ID as a parameter.

For example, the ID of a file in the database is 2.

You can call it like this: getdata. php3? Id = 2

If you store an image in a database, you can call it like calling an image.

For example, the ID of an image file in the database is 3. you can call it as follows:

How to store files larger than 1 MB:

If you want to store files larger than 1 MB, you must make many modifications to your programs, PHP settings, and SQL settings. the following may help you store files smaller than 24 MB:

Modify: store. php3, and change the value of MAX_FILE_SIZE to 24000000.

Modify: Your PHP settings. In general, PHP only allows files smaller than 2 MB. you must change the value of max_filesize (in php. ini) to 24000000. Remove the MYSQL data packet size limit. generally, MYSQL data packets smaller than 1 MB.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.