Storing images in MySQL [closed]
开发者_高级运维
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this questionPlease 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.
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';
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.
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
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
精彩评论