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.
A Binary Large Object ( BLOB ) is a MySQL data type that can store binary data such as images, multimedia, and PDF files.
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.
In MySQL, the preferred data type for image storage is BLOB.
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.
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';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With