Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing images in MySQL [closed]

Tags:

mysql

Please give me the query for inserting images in a MySQL database. I am new to stackoverflow, so please ignore me if my question is not up to mark.

like image 749
Gautam Kumar Avatar asked Jun 10 '10 13:06

Gautam Kumar


People also ask

Is it possible to store images in MySQL?

A Binary Large Object ( BLOB ) is a MySQL data type that can store binary data such as images, multimedia, and PDF files.

Why images should not be stored in database?

Storing images in a database table is not recommended. There are too many disadvantages to this approach. Storing the image data in the table requires the database server to process and traffic huge amounts of data that could be better spent on processing it is best suited to.

What data type is suitable to store images in MySQL?

In MySQL, the preferred data type for image storage is BLOB.

Can we store the image in DB only?

Because images cannot be inserted into the database by using the SQL INSERT command, the only way to insert images into database is through embedded SQL programming.


1 Answers

If the image is located on your MySQL host, you could use the LOAD_FILE() function to store the image in a BLOB field:

CREATE TABLE MyTable (id INT, image BLOB);

INSERT INTO MyTable (id, image) VALUES(1, LOAD_FILE('/tmp/your_image.png'));

You have to make sure that the image file is readable by MySQL, and that the MySQL user has the FILE privilege. To grant the FILE privilege, log-in as root and execute:

GRANT FILE ON *.* TO 'mysql_user'@'localhost';
like image 172
Daniel Vassallo Avatar answered Nov 15 '22 08:11

Daniel Vassallo