将 PHP 设置数据存储为数组还是 MySQL 数据库?

发布于 2024-10-14 02:17:49 字数 352 浏览 2 评论 0原文

我正在开发一个 WordPress 主题,它需要一些设置。到目前为止,我将它们保存在数据库中,但我也希望它们是可移植的。因此,我还将数据作为数组保存到 settings.php 文件中。现在我正在考虑根本不使用数据库以避免存储两次。

与此相关的一些问题

  • 与 MySQL 数据库相比,将数据存储在包含的 PHP 文件内的数组中是否有什么不好的地方? (这只是设置,不需要排序,不需要关系)
  • 哪个最快?包含一个带有数组的 php 文件,或者从数据库加载数据?
  • 对此还有其他想法吗?

我对我的问题的最完整答案进行了检查投票。简短的回答可能会获得投票。

I'm working on a Wordpress theme and it needs to have some settings. Until now I have them in the database but I also want them to be portable. For that reason I also saved the data as an array into a settings.php-file. Now I'm considering to don't use the database at all to avoid storing things twice.

Some questions about this

  • Are there any bad thing about storing data in an array within a included PHP-file, compared to MySQL database? (It's just settings, nothing needs to be sorted, no relations needed)
  • Which is fastest? Include a php-file with an array, or load data from the database?
  • Other thougts about this?

I give a check-vote to the most complete answer to my questions. Short answers might get a vote up.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

情魔剑神 2024-10-21 02:17:49

如果您希望通过网站本身可配置设置,则将它们放在文件中的主要问题就会出现。如果没有,那么将它们包含在代码中就不必担心。

The main problem of having them in a file occurs if you want the settings to be configurable via the website itself. If not, then having them in the code is no real cause for concern.

白芷 2024-10-21 02:17:49

我是一个非常铁杆的数据库专家,我想说它们不需要在数据库中。

线索就在你的陈述中“这只是设置,不需要排序,不需要关系”和“我也希望它们是可移植的”

我的主要论点是简单性。 PHP 非常擅长处理数组,它喜欢它们,它理解它们,可以轻松地从文件加载它们并将它们保存到文件中。因此,即使您确实从应用程序中不时更改它们,更新和保存数组也没什么大不了的。因此,如果您使用数组,您就使用了 PHP 的本机功能,并且为此功能创建了架构简单性

因此,就可移植性而言,最可移植的数据库就是您不使用的数据库。当您可以简单地使用本机 PHP 数据格式时,您不需要数据库(至少不需要)

为了速度,无论如何,在 Linux 上 PHP 可以打开文件并读取它,这比它往返于文件的速度要快。任何东西的数据库。

反对数组解决方案的唯一剩下的论点是与其他数据的交互,但你已经说过没有。

因此,作为一个铁杆数据库人员,我想说不要仅仅因为数据库存在就使用它。数据库对于结构化数据来说是不可思议的,如果这只是一个简单的设置列表,那么它就不是结构化数据。数据库可以做到,文件系统也可以做到,选择更简单的。

I'm a pretty hardcore database guy and I would say they do not need to be in a database.

The clues are in your statements "It's just settings, nothing needs to be sorted, no relations needed" and "I also want them to be portable"

My main argument is simplicity. PHP is extremely good with arrays, it likes them, it understands them, can easily load them from files and save them to files. So even if you do change them from time to time from the app, updating and saving an array is no big deal. So, if you use the array, you use a native feature of PHP and that creates architectural simplicity for this feature.

So for portability, the most portable database is the one you do not use. When you have the simplicity of using a native PHP data format, you don't need the database (at least not for this)

For speed, on Linux anyway PHP can open a file and read it faster than it can make a roundtrip to the database for anything.

The only remaining argument against an array solution would be interaction with other data, but you have said there is none.

So, as a hardcore database guy, I would say do not use the database just because it is there. Databases are incredible for structured data, if this is just a flat list of settings it is not structured data. The db can do, the filesystem can do it, pick what is simpler.

千と千尋 2024-10-21 02:17:49

如果您希望 WordPress-Theme-Settings 具有可移植性,请不要为它创建基于文件的存储。有些网站的主题文件夹可能是只读的。

对于初始设置,可以从几乎所有基于文件的内容中读取您的设置。稍后最好将其存储(和备份)在数据库中。

如果您将内容存储在文件中,请使用 ini-style 基于文件,因为 PHP 为您免费提供 API。通常情况下,如果您的数据存储在数组或序列化中,效果会更好。选项不受限制。

不要太关心性能,只需使用 wordpress options api 作为最佳实践。

Don't create a file based storage for your Wordpress-Theme-Settings if you want it to be portable. Some sites might have the themes folder readonly.

For the initial setup it's ok to read your settings from almost everything filebased. Later on its best stored (and backuped) in the database.

If you store things in a file use the ini-style based files as PHP gives you an API for free. Things usually only tend to be better stored in an array or serialized if your options are not restricted.

Don't care about performance too much, simply use the wordpress options api as a best practice.

夜还是长夜 2024-10-21 02:17:49

如果您担心可移植性,您可能有兴趣使用 ODBC 或 PHP 数据对象

至于哪个最快,我不是专家,但设置文件只涉及读取文本文件并解析它。数据库选项通常会导致 TCP 连接(除非您使用 mysqlite,如果您要存储的不仅仅是文件路径和数据库名称,我建议您使用 mysqlite。

问候,

If you're worried about portability, you might be interested in using ODBC or PHP Data Objects

As for which is fastest, I'm no expert, but the settings file only involves reading a text file and parsing it. The database option usually will result in TCP connections (unless you use mysqlite, which I would recommend if you are going to store more than just file paths and database names.

Regards,

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