Can i use CSV with PHP?

stndn

Golden Member
Mar 10, 2001
1,886
0
0
Helo...
i'm wondering if it's possible to use PHP to access data stored in CSV (comma separated value) functions?

I have a small database (quite small that i don't think i want to export it to mySQL) that i'm using for my CGI scripts.
I'm thinking of migrating some of my PHP scripts to use this same CSV as well.
To avoid having to make changes to the CGI scripts as well, i've decided to just try to have PHP access the CSV "database" somehow..

So, is there a way to have PHP connect to CSV, search for records, fetch data, and close the connection?
If so, what would be the command/function to open the connection and fetch the data?

Thanks.
 

igowerf

Diamond Member
Jun 27, 2000
7,697
1
76
A CSV file is just a text file with lots of data separated by commas. You treat it like a regular text file, reading it line by line and then tokenizing each line. The explode() function will probably come in very handy.

Take a look at PHP.net for documentation.

EDIT: amdfanboy seems to have a better method.
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
Simply splitting on commas is naive: there are many slight variations of the csv format, using different methods of quoting and escaping.
 

stndn

Golden Member
Mar 10, 2001
1,886
0
0
hmm... i wonder why it's not included in the php manual that i downloaded...
but anyways, fgetcsv() looks quite promising ...
thanks, amdfanboy -)

and yeah, i know that separating by commas are not a good idea for CSV files...
i was taught to use \0, but i don't know if it's a good idea as well...
(or maybe \0 is used for something else .. i forgot..)

thanks again -)
 

stndn

Golden Member
Mar 10, 2001
1,886
0
0
oopss.. i mean:
use \0 to separate fields in text files, not use \0 as separator in comma separated values files ... ,p