简体   繁体   English

使用PHP从平面文件数据库中读取

[英]Read from a flat file database with PHP

I am looking for the most simple way to pull out a line of data from a flat file database with PHP based on a variable ID number. 我正在寻找最简单的方法,使用基于可变ID号的PHP从平面文件数据库中提取一行数据。

For example, if the page is passed the ID of 10001 it grabs the first name, last name, company, address, and other demographic info of the user, etc. 例如,如果该页面的ID为10001,则它将获取用户的名字,姓氏,公司,地址和其他人口统计信息等。

The database is a text file that has field names on the first line and the subsequent lines below contain the data. 该数据库是一个文本文件,其第一行具有字段名称,而下面的后续行包含数据。 Its tab delimited and each line is separated by a carriage return. 它的制表符定界,每行由回车符分隔。

Any ideas? 有任何想法吗? Thanks in advance. 提前致谢。

Edit: The speed concern brought up by Eddie is definitely a valid one. 编辑:埃迪(Eddie)提出的速度担忧绝对是有效的。 So, to work around the need for PHP flat file database interaction I have instead used WebDNA to pull up the database data on the page prior to my PHP page. 因此,为了解决PHP平面文件数据库交互的需要,我改用WebDNA在PHP页面之前的页面上提取数据库数据。 I then passed the data from the WebDNA page to the PHP page via form post. 然后,我通过表单发布将数据从WebDNA页面传递到PHP页面。 So there is 1 extra step in the payment process for the customer but the data is pulled up in a standard and quick way. 因此,在客户的付款过程中,需要多做一个步骤,但是数据是以一种标准且快速的方式提取的。

I've done this before and was very concerned about speed. 我以前做过这个,并且非常关心速度。 The data file had about 10,000 lines, but it is pretty fast. 数据文件大约有10,000行,但是速度非常快。 You may have issues, however, accessing the file if there are simultaneous requests. 但是,如果同时有请求,则访问文件时可能会遇到问题。

Basically, open the file in read-only mode, read one line at a time, parse the line into an array, find the ID, and repeat until you find the ID you want. 基本上,以只读模式打开文件,一次读取一行,将该行解析为一个数组,找到ID,然后重复进行直到找到所需的ID。

Depending on the size of the text file, you may consider a database, of course. 当然,根据文本文件的大小,您可以考虑使用数据库。

The database is a text file that has field names on the first line and the subsequent lines below contain the data. 该数据库是一个文本文件,其第一行具有字段名称,而下面的后续行包含数据。 Its tab delimited and each line is separated by a carriage return. 它的制表符定界,每行由回车符分隔。

That sounds like a CSV type of file . 听起来像是CSV类型的文件 PHP has functions and classes to deal with those, you only need to specify the field delimiter and take a bit care about the line delimiter: fgetcsv Docs PHP具有处理这些函数和类的功能,您只需要指定字段定界符并稍微注意行定界符即可: fgetcsv Docs

Following Eddie's lead, the function would look something like: 在Eddie的带领下,该函数将类似于:

function get_data_field_from_tsv($id_to_look_for, $filename){
   $id_field_im_interested_in_position = '0'; //if it's the first field in the line
   $row = 1;
   if (($handle = fopen($filename, "r")) !== FALSE) {
      while (($data = fgetcsv($handle, 0, "\t")) !== FALSE) {
         $row++;
         if (trim($data[$id_field_im_interested_in_position])==$id_to_look_for){
            fclose($handle);
            return $data;
         }
      }
      fclose($handle);
      return false;
   }
}

So a sample use for the following file "testfile.db": 因此,以下文件“ testfile.db”的使用示例:

ID  SECOND  THIRD   FOURTH
12  test2   test3   test4
13  test22  test33  test44
14  test222 test333 test444

would be: 将会:

$data = get_data_field_from_tsv(14, '/path/to/testfile.db');
print_r($data);

Which results in: 结果如下:

Array
(
    [0] => 14
    [1] => test222
    [2] => test333
    [3] => test444
)

This function may simplify your seeking, if the file is well-formed: 如果文件格式正确,此功能可以简化您的查找:

http://us3.php.net/fgetcsv http://us3.php.net/fgetcsv

It's for CSVs, but if you have a standard delimiter you change the delimiter as a function param. 它适用于CSV,但是如果您有标准定界符,则可以将定界符更改为函数参数。

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

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