I am trying to insert values to a csv file through the following script….the script works fine but in the output, the values are not properly arranged in order..
<?php
require_once '../config.php';
$month = $_GET['month'];
$sq="select * from employee where month ='$month'";
$sql=mysql_query($sq);
$row = array();
$row[] = 'Employee Code';
$row[] = 'Employee Name';
$row[] = 'DOJ';
$data .= join(',', $row)."\n"; // Join all values without any trailing commas
$row = array(); // We must clear the previous values
while($rs = mysql_fetch_object($sql))
{
$row[] = $rs->employee_code;
$row[] = $rs->employee_name;
$row[] = $rs->emp_dateofjoin;
$row[] = "\n";
}
$data .= join(',', $row);
// Output the headers to download the file
header("Content-type: application/x-msdownload");
header("Content-Disposition: attachment; filename=test.csv");
header("Pragma: no-cache");
header("Expires: 0");
echo $data;
?>
The values are added in the csv file with one blank cell in the beginning of a row. except the first row.
Without even looking at it, what’s wrong with the core of your script is that you are using
mysql_*functions. So don’t. You should instead move to PDO (awesome), or mysqli (good).other then that, have you considered adding an
ORDER BYclause to your query?