I've got quite a few fields in my tables that I've serialised to keep the number of fields down. For everything else that works perfect as it stores the data and when needed I can use the following as an example:
$dateofbirth = unserialize($row['dateofbirth']);
$dobday = $dateofbirth[0];
$dobmonth = $dateofbirth[1];
$dobyear = $dateofbirth[2];
Date of birth is stored as dd,mm,yyyy
and for everything else I can call it fine. My issue is now that I'm trying to use fputcsv
to create a CSV file using the following:
$result = mysqli_query($con, 'SELECT u.user_id, b.dateofbirth FROM Users u INNER JOIN Basic b USING (user_id) ORDER BY user_id DESC');
$fp = fopen('latest.csv', 'w');
fputcsv($fp, array('User ID', 'DOB' ));
The CSV generates, but for the date of birth column in the CSV it outputs as "*a:3:{i:0;s:2:"03";i:1;s:2:"02";i:2;s:4:"1986";}*"
because it's obviously still serialised. What is my best and or easiest way of handling these fields?
Many thanks in advance.
U can try this:
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=latest.csv");// Disable caching
header("Cache-Control: no-cache, no-store, must-revalidate"); // HTTP 1.1
header("Pragma: no-cache"); // HTTP 1.0
header("Expires: 0"); // Proxies
$fp = fopen('latest.csv', 'w');
while($row = mysqli_fetch_array($result)){
fputcsv($fp,array($row[0],$row[1]));
}
//or you can use with out headers above mentioned
or set some format for date of birth like:
$rowdob = date("M d, Y - g:i:s A", strtotime( $row[0] ) );
in while loop and pass $rowdob into fputcsv();