I have one large csv file with 100000 customer details. I want to fetch data for my android application. I want to read specific row from csv file. like I want all details of customer whoes cust_id
is 070507
Reading this much big file in php using this method will take too much time.
if (($handle = fopen("test.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
}
fclose($handle)
}
I want a simple solution. and If this is simplest method then please tell me how to read specific row ?
You can get the file into an array using file(), then you can target the specific line and parse it to CSV
$lines = file('test.csv');
$row = $lines[70507]; // Assuming one header row
$csvdata = str_getcsv($row); // Parse line to CSV
$num = count($data); // Get number of columns
for ($c=0; $c < $num; $c++) { // Loop through columns
echo $data[$c] . "<br />
"; // Echo column
}
This is how you read specific row.
$ch = fopen($link_to_file);
$found = '';
/* If your csv file's first row contains Column Description you can use this to remove the first row in the while */
$header_row = fgetcsv($read_file);
/* This will loop through all the rows until it reaches the end */
while(($row = fgetcsv($ch)) !== FALSE) {
/* $row is an array of columns from that row starting at 0 */
$first_column = $row[0];
/* Here you can do your search */
/* If found $found = $row[1]; */
/* Now $found will contain the 2nd column value (if found) */
}