How to store images directly in MySQL database _ MySQL

Source: Internet
Author: User
How to store images directly in the MySQL database if you want to store binary data, such as image files and HTML files, directly in your MySQL database, this article is for you! I will show you how to store these files through HTML forms and how to access and use these files.

Overview:

Create a new database in mysql

Example program for storing files

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:

The following is a reference clip:
Go to MySql controller
Enter the command "create database binary_data ;"
Enter the command "use binary_data ;"
Enter the 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); "(line 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:

The following is a reference clip:
 

// Store. php3-by Florian Dittmer
?>


Store binary data into SQL Database

// If a form is submitted, the code will be executed:

If ($ submit ){

// Connect to the database
// (You may need to adjust the host name, user name, and password)

MYSQL_CONNECT ("localhost", "root", "password ");
Mysql_select_db ("binary_data ");

$ Data = addslashes (fread (fopen ($ form_data, "r"), filesize ($ form_data )));

$ Result = MYSQL_QUERY ("insert into binary_data (description, bin_data, filename, filesize, filetype )".
"VALUES ('$ form_description', '$ data',' $ form_data_name ',' $ form_data_size ',' $ form_data_type ')");

$ Id = mysql_insert_id ();
Print"

This file has the following Database ID:$ Id";

MYSQL_CLOSE ();

} Else {

// Otherwise, the new data storage form is displayed.
?>

}

?>


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.