1

I need to export csv file in php using mysql query .But when i export filename like this "yourfilename.csv_".

My code is

header("Content-type: application/csv");

header("Content-Disposition: \"inline; filename=yourfilename.csv\"");  

$query = "SELECT * FROM login";

$result = mysql_query($query) ;

echo "username ,password \r\n";

while($row = mysql_fetch_array($result))
{
    echo "$row[user],$row[pass]\r\n";  
}  

I need file name "yourfilename.csv".

1 Answer 1

2

You can have MySql return a query result to file directly with

SELECT * INTO OUTFILE 'result.csv'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
FROM my_table;

and then just do (also see Example #1 Download dialog)

header('Content-type: text/csv');
header('Content-Disposition: attachment; filename="yourfilename.csv"');
readfile('result.csv');
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.