简体   繁体   English

PHP 保存/存储极少量数据的最佳实践

[英]PHP Best practice for saving/storing very small amounts of data

What I'm doing currently works but I just have the feeling that there's a better way, or perhaps a more professional way to do it.我目前正在做的事情有效,但我只是觉得有更好的方法,或者可能是更专业的方法。 I'm talking about very small amounts of data, like one number (or a few numbers) that is saved so as to store the selection made in a drop down list so its remembered every time the page is re-opened.我说的是非常少量的数据,例如保存的一个数字(或几个数字),以便将所做的选择存储在下拉列表中,以便每次重新打开页面时都会记住它。 I'm not using a mysql database at all so its all saved to file only.我根本没有使用 mysql 数据库,所以它只保存到文件中。

Right now I simply save the number to a text file to save the selected folder, and then have several other numbers underneath it to save the selected files.现在我只是将数字保存到一个文本文件中以保存选定的文件夹,然后在它下面有几个其他数字来保存选定的文件。 All selected from drop down lists like this:全部从下拉列表中选择,如下所示:

<form method="POST" action="index.php">
<select onchange="this.form.submit();" name="text">
<option>Select</option>
<option value="first">first</option>
<option value="second">second</option>
<option value="etc">etc</option>
</select>
</form>

Then they are stored in a text file that simply looks like this:然后它们被存储在一个简单的文本文件中,如下所示:

7
4
3

When the folder or file is altered the change is made with the following code:当文件夹或文件被更改时,使用以下代码进行更改:

function saveFile($line,$data,$savepath) {
$trace=file($savepath);$trace[$line]=$data;
file_put_contents($savepath,$trace);
}

if(isset($_POST['text'])) {
$changed=$_POST['text'];
saveFile($selected,$changed."\n",$savepath);
}

The code chooses which line of the text file to save to.代码选择要保存到文本文件的哪一行。 Maybe there is a much better way or something completely different?也许有更好的方法或完全不同的方法? Although I don't want to use sql or anything like that.虽然我不想使用 sql 或类似的东西。

At the risk of downvotes for ignoring the following point:由于忽略以下几点而面临被否决的风险:

Although I don't want to use sql or anything like that虽然我不想使用 sql 或类似的东西

You don't need to run a huge database system for this.您不需要为此运行庞大的数据库系统。 Something along the lines of SQLite lets you have the functionality of the database, without huge overheads. SQLite 的一些东西可以让你拥有数据库的功能,而没有巨大的开销。 The SQLite database is essentially a specific type of file with all of the relationships you configure, but gives a bit more flexibility than a flat file structure you're working with. SQLite 数据库本质上是一种具有您配置的所有关系的特定类型的文件,但比您正在使用的平面文件结构提供了更多的灵活性。

To me it looks you are attempting to persist few values from page load to page load.在我看来,您正试图从页面加载到页面加载保持几个值。 If this is only needed in the UI and not on the server-side I would save the data in a cookie ,but this is a rather dated solution to the problem ,also using local storage should do the trick for you.如果这只是在 UI 中而不是在服务器端需要,我会将数据保存在 cookie 中,但这是一个相当过时的问题解决方案,使用本地存储也应该为您解决问题。 Sorry for the JavaScript solution and not PHP.对不起,JavaScript 解决方案,而不是 PHP。 Sample code:示例代码:

localStorage.setItem("attr", "value"); localStorage.setItem("属性", "值");

alert( "attr: " + localStorage.getItem("attr")); alert("属性:" + localStorage.getItem("属性"));

Another idea is to save your data parsed as Json, serialized (associative) array or similar to a file.另一个想法是将解析为 Json、序列化(关联)数组或类似文件的数据保存。 That way you have named values and you know what value belongs to what function in your system if you open that file again in two years.这样您就已经命名了值,并且如果您在两年后再次打开该文件,您就知道哪个值属于您系统中的哪个函数。

But again, if that hugely increases the complexity of your project or it begins to stop being efficient (because of the great amount of stored values), an optimised database, like others already recommended, would be the only way.但同样,如果这极大地增加了项目的复杂性,或者它开始变得效率低下(因为存储了大量的值),一个优化的数据库,就像其他人已经推荐的那样,将是唯一的方法。

To save small amounts of data like this to file, I would recommend storing the data as key/value pairs in an array and then either serialize the array (or convert it to JSON) and then save that.要将这样的少量数据保存到文件中,我建议将数据作为键/值对存储在数组中,然后序列化数组(或将其转换为 JSON),然后保存。

Eg例如

$folderValue = 5;
$file1Value = 6;
$file2Value = 7;

/**
  * Saving data
  */
$data = array(
    'folder' => $folderValue,
    'file1' => $file1Value,
    'file2' => $file2Value,
);

file_put_contents($filename , serialize(data));


/**
  * Getting data (this will be the same array you
  * saved to file at an earlier point) which you can
  * iterate over or grab values via key.
  */

$data = unserialize(file_get_contents($filename));

foreach ($data as $key => $value) {
    echo "key {$key} is {$value}\n";
    // e.g. "key folderValue is 5";
}

echo $data['folder'] . "\n";
// e.g. "5";

SQL is still a better candidate for storing application state. SQL 仍然是存储应用程序状态的更好选择。 Flat files are poor because they become difficult to manage over time, but if you do use files, a key=>value mapping is important for readability.平面文件很差,因为随着时间的推移它们变得难以管理,但是如果您确实使用文件,键=> 值映射对于可读性很重要。

SQLite could be a good candidate if you need a low overhead SQL (low configuration, no background server running), but it has its own limitations (low performance, doesn't scale).如果你需要一个低开销的 SQL(低配置,没有后台服务器运行),SQLite 可能是一个很好的候选者,但它有它自己的局限性(低性能,不能扩展)。

PHP has good documentation around serializing data: http://php.net/manual/en/function.serialize.php http://php.net/manual/en/function.unserialize.php PHP 有关于序列化数据的很好的文档: http : //php.net/manual/en/function.serialize.php http://php.net/manual/en/function.unserialize.php

Json is another good format for data serialization. Json 是另一种数据序列化的好格式。 Also see json_encode and json_decode.另请参阅 json_encode 和 json_decode。

看看火箭商店...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM