简体   繁体   English

使用 PHP 替换平面文件数据库中的一行

[英]Using PHP to replace a line in a flat-file database

There are quite a few different threads about this similar topic, yet I have not been able to fully comprehend a solution to my problem.关于这个类似的主题有很多不同的主题,但我还没有完全理解我的问题的解决方案。

What I'd like to do is quite simple, I have a flat-file db, with data stored like this -我想做的很简单,我有一个平面文件数据库,数据存储如下 -

$username:$worldLocation:$resources

The issue is I would like to have a submit data html page that would update this line based upon a search of the term using php问题是我想要一个提交数据的 html 页面,该页面将根据使用 php 的术语搜索来更新此行

search db for - $worldLocation

if $worldLocation found

replace entire line with $username:$worldLocation:$updatedResources

I know there should be a fairly easy way to get this done but I am unable to figure it out at the moment, I will keep trying as this post is up but if you know a way that I could use I would greatly appreciate the help.我知道应该有一种相当简单的方法来完成这项工作,但目前我无法弄清楚,我会继续尝试,因为这篇文章已经发布,但如果您知道我可以使用的方法,我将非常感谢您的帮助.

Thank you谢谢

I always loved c, and functions that came into php from c.我一直喜欢 c,以及从 c 进入 php 的函数。

Check out fscanf and fprintf .查看fscanffprintf

These will make your life easier while reading writing in a format.这些将使您在以某种格式阅读写作时更轻松。 Like say:喜欢说:

$filehandle = fopen("file.txt", "c");
while($values = fscanf($filehandle, "%s\t%s\t%s\n")){
    list($a, $b, $c) = $values;
    // do something with a,b,c
}

Also, there is no performance workaround for avoiding reading the entire file into memory -> changing one line -> writing the entire file .此外,没有避免reading the entire file into memory -> changing one line -> writing the entire file性能解决方法。 You have to do it.你必须这样做。

This is as efficient as you can get.这是尽可能有效的。 Because you most probably using native c code since I read some where that php just wraps c's functions in these cases.因为您很可能使用本机 c 代码,因为我读了一些 php 在这些情况下只包装 c 函数的地方。

  1. You like the hard way so be it....你喜欢艰难的方式就这样吧......
  2. Make each line the same length.使每条线的长度相同。 Add space, tab, capital X etc to fill in the blanks添加空格、制表符、大写 X 等以填空
  3. When you want to replace the line, find it and as each line is of a fixed length you can replace it.当你想更换线时,找到它,因为每条线的长度是固定的,你可以更换它。
  4. For speed and less hassle use a database (even SQLLite)为了提高速度和减少麻烦,请使用数据库(甚至 SQLLite)

If you're committed to the flat file, the simplest thing is iterating through each line, writing a new file & changing the one that matches.如果您致力于平面文件,最简单的事情就是遍历每一行,编写一个新文件并更改匹配的文件。

Yeah, it sucks.是的,糟透了。

I'd strongly recommend switching over to a 'proper' database.我强烈建议切换到“适当的”数据库。 If you're concerned about resources or the complexity of running a server, you can look into SQLite or Berkeley DB.如果您担心资源或运行服务器的复杂性,您可以查看 SQLite 或 Berkeley DB。 Both of these use a database that is 'just a file', removing the issue of installing and maintaining a DB server, but still you the ability to quickly & easily search, replace and delete individual records.这两者都使用“只是一个文件”的数据库,消除了安装和维护数据库服务器的问题,但您仍然能够快速轻松地搜索、替换和删除单个记录。 If you still need the flat file for some other reason, you can easily write some import/export routines.如果由于其他原因仍然需要平面文件,则可以轻松编写一些导入/导出例程。

Another interesting possibility, if you want to be creative, would be to look at your filesystem as a database.另一个有趣的可能性是,如果您想发挥创意,将您的文件系统视为一个数据库。 Give each user a directory.给每个用户一个目录。 In each directory, have a file for locations.在每个目录中,都有一个位置文件。 In each file, update the resources.在每个文件中,更新资源。 This means that, to insert a row, you just write to a new file.这意味着,要插入一行,您只需写入一个新文件。 To update a file, you just rewrite a single file.要更新文件,您只需重写单个文件。 Deleting a user is just nuking a directory.删除用户只是破坏目录。 Sure, there's a bit more overhead in slurping the whole thing into memory.当然,将整个内容放入内存中会产生更多的开销。

Other ways of solving the problem might be to make your flat-file write-only, since appending to the end of a file is a trivial operation.解决问题的其他方法可能是使您的平面文件只写,因为附加到文件末尾是一项微不足道的操作。 You then create a second file that lists "dead" line numbers that should be ignored when reading the flat file.然后创建第二个文件,列出读取平面文件时应忽略的“死”行号。 Similarly, you could easily "X" out the existing lines (which, again, is far easier than trying to update lines in a file that might not be the same length) and append your new data to the end.类似地,您可以轻松地“X”出现有行(这再次比尝试更新文件中长度可能不同的行要容易得多)并将新数据附加到末尾。

Those second two ideas aren't really meant to be practical solutions as much as they are to show you that there's always more than one way to solve a problem.后两个想法并不是真正的实用解决方案,而是要向您展示解决问题的方法总是不止一种。

ok.... after a few hours work..this example woorked fine for me... I intended to code an editing tool...and use it for password update..and it did the trick!好的......经过几个小时的工作......这个例子对我来说很好......我打算编写一个编辑工具......并将其用于密码更新......它做到了! Not only does this page send and email to user (sorry...address harcoded to avoid posting aditional code) with new password...but it also edits entry for thew user and re-writes all file info in new file... when done, it obviously swaps filenames, storing old file as usuarios_old.txt.此页面不仅使用新密码向用户发送和发送电子邮件(抱歉...地址被硬编码以避免发布附加代码)...而且它还为 w 用户编辑条目并在新文件中重新写入所有文件信息...完成后,它显然会交换文件名,将旧文件存储为 usuarios_old.txt。

grab the code here (sorry stackoverflow got VERY picky about code posting)在这里获取代码(抱歉,stackoverflow 对代码发布非常挑剔)

https://www.iot-argentina.xyz/edit_flat_databse.txt https://www.iot-argentina.xyz/edit_flat_databse.txt

那是您所在的位置吗:

update `field` from `table` set `field to replace` = '$username:$worldlocation:$updatesResources' where `field` = '$worldLocation';

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

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