I'm trying to write to a csv file using fputcsv and it appears to be failing when the string is too long.
Does fputcsv have a limit when writing?
I'm trying to write to a csv file using fputcsv and it appears to be failing when the string is too long.
Does fputcsv have a limit when writing?
trim()
it before saving!
I tried some things to solve it (like stripslashes() and so on).
When i manually added fputcsv($fp, 'too loooooooooooo...ong string') it worked.
Then i've looked into the db fields that were too long for this function and found out some white spaces ahead in values.
So here is the code:
function get_items($db_handler) {
$array = array();
$query = "SELECT * FROM `db` WHERE <some condition>";
$result = mysqli_query($db_handler, $query);
$i = 0;
while ($row = mysqli_fetch_assoc($result)) {
foreach ($row as $key => $value) $array[$i][$key] = trim($value);
$i++;
}
mysqli_free_result($result);
return $array;
}
// now we put records into csv from the assoc array returned by get_items():
$items = get_items($db_handler);
$fp = fopen($file, 'a+');
foreach ($items as $fields) //
{
fputcsv($fp, $fields);
}
fclose($fp);
Do not forget to trim($value) inside of foreach in the function!