Jump to content

HELP!Export SQL data into CSV format


jigen7

Recommended Posts

can any one help me make a function that take in a query then output the results in CSV format ??

 

i have found here a sample but its not working for me

<?

$db = new MyDb;

$db->db_connect($host, $dbuser, $password);    # connect to database

$db->db_select($database);

 

$csv_output = '"column 1","column2"';

$csv_output .= "\015\012";

$result = mysql_query("select * from users");

 

  while($row = mysql_fetch_array($result)) {

    $csv_output .= '"'.$row[col1].'","'.$row[col2].'"';

    $csv_output .= "\015\012";

  }

 

  //You cannot have the breaks in the same feed as the content.

  header("Content-type: application/vnd.ms-excel");

  header("Content-disposition: csv; filename=document_" . date("Ymd") .

".csv");

  print $csv_output;

  exit;

?>

Link to comment
https://forums.phpfreaks.com/topic/71615-helpexport-sql-data-into-csv-format/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.