简体   繁体   English

使用PHP MySQL创建CSV时,在开头插入空白行

[英]A blank line is inserted at the beginning when Creating CSV using PHP MySQL

I am trying to get the contents of the MySQL table to a CSV file. 我正在尝试将MySQL表的内容转换为CSV文件。 Everything works fine except that a blank row is being inserted before the heading label that I pass through Array. 一切正常,除了在我通过Array的标题标签之前插入空白行。

 <?php
    error_reporting(0);
    ob_start();
    session_start();
    include 'auto_logout.php';
    //echo $_SESSION['fullname'];
    if ((!isset($_SESSION['ufullname'])) && (!isset($_SESSION['fullname']))) {
        /* Redirect browser */
        header("Location: index.php");
        /* Make sure that code below does not get executed when we redirect. */
        exit();
    }
    $output = "";
    require_once('config/config.php');
    require_once("includes/ftp_settings.php");
    $table = "Download CSV"; // Enter Your Table Name 
    if (is_array($new_exist) && (is_array($ftp1))) {
        $ressd_new = 'select filename from files where uploaded_by IN ("' . implode('","', $new_exist) . '")';
        $resd_new  = mysql_query($ressd_new);
        while ($kkk_new = mysql_fetch_array($resd_new)) {
            $gotit_new = $kkk_new[0];
        }

        $resultka_new = $ftp1;

        $sql = 'SELECT slno, filename, uploaded_by, dateadded, timeadded, size FROM files where uploaded_by IN ("' . implode('","', $new_exist) . '") and filename IN ("' . implode('","', $resultka_new) . '") and size != "0"';

    } else {

        $sql = "SELECT slno, filename, uploaded_by, dateadded, timeadded, size FROM files where ftp_file  = '$ftp_server' and ftp_u_file = '$ftp_user_name' and ftp_p_file = '$ftp_user_pass' and size != '0'";

    }
    $sql           = mysql_query($sql);
    $columns_total = mysql_num_fields($sql);

    // Get The Field Name
    $heading1 = array(
        "Sl. No.",
        "File Name",
        "Uploaded By",
        "Date Added",
        "Time Added",
        "Size"
    );
    $inc = 0;
    for ($i = 0; $i < $columns_total; $i++) {
        $heading = $heading1[$inc];
        $output .= '"' . $heading . '",';
        $inc = $inc + 1;
    }
    $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 = "myFile.csv";
    header('Content-type: application/csv');
    header('Content-Disposition: attachment; filename=' . $filename);

    echo $output;
    exit;

?>

Not really sure where is the blank line coming from. 不太确定空白行从何而来。

Including files will often add unwanted whitespaces if not cared properly. 如果处理不当,包含文件通常会添加不需要的空格。 Suppressing errors will make the things worse in every case. 在任何情况下,抑制错误都会使情况变得更糟。

The reason you need ob_start() is because the script between ob_start() and "echo $output;" 您需要ob_start()的原因是因为ob_start()与“ echo $ output;”之间的脚本 is printing some content (your empty line probably). 正在打印一些内容(可能是您的空行)。 And therfore you can't set headers. 因此,您无法设置标题。 I'll bet it's in your included files (auto_logout, config, ftp_settings). 我敢打赌,它在您包含的文件中(auto_logout,config,ftp_settings)。

Allow errors, remove ob_start, solve all warnings, profit. 允许错误,删除ob_start,解决所有警告,赢利。

And btw: Do not use mysql extension, use mysqli instead. 顺便说一句:请勿使用mysql扩展名,而应使用mysqli。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM