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