Using this php code you can export the mysql data into excel sheet before that while starting we need to create a blank file in csv format, because then only you can perfectly export the data into to sheet later we would save the file as excel format.
<?php
$conn = mysql_connect(“localhost”,”root”,”");
mysql_select_db(“dbname”,$conn);
$filename = “master_data_” . date(‘d-m-Y’) . “.csv”;
$fp = fopen($filename, “w”);
$res = mysql_query(“SELECT * FROM table WHERE condition”);
// fetch a row and write the column names out to the file
$row = mysql_fetch_assoc($res);
$line = “”;
$comma = “”;
foreach($row as $name => $value) {
$line .= $comma . ‘”‘ . str_replace(‘”‘, ‘”"‘, $name) . ‘”‘;
$comma = “,”;
}
$line .= “\n”;
fputs($fp, $line);
// remove the result pointer back to the start
mysql_data_seek($res, 0);
// and loop through the actual data
while($row = mysql_fetch_assoc($res)) {
$line = “”;
$comma = “”;
foreach($row as $value) {
$line .= $comma . ‘”‘ . str_replace(‘”‘, ‘”"‘, $value) . ‘”‘;
$comma = “,”;
}
$line .= “\n”;
fputs($fp, $line);
}
fclose($fp);
?>



Posted in
Tags: