1$lines =file('CSV Address.csv');
2
3foreach($lines as $data)
4{
5list($name[],$address[],$status[])
6= explode(',',$data);
7}
8
1Instead of writing out values consider using 'fputcsv()'.
2
3This may solve your problem immediately.
4
5function array2csv($data, $delimiter = ',', $enclosure = '"', $escape_char = "\\")
6{
7 $f = fopen('php://memory', 'r+');
8 foreach ($data as $item) {
9 fputcsv($f, $item, $delimiter, $enclosure, $escape_char);
10 }
11 rewind($f);
12 return stream_get_contents($f);
13}
14
15$list = array (
16 array('aaa', 'bbb', 'ccc', 'dddd'),
17 array('123', '456', '789'),
18 array('"aaa"', '"bbb"')
19);
20var_dump(array2csv($list));
21
22/*
23I hope it will help you.
24Namaste
25Stay Home Stay Safe
26*/
1// output headers so that the file is downloaded rather than displayed
2header('Content-Type: text/csv; charset=utf-8');
3header('Content-Disposition: attachment; filename=data.csv');
4
5// create a file pointer connected to the output stream
6$output = fopen('php://output', 'w');
7
8// output the column headings
9fputcsv($output, array('Column 1', 'Column 2', 'Column 3'));
10
11// fetch the data
12mysql_connect('localhost', 'username', 'password');
13mysql_select_db('database');
14$rows = mysql_query('SELECT field1,field2,field3 FROM table');
15
16// loop over the rows, outputting them
17while ($row = mysql_fetch_assoc($rows)) fputcsv($output, $row);
18