tags:

views:

41

answers:

3

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.

+1  A: 

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

Images big. Make database big. SQL painful not OO at all doesn't have image type.

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

Selenia
+1  A: 

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

Eric Petroelje
Can you give an example?
gautam kumar
+1  A: 

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
ITS NOT WORKING .CAN YOU PLEASE HELP ME OUT
gautam kumar
@gautam: How is it not working? What error are you receiving?
Daniel Vassallo
i have given path in load_file as(c:\dir\a.jpg) but after performing `select` operation I see NULL(not my img) being stored, why?
gautam kumar
@gautam: It looks like a permissions problem. You have to make sure that the file you are reading can be read by the MySQL service.
Daniel Vassallo
@gautam: In addition, I think in Windows you need to specify your path like this: `'c:\\dir\\a.jpg'`
Daniel Vassallo