在 PHP 中存储一些数据的无数据库方法
我是 PHP 新手,但一般不会编程。我想存储从网络服务检索到的一些数据,但我不认为我需要为此使用数据库。首先,数据更新非常频繁,且大小始终小于1MB。 在 Apache 上使用 PHP 的最佳、快速且高效的方法是什么?注意:我使用的是托管提供商,因此我不喜欢自定义安装。 也许,单身? 谢谢
I am new to PHP but not programming in general. I want to store some data I retrieve from the web service but I do not think I want a database for that. First, data will be updated quite frequently and its size is always less than 1MB.
What is the best, fast but efficient approach in PHP on Apache? Note: I am using a hosting provider so I do not prefer custom installations.
Perhaps, singleton?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
使用数据库。否则,您将陷入序列化文件的困境。但要做到这一点,您需要实施并发控制。
使用数据库可以节省时间和精力。
Use a database. Otherwise you are stuck serialising a file. But to do this right you need to implement concurrency controls.
Save yourself the time and energy and use a database.
我认为问题是,数据会存储多长时间?如果您要存储数据直到被替换或比单个用户会话更长,我个人认为数据库是理想的解决方案 - 它被设计为非常高效,可以快速更改和检索数据。
如果数据仅保留单个用户会话,则可以使用 PHP 会话将数据存储为数组。
另一种选择是将数据存储在文件中。但是,这在检索少量数据时效率可能会低得多。
I think the question becomes, how long will the data be stored for? If you are storing the data until it is replaced or longer than a single user session, personally I believe a database is the ideal solution - it is designed to be very efficient with quickly altering and retrieving data.
If the data is only kept for a single user session, you could use PHP sessions to store the data as an array.
The other alternative is to store the data in a file. However, this can be much less efficient in retrieving small amounts of data.
尝试使用平面文件。如果您不需要进行任何花哨的查找,那就是。
Try using a flat file. If you don't need to do any sort of fancy lookups, that is.
数据库的明显替代方案是文件存储。 PHP 可以读写良好的旧磁盘文件;参见 fopen()、fread()、fwrite() 等等。您需要在服务器上为文件指定一个文件夹(除了 public_html 空间),并制定命名方案和数据格式。
The obvious alternative to the database is file storage. PHP can read and write good old disk files; see fopen(), fread(), fwrite() and then some. You'll need to designate a folder on the server (aside from the public_html space) for file(s) and come up with a naming scheme and data format.
您可以使用云服务(亚马逊、谷歌......)。但是,除了使您的应用程序更加复杂和脆弱以及使您自己更加时尚之外,我认为使用普通数据库或平面文件没有任何好处。
You could use a Cloud service (Amazon, Google ...). But apart from making your app more complecated and brittle and yourself more hip I don't see any benefit over using a normal db or a flat file.
您可以将数据存储在 xml 文件中,并使用 simplexml 加载数据并进行管理,例如:
$xml = simplexml_load_file("test.xml");
然后您就可以获得您定义的节点列表并执行您的操作。如需进一步参考,您可以查看以下内容:
SimpleXML 教程
You can store your data in xml files and use an simplexml to load the data an manage it, like:
$xml = simplexml_load_file("test.xml");
Then you can have a list of the nodes you defined and do your stuff. For further reference you can check out the following:
SimpleXML tutorial
您可以使用 serialize 将数组保存在纯文本文件中。
类似于
使用 fread 和模式“r”再次检索它,然后使用方法
unserialize
。然后管理数组中的数据。
You could save arrays in a plain text file, with serialize.
Something like
Retrieve it again with fread and mode "r" and then the method
unserialize
.And then manage your data in the array.