Storing Files in MySQL Database: Column Type and Insert Statement
When inserting a file into a MySQL database, the appropriate column type to use is a BLOB (Binary Large Object) column. BLOB columns store binary data, including files, and come in varying sizes:
Note: It's not advisable to store large files in MySQL databases due to performance impacts and increased database size.
Insert Statement:
The INSERT statement for inserting a file into a BLOB column is similar to other data types, but with a slight variation:
INSERT INTO table_name (column_name) VALUES (load_file('/path/to/file.ext'));
Here, /path/to/file.ext represents the absolute path to the file you want to insert.
Example:
To insert a file named document.pdf into a BLOB column named file_data in the document_table, use the following statement:
INSERT INTO document_table (file_data) VALUES (load_file('/home/user/documents/document.pdf'));
The above is the detailed content of How to Store Files in a MySQL Database Using BLOB Columns?. For more information, please follow other related articles on the PHP Chinese website!