【发布时间】:2015-10-13 18:08:14
【问题描述】:
我有意使用文本文件来执行此操作。所以我读取了一个文本文件,我想检查该文本文件中是否已经存在用户名,如果他不存在,我想将此用户名添加到文本文件中,或者只是将点添加给他。
我当前的代码:
<?php
$myfile = fopen("test.txt", "r") or die("Unable to open file!");
$file = fread($myfile,filesize("test.txt"));
//echo $file;
fclose($myfile);
//$username = $_REQUEST['username'];
//$points = $_REQUEST['point'];
$username = 'chinmay'; //chinmay is a username this is unique
$points = 200; //if username chinmay not exitst then Insert first time otherwise if username chimay exist then next onwards this point will update everytime in the text file.
$myfileWrite = fopen("test.txt", "a") or die("Unable to open file!");
$txt = $username."|".$points."\n";
fwrite($myfileWrite, $txt);
fclose($myfileWrite);
?>
test.txt:
chinmay|800
john|200
sanjib|480
debasish|541
这是我的完整代码。我的要求是:
-
\n在我使用插入同一行的文本时不起作用。 - 如何检查重复的用户名?
- 如果我找到了用户名,那么如何更新用户积分?
我用谷歌搜索了过去 2 个小时,但没有得到任何解决方案。我不知道这个问题。
【问题讨论】:
-
我不想使用任何数据库。因为我的数据库负载很重。另一个应用程序正在运行我的数据库。查询在一分钟内执行了 1000 次以上。所以我想使用文本文件。
-
文本文件的效率会降低。
-
我知道这效率较低。但这是暂时的。我将开始一个从今天起持续 7 天的比赛。所以我不需要对我的数据库施加任何压力。我得到
Too many connection in my database的最长时间@ -
Flat File Databases 的可能重复项
-
您根本没有实施任何错误检查/处理。这是故意的吗?