Reputation: 2097
is there a way to insert pics(not url,the pic)into a MYSQL table made with phpmyadmin? and if there is when i want to get that picture and insert it in the page , what should i do? :)
Upvotes: 7
Views: 65430
Reputation: 420951
When it comes to static resources such as background images or menu icons etc, I've never seen anyone store them in the database. For dynamic content such as user uploaded images, it's not that uncommon.
Here are some pro's and con's of storing images in the database.
Benefits:
Drawbacks:
Personally I've always found that the benefits outweighs the drawbacks and always had the images (along with mime-types) in the database.
Related questions and resources:
Upvotes: 2
Reputation: 60508
It is possible (using the BLOB type) but it is almost always better to just store the image in the filesystem and just store the path to the image in the database.
A few reasons why off the top of my head:
BLOBs will balloon the size of your database, making backups and restores potentially more difficult (although some might argue that it makes it easier since you only have to back up the database rather than the database plus a bunch of files).
Storing the data as BLOBs could cause performance issues if you have lazy code that uses select *
.
Serving the images from the filesystem will be faster than serving them from the database, and reduce load on the db server.
You get some flexibility with where you serve images - for example if you want to move them to a CDN in the future.
Upvotes: 11
Reputation: 85784
If you'll be fetching the image often, you'd probably be better off storing it as a file and saving the path to it, to save you a database run on fetching the image.
Otherwise, store the content of the image file in a BLOB column, and then print <img src="image.php?id=1234" />
in the HTML.
image.php
should look up the image with ID 1234 in the database, fetch the contents of that BLOB column, and print it after serving the correct HTTP header, e.g. header('Content-type: image/gif');
But seriously. Just save the image file. It's much less of a pain, I promise.
Upvotes: 0