6

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.

Daniel Vassallo
  • 337,827
  • 72
  • 505
  • 443
Gautam Kumar
  • 1,162
  • 3
  • 14
  • 29

4 Answers4

18

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';
Daniel Vassallo
  • 337,827
  • 72
  • 505
  • 443
9

Is there a particular reason why you can't store a reference to the image, rather than the actual image?

Images are big—they make databases big. SQL can be painful (as it's not object-oriented) and doesn't have an image type.

You could store them as a BLOB... if you want to write all the coding, encoding, checking, etc.

Gabriel Ryan Nahmias
  • 2,135
  • 2
  • 26
  • 39
Selenia
  • 286
  • 2
  • 3
  • 6
    Particular reason? uhmm, let us think. 1- It's not a web appllication and no web server is installed on the machine (to serve the images to the clients)? 2- Plus no file sharing preferred? 3- An application that has a feature to connect remote database & configuration? I can count lots more... – Roni Tovi Mar 16 '16 at 14:56
4

You can use a BLOB field to do this, but I would generally not recommend that. It's almost always better to simply store the image on the filesystem and store the path to the image in the database.

ETA:

See this question for more discussion

Community
  • 1
  • 1
Eric Petroelje
  • 59,820
  • 9
  • 127
  • 177
0

It is always a better idea to store images in external folders and store the refrence in the database.

Here is the step by step guide to upload images in the database. http://www.phpriot.com/articles/images-in-mysql/7

http://forum.codecall.net/topic/40286-tutorial-storing-images-in-mysql-with-php/

Here is the link for step by step guide for retreival of images from the database http://forum.codecall.net/topic/40849-tutorial-storing-images-in-mysql-with-php-part-ii-display-your-images/

Some more information on BLOB http://forums.mysql.com/read.php?20,17671,27914