Can I insert an image into a MYSQL DB table?

Is there a way to insert pics (not url, pic) into a MYSQL table created using phpmyadmin? and if there is, when I want to get this photo and paste it on the page, what should I do? :)

+6
mysql phpmyadmin
source share
3 answers

It is possible (using the BLOB type), but it is almost always better to just save the image in the file system and just save the image path to the database.

A few reasons why from the head:

  • BLOB files will increase the size of your database by making backups and restoring them potentially more complex (although some may argue that this makes work easier because you need to back up the database, not the database, as well as many files).

  • Saving data as a BLOB can lead to performance problems if you have lazy code that uses select * .

  • Serving images from the file system will be faster than servicing them from the database, and reduce the load on the db server.

  • You get some flexibility in where you serve your images — for example, if you want to move them to a CDN in the future.

+7
source share

If you often get the image, you probably would be better off storing it as a file and saving the path to it in order to keep the database running when the image is extracted.

Otherwise, save the contents of the image file in the BLOB column, and then type <img src="image.php?id=1234" /> in HTML.

image.php should search for the image with ID 1234 in the database, extract the contents of this BLOB column and print it after serving the correct HTTP header, for example. header('Content-type: image/gif');

But seriously. Just save the image file. I promise much less pain.

0
source share

When it comes to static resources such as background images or menu icons, etc., I have never seen anyone store them in a database. For dynamic content such as user-uploaded images, this is not uncommon.

Here are a few pros and save images in a database.

Benefits:

  • This is technically no different from other data. Only more and in binary format.
  • Images are saved with the rest of the database.
  • The absence of a headache when exiting synchronization (the presence of a path in the database indicating a nonexistent image, or unpublished images in the database).

Disadvantages:

  • Saving individual files is exactly what the file system is attached to, so it will most likely scale better.
  • Easier to manually debug / view images
  • The web server can directly serve images (there is no need for additional code to download content from the database and use the appropriate mime type, etc.).

Personally, I have always found that advantages outweigh disadvantages and always have images (along with mime types) in the database. I have never noticed any flaws or performance problems, but on the other hand, I have never worked on really large sites.

Related Questions and Resources:

0
source share

All Articles