mysql - How can I export a csv file of the sql data with php? -


this question has answer here:

i have app displays 4,000 items. pulling mysql database. trying create button force download of of items database csv file. how can go this?

csv.php

<?php  header('content-type: text/csv; charset=utf-8'); header('content-disposition: attachment; filename=data.csv');  $output = fopen('php://output', 'w');  fputcsv($output, array('column 1', 'column 2', 'column 3'));  mysql_connect('localhost', 'root', 'root'); mysql_select_db('wp_xroads'); $rows = mysql_query('select first_name,last_name,phone bags');  while ($row = mysql_fetch_assoc($rows)) fputcsv($output, $row); ?> 

html

<div class="col-md-2">   <!-- button trigger csv file exporting -->     <form action="csv.php" method="get">         <a class="btn btn-success" type="submit" target="_blank">           export csv <span class="badge"><span class='glyphicon glyphicon-save-file'></span></span>         </a>     </form> </div> 

thanks in advance. -ken

the select outfile opposite of load data file. puts results of select statement specified file. no slow, zanny, non-performant php looping. export_options clause in same flavor of options in load data infile.

https://dev.mysql.com/doc/refman/5.1/en/select-into.html

select col1,col2,col3 outfile '/tmp/result.txt'   fields terminated ',' optionally enclosed '"'   lines terminated '\n'   test_table;   select first_name,last_name,phone outfile '/tmp/mybags.txt'   fields terminated ',' optionally enclosed '"'   lines terminated '\n'   bags; 

Comments