如何在mysql数据库中存储图像和文本

发布于 2024-10-19 01:56:57 字数 68 浏览 3 评论 0原文

我想将食谱存储在数据库中,食谱的格式应该是顶部的小照片和下面的文字。 谁能解释一下结构应该如何?

提前致谢

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

何以笙箫默 2024-10-26 01:56:57

我会避免将图像的实际字节存储在数据库中,这通常是一个坏主意(请参阅 在数据库中存储图像 - 是还是否?)。相反,我会创建一个简单的 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.

念三年u 2024-10-26 01:56:57

上传图像后,为其创建一个唯一的 ID 并将其存储在图像目录中。

将唯一图像 id 存储在带有 TEXT 字段的表中: -

id |图像ID |配方

id 字段应设置为 auto_increment。您可能需要一个标题字段。

填充的数据库看起来像: -

id | image_id           | recipe_title  | recipe_body
--------------------------------------------------------------------------------
1  | 218328132192.jpg   | 'Boiled egg'  | 'Put egg in pan and boil for 10 mins'

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: -

id | image_id           | recipe_title  | recipe_body
--------------------------------------------------------------------------------
1  | 218328132192.jpg   | 'Boiled egg'  | 'Put egg in pan and boil for 10 mins'

The image_id can be generated using a hash function or even auto_increment again.

身边 2024-10-26 01:56:57

您可以将图像存储在数据库中,但从数据库检索图像并将其呈现给用户通常并不容易(它的性能也不是那么好)。我建议将图像存储在某种 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.

落在眉间の轻吻 2024-10-26 01:56:57

您可以使用 BLOB 表示图像数据,使用 VARCHAR 表示文本。如果您愿意的话,可以使用 VARCHAR name 字段或 INT id 字段作为标识符/键。

You could use a BLOB for the image data and a VARCHAR for the text. Perhaps a VARCHAR name field or INT id field for the identifier/key if you feel so inclined.

深陷 2024-10-26 01:56:57

您可以轻松选择

| id | image | text |
---------------------
|    |       |      |

INT、AUTO-INCRMENT、PK 类型的 3 字段表 ID,图像可以是 BLOB,文本可以是 VARCHAR (255) 或 TEXT(如果更长)。我习惯将图像保存为路径而不是 blob(即 VARCHAR 字段),然后仅将此路径打印到存储相对图像的文件夹。它需要更少的资源和更少的工作;)

You could easily go for a 3-field table

| id | image | text |
---------------------
|    |       |      |

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 ;)

绾颜 2024-10-26 01:56:57

一般来说,在这种情况下,最好的想法(在图像和文本分离的意义上更易于管理)是将文本存储在表中的一个字段中,并将图像 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文