0

I have a CSV file which is more than 5 MB. As an example like this,

id,song,album,track
200,"Best of 10","Best of 10","No where"
230,"Best of 10","Best of 10","Love"
4340,"Best of 10","Best of 10","Al Road"

I have plenty of records. I need to get a one record at a time. Assume I need to get the detail of the id 8999. My main question is Is there any method to get exact data record from CSV just like we query in mysql table.

Other than that I have following solutions in my mind to perform the task. What could be the best way.

  1. Read the CSV and load in to array. Then search trough it and get data. (I have to get all records. The order is different that's why I face this issue. Other wise I could get record by record.)
  2. Export CSV to MYSQL database and then query from that table
  3. Read the CSV all the time without loading it to array

If I can search in CSV file in quick way it will be great. Appreciate your suggestions.

Thank you.

Prasad Rajapaksha
  • 6,118
  • 10
  • 36
  • 52
  • possible duplicate of [how to extract data from csv file in php](http://stackoverflow.com/questions/2805427/how-to-extract-data-from-csv-file-in-php) – Gordon Mar 20 '12 at 12:25
  • How to parse (large) CSV files has been asked and answered multiple times before. I take you know how to use an IF so I dont see any real question here. – Gordon Mar 20 '12 at 12:25
  • @Gordon. My Question is not how to extract data from CSV file. I already did that. But since I load 5 MB file always just get one record I am asking is there any way to go to exact record and get it without loading the file all the time. – Prasad Rajapaksha Mar 20 '12 at 12:27
  • possible duplicate of [Least Memory Intensive way to read a file in PHP](http://stackoverflow.com/questions/3171155/least-memory-intensive-way-to-read-a-file-in-php/3171193#3171193) – Gordon Mar 20 '12 at 12:30
  • No, there is no way to get data from a storage without loading the storage somehow. however, in case of files you can use streams so you dont need to load the entire file into memory, but like I said: has all been asked and answered before. please use the search function and follow the given duplicate links. – Gordon Mar 20 '12 at 12:37
  • OK Gordon thanks for your information. I will go trough those. I didn't find these links. May be because the way I search is wrong. Thanks. – Prasad Rajapaksha Mar 20 '12 at 12:39

2 Answers2

1

I'm not aware if there are any libraries that allow you to directly query a CSV file. If there are, that would be your best bet. If not, here's another solution.

If you want to get details of id 8999, it would be extremely memory inefficient to load in the entire file.

You could do something like this

Read in a line using fgets, explode on comma and check 0th element. If it is not the ID you want, discard and repeat.

Ayush
  • 41,754
  • 51
  • 164
  • 239
0

Not familiar with PHP, but if this query is frequent, consider keeping the data in the memory and index them by id using a mapping data structure. (should also be Array in PHP)

wks
  • 1,158
  • 1
  • 7
  • 12
  • A PHP script lives just for the request so you cannot keep the data around (unless you use Shared Memory Segments or Semaphores). – Gordon Mar 20 '12 at 12:48