忽略导出数据的空列到php mysql中的csv文件

时间:2015-11-16 09:31:28

标签: php mysql wordpress csv

我正在处理一个脚本,该脚本从三个表(playlist, songs, rate)导出数据并将其放入一个csv文件中,使其完美运行。

如果一个或多个列为空或具有空值,它也会在csv文件中导出并显示如下的空列:

enter image description here

而且这个 enter image description here

而且这个 enter image description here

所以我希望如果列为空,那么这些列不会在csv文件中导出。我不知道怎么能这样做。

注意:我只希望列不为导出,而不是行

这是我在一个csv文件中导出三个表的代码。

$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;

还有一件事:

  

完成后,空列不导出,文件导入   成功吗?

2 个答案:

答案 0 :(得分:1)

好的,我感谢@vel。

我只是更改了我的查询,它运行正常,也可以成功导入。

这是我的查询:

$query = "SELECT plist.playlist_id, plist.playlist_name, plist.playlist_shortcode, psong.song_id, psong.list_order,
              psong.song_playlist, psong.mp3, psong.ogg, psong.title, psong.buy, psong.buyy, psong.buyyy, psong.price, psong.cover, 
              psong.artist
              FROM " . $pre . "hmp_songs As psong
              LEFT JOIN " . $pre . "hmp_playlists As plist
              On plist.playlist_name = psong.song_playlist
              Where plist.playlist_id IS NOT NULL
              And plist.playlist_name IS NOT NULL
              And plist.playlist_shortcode IS NOT NULL
              And psong.song_id IS NOT NULL
              And psong.list_order IS NOT NULL
              And psong.song_playlist IS NOT NULL
              And psong.mp3 IS NOT NULL
              And psong.ogg IS NOT NULL
              And psong.title IS NOT NULL
              And psong.buy IS NOT NULL
              And psong.buyy IS NOT NULL
              And psong.buyyy IS NOT NULL
              And psong.price IS NOT NULL
              And psong.cover IS NOT NULL
              And psong.artist IS NOT NULL";

答案 1 :(得分:0)

我认为array_filter可能会在这里使用。

$row = array_filter( mysqli_fetch_assoc( $result ) );

并在循环中

while( $row = array_filter( mysqli_fetch_assoc( $result ) ) ){
    /* do stuff */
}