【问题标题】:Ignore empty columns for export data to csv file in php mysql忽略空列以将数据导出到 php mysql 中的 csv 文件
【发布时间】:2016-02-17 08:34:32
【问题描述】:

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

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

还有这个

还有这个

所以我希望如果列是空的,那么这些列不会导出到 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;

还有一件事:

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

【问题讨论】:

标签: php mysql wordpress csv


【解决方案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";

【讨论】:

  • 欢迎您@deemi。我也在寻找减少查询长度的替代方法。但没有运气。很乐意为您提供帮助。
  • 是的,我会的。为什么你在 wordpress 中使用 mysqli
  • 由于某些原因,我使用了自定义查询...我会在几天后更改它:)
  • 好的。您是否仅更改查询?你能发布完整的代码供我参考吗?
【解决方案2】:

我认为array_filter 可能在这里有用。

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

在循环中

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

【讨论】:

  • 没有兄弟它不工作......现在我的数据在 csv 文件中混合了:(......它不好
猜你喜欢
  • 2017-05-13
  • 2011-03-15
  • 2014-10-20
  • 2013-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多