export mysql table data to csv file through php..html also get dumped -


So I was trying to export the mysql table to a csv file via php. This code is

  & lt ;? Php if (isset ($ _ POST ['exp'])) {header ('content-type: text / csv; charset = UTF-8'); Header ('content-dispute: attachment; file name = data csv'); $ Output = FOPAN ('php: // output', 'w'); Fputcsv ($ output, array ('user id', 'user name', 'password')); $ Con = mysqli_connect ('localhost', 'root', 'pass', 'xyz'); $ Line = mysqli_query ($ con, 'select' from 'user'); While ($ line = mysqli_fetch_assoc ($ rows)) {fputcsv ($ output, $ line); } Fclose ($ output); Mysqli_close ($ thief); }? & Gt; & Lt; Div & gt; & Lt; Form action = "#" method = "post" & gt; & Lt; Input type = "submit" value = "export" name = "exp" /> & Lt; / Form & gt; & Lt; / Div & gt;  

Everything is ok but the HTML part is also dumped in the CSV file. Here is the screen shot of CSV csv Output

Why is this happening? Am I doing something wrong?

Your page continues to load beyond PHP I Add and it will stop running fine.

  & lt ;? Php if (isset ($ _ POST ['exp'])) {header ('content-type: text / csv; charset = utf-8'); Header ('content-dispute: attachment; file name = data csv'); $ Output = FOPAN ('php: // output', 'w'); Fputcsv ($ output, array ('user id', 'user name', 'password')); $ Con = mysqli_connect ('localhost', 'root', 'pass', 'xyz'); $ Line = mysqli_query ($ con, 'select' from 'user'); While ($ line = mysqli_fetch_assoc ($ rows)) {fputcsv ($ output, $ line); } Fclose ($ output); Mysqli_close ($ thief); Go out(); }? & Gt; & Lt; Div & gt; & Lt; Form action = "#" method = "post" & gt; & Lt; Input type = "submit" value = "export" name = "exp" /> & Lt; / Form & gt; & Lt; / Div & gt;  

Comments