本文介绍了MYSQL 存储多张图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网页,用户可以在其中上传带有图片的文章.用户可以随文章上传的图片数量没有限制.MySQL 数据库中的每一行代表许多文章中的一篇.存储所有这些图像的最佳方法是什么.我知道我会使用 BLOBS/LONGBLOBS,但如果我无法控制用户上传的图像数量,我不能只为不同的图像插入 50 列并希望它们上传的数量少于 50.这样做的最佳方法是什么.

I have a web page where users can upload articles with images on them. There is not limit on the amount of images a user can upload with their article. Each row in a MySQL database represents one article of many. What is the best way to store all these images. I know I would use BLOBS/LONGBLOBS but if I have no control over the amount of images a user uploads I can't just insert 50 columns for different images and hope they upload less than 50. What is the best way to do this.

推荐答案

最好的方法是创建一个包含引用文章的图像的表格,这样它就会为您留下这样的数据库:

The best way to do this is to create a table with images that references the article so it will leave you with a database like this:

Images 的表创建脚本如下:

CREATE TABLE `Images` (
    `ImageID` bigint NOT NULL AUTO_INCREMENT,
    `ArticleID` bigint NOT NULL,
    `Image` longblob NOT NULL,
    PRIMARY KEY (`ImageID`)
);
ALTER TABLE `Images` ADD CONSTRAINT `FK_Images` FOREIGN KEY (`ArticleID`) REFERENCES `Articles`(`ArticleID`);

现在您要做的就是将图像连同它所属的ArticleID一起插入到Images中.

Now all you have to do is insert an image into Images together with the ArticleID it belongs to.

这篇关于MYSQL 存储多张图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 05:23