如何在mysql数据库中存储图像和文本
我想将食谱存储在数据库中,食谱的格式应该是顶部的小照片和下面的文字。 谁能解释一下结构应该如何?
提前致谢
I want to to store recipes in database, the format of the recipes should be a small photo on the top and a text below.
Could anyone explain me how the structure should be?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我会避免将图像的实际字节存储在数据库中,这通常是一个坏主意(请参阅 在数据库中存储图像 - 是还是否?)。相反,我会创建一个简单的
TEXT
字段,它可能存储 Markdown 食谱页面的版本。此页面可能包含指向您的图像的图像标签或仅包含食谱说明。I would avoid storing the actual bytes of the image in the database, that's generally a bad idea (See Storing Images in DB - Yea or Nay?). Instead, I would create a simple
TEXT
field that stores perhaps a Markdown version of your recipe page. This page could include an image tag pointing to your image or just the recipe instructions.上传图像后,为其创建一个唯一的 ID 并将其存储在图像目录中。
将唯一图像 id 存储在带有 TEXT 字段的表中: -
id |图像ID |配方
id 字段应设置为 auto_increment。您可能需要一个标题字段。
填充的数据库看起来像: -
image_id 可以使用哈希函数甚至再次 auto_increment 生成。
When the image is uploaded create a unique id for it and store it in an images directory.
Store the unique image id in a table with a TEXT field: -
id | image_id | recipe
the id field should be set to auto_increment. You'll probably want a title field.
A populated db would look like: -
The image_id can be generated using a hash function or even auto_increment again.
您可以将图像存储在数据库中,但从数据库检索图像并将其呈现给用户通常并不容易(它的性能也不是那么好)。我建议将图像存储在某种 NAS 上,并将该图像的位置以及配方的其他数据存储在数据库中。
You can store the image in the DB but it is not generally trivial to retrieve and present the image from the DB to the user (It doesn't perform that great either). I would suggest storing the image on some sort of NAS, and storing the location to that image in the database along with the other data for the recipe.
您可以使用
BLOB
表示图像数据,使用VARCHAR
表示文本。如果您愿意的话,可以使用VARCHAR name
字段或INT id
字段作为标识符/键。You could use a
BLOB
for the image data and aVARCHAR
for the text. Perhaps aVARCHAR name
field orINT id
field for the identifier/key if you feel so inclined.您可以轻松选择
INT、AUTO-INCRMENT、PK 类型的 3 字段表 ID,图像可以是 BLOB,文本可以是 VARCHAR (255) 或 TEXT(如果更长)。我习惯将图像保存为路径而不是 blob(即 VARCHAR 字段),然后仅将此路径打印到存储相对图像的文件夹。它需要更少的资源和更少的工作;)
You could easily go for a 3-field table
Id of type INT, AUTO-INCREMENT, PK, image could be BLOB and text VARCHAR (255) or TEXT if longer. I'm used to save image as a path instead of blob (a VARCHAR field, i.e.), and then print out only this path to the folder where I store the relative image. It takes less resources and less work ;)
一般来说,在这种情况下,最好的想法(在图像和文本分离的意义上更易于管理)是将文本存储在表中的一个字段中,并将图像 PATH 存储在另一个字段中,如 Damien Pirsy 所建议的那样。所以我只是添加我的声音。它不仅可以更好地控制数据,还可以更好地控制数据的呈现方式。并且以最高效的数据库方式。
将图像字节本身存储在数据库中效率不是很高,具体取决于图像有多大以及一次检索的数量。您必须考虑到这些字段的内容是可变的并且依赖于用户操作,因此从性能的角度来看并不是非常安全。 EG 如果用户插入 1MB 图像怎么办?
Generally the best idea (more managable in the sense that the image and text are separated) in this situation is to store the text in one field in the table, and the image PATH in another as Damien Pirsy suggests. So I am just adding my voice to that. It gives better control not only over the data, but how the data is presented. And in the most database-efficient manner.
Storing the image bytes itself in the database is not very efficient, depending on how big the image is and how many you are retrieving at one time. You have to consider that the contents of such fields is variable and dependant on user actions, hence not terribly safe from a performance point of view. E.G. What if a user inserts a 1MB image?