2

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.

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.

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. 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. I then passed the data from the WebDNA page to the PHP page via form post. 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.

msargenttrue
  • 425
  • 1
  • 5
  • 11
  • 1
    Why are you using a non-standard format vs XML/JSON/etc? – afuzzyllama Jan 06 '12 at 19:39
  • 1
    Moreover, why are you using a flat file with presumably tens of thousands of records? – Jason McCreary Jan 06 '12 at 19:40
  • I am working on a website that is already near completion with several flat file databases and the database interaction coded in WebDNA. However to interface with Authorize.net I am using PHP for one page because there is an simple SDK for that language. I am looking for the quickest fix to get the current users data and pass it to the first page of the form of the payment gateway so the user does not have to re-input data. – msargenttrue Jan 06 '12 at 19:44
  • Have you looked at [SQLite](http://www.sqlite.org/)? – salathe Jan 06 '12 at 19:50
  • Does SQLite work with my particular format of CSV-style flat file databases? Or would I have to create entirely new databases that are compatible with SQLite? – msargenttrue Jan 06 '12 at 22:58

3 Answers3

4

I've done this before and was very concerned about speed. The data file had about 10,000 lines, but it is pretty fast. 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.

Depending on the size of the text file, you may consider a database, of course.

Eddie Paz
  • 2,219
  • 16
  • 11
3

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. 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

Following Eddie's lead, the function would look something like:

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":

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
)
Ben D
  • 14,321
  • 3
  • 45
  • 59
  • This code snippet looks promising however I haven't been able to successfully display the data after passing it to the function. Shouldn't something like this work? get_data_field_from_tsv(3002, "mydatabase-located-in-same-folder.db"); echo $data[0]; echo $data[1]; echo $data[2]; All i'm getting is a blank page. – msargenttrue Jan 06 '12 at 23:03
  • try print_r($data)... I'm guessing it's returning false? Can you give me a sample file? – Ben D Jan 06 '12 at 23:09
  • I just updated it to show a usage scenario which works on my end. Also, I corrected the function to add a semicolon to the end of "return $data"... if you had error reporting turned off it would explain the blank page. – Ben D Jan 06 '12 at 23:23
  • Thanks for the update. Weird… I'm still getting the blank screen even with the updated code. Permissions seem to be okay and I know that errors are turned on because I saw the issue with the semicolon. The path doesn't have to be the full path on the server right? Mine is just $data = get_data_field_from_tsv(14, 'testfile.db'); Also, my db has an extra carriage return on the very last line (required by WebDNA) don't know if that would effect anything. Here's the latest: http://webdna.msarg.com/downloads/php-flat-file-db-test2.zip – msargenttrue Jan 06 '12 at 23:55
  • OK, I think I see the issue here. PHP defaults to presume that line-termination will be a new line "\n", whereas you're terminating with a carriage return "\r". If you don't want to modify the file itself you can try having PHP detect the carriage return terminator by setting ini_set('auto_detect_line_endings', TRUE); ?> at the top of your script. Let me know if that works. – Ben D Jan 07 '12 at 16:49
0

This function may simplify your seeking, if the file is well-formed:

http://us3.php.net/fgetcsv

It's for CSVs, but if you have a standard delimiter you change the delimiter as a function param.

Fleep
  • 429
  • 4
  • 11