I want to export a table from my database, which contains a column where I put large text content, even many paragraphs. I've tested a script that does this work, but it has a special problem with this large text column. When I run the script and it exports the csv file, large text column just splits when there's a line break, and it takes all these line breaks as new fields inside the file, so csv file just breaks down.
I put my code here. Any ideas would be very appreciated.
<?php
// Database Connection
$host="localhost";
$uname="*****";
$pass="*****";
$database = "****";
$connection=mysql_connect($host,$uname,$pass);
echo mysql_error();
//or die("Database Connection Failed");
$selectdb=mysql_select_db($database) or die("Database could not be selected");
$result=mysql_select_db($database)
or die("database cannot be selected <br>");
// Fetch Record from Database
$output = "";
$table = "table"; // Enter Your Table Name
$sql = mysql_query("SELECT * FROM $table");
$columns_total = mysql_num_fields($sql);
// Get The Field Name
for ($i = 0; $i < $columns_total; $i++) {
$heading = mysql_field_name($sql, $i);
$output .= '"'.$heading.'",';
}
$output .="\n";
// Get Records from the table
while ($row = mysql_fetch_array($sql)) {
for ($i = 0; $i < $columns_total; $i++) {
$output .='"'.$row["$i"].'",';
}
$output .="\n";
}
// Download the file
$filename = "file.csv";
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename='.$filename);
echo $output;
exit;
?>