PHP循环通过csv文件并返回错误的行

时间:2014-07-22 17:07:40

标签: php csv fgetcsv

只是尝试将url查询字符串用作csv表中的查找代码。这可以工作9/10次,但有时会返回错误的行(通常在正确的行下方几行)。

csv看起来像这样

taskcode1, info1, info1, info1

taskcode2, info2, info2, info2

taskcode3, info3, info3, info3

问题是有时(到目前为止大约1/10),taskcode1的给定url查询实际上会返回行info3。

此csv文件正由多个用户同时读取。问题可能源于同时阅读吗?我知道写作可能会有问题,可能需要对文件进行一系列的讨论。这是我的PHP脚本中的实际代码。谢谢你的任何建议。

请注意,一旦找到任务代码,$ this_taskcode == $ taskcode,我就会打破while循环。

//get query from request and look up task configuration
$csv_file_path = "tasks.csv";
$taskcode = $_SERVER['QUERY_STRING'];

//open csv file and find taskcode
$fid = fopen($csv_file_path, 'r');

//loop through each line of csv until taskcode is found, then save the whole line as $hit
while (($line = fgetcsv($fid)) !== FALSE){

    $this_taskcode = $line[0];

    if ($this_taskcode == $taskcode){
        $hit = $line;
        break;
    };
}

fclose($fid);

0 个答案:

没有答案