I am working on a script that export the data from three tables (playlist, songs, rate
) and put it into one csv file its working perfectly.
If the one or more columns are empty or have null value it also export in csv file and show empty column like this:
So I want that if the columns are empty, then these columns are not export in csv file. I have no idea how can I do this.
Note: I just want columns empty are not export, not rows
Here is my code of export three tables in one csv file.
$pre = $wpdb->prefix;
$link = mysqli_connect($mysql_host,$mysql_user,$mysql_pass,$mysql_db) or die('Could not connect: '.mysqli_error());
mysqli_select_db($link,$mysql_db) or die('Could not select database: '.$mysql_db);
$query = "SELECT plist.*, psong.*, prate.*
FROM " . $pre . "hmp_songs As psong
LEFT JOIN " . $pre . "hmp_playlists As plist
On plist.playlist_name = psong.song_playlist
LEFT JOIN " . $pre . "hmp_rating As prate
On psong.song_id = prate.rsong_id";
$result = mysqli_query($link,$query) or die("Error executing query: ".mysqli_error());
$row = mysqli_fetch_assoc($result);
$line = "";
$comma = "";
foreach($row as $name => $value){
$line .= $comma . '"' . str_replace('"', '""', $name) . '"';
$comma = ",";
}
$line .= "\n";
$out = $line;
mysqli_data_seek($result, 0);
while($row = mysqli_fetch_assoc($result)){
$line = "";
$comma = "";
foreach($row as $value)
{
$line .= $comma . '"' . str_replace('"', '""', $value) . '"';
$comma = ",";
}
$line .= "\n";
$out .= $line;
}
$csv_file_name = 'HMP_'.date('Ymd_His').'.csv'; # CSV FILE NAME WILL BE table_name_yyyymmdd_hhmmss.csv
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=".$csv_file_name);
header("Content-Description:File Transfer");
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Type: application/octet-stream');
echo __($out,"hmp");
exit;
One more thing:
After I done that, empty columns are not export, the file import successfully ?