使用PHP将MySQL数据导出到CSV文件显示1行和空白CSV

使用PHP将MySQL数据导出到CSV文件显示1行和空白CSV,php,mysql,csv,export,Php,Mysql,Csv,Export,我有一段将MySQL数据导出到CSV文件的代码,但是当我回显结果时,它只显示数据库中的一行,即使我在PHP My Admin中运行SQL,它也会显示大约29行 它只是生成一个空白的CSV文件 $sql="select description, jobreceived, timebookedfor, bookedfor, site_contact, site_address, invoice_contact, invoice_address, quotedprice, cl

我有一段将MySQL数据导出到CSV文件的代码,但是当我回显结果时,它只显示数据库中的一行,即使我在PHP My Admin中运行SQL,它也会显示大约29行

它只是生成一个空白的CSV文件

$sql="select description, jobreceived, timebookedfor, bookedfor,
      site_contact, site_address, invoice_contact, invoice_address,
      quotedprice, cleardowndetails, notes, mo_number from jobs ";
$rs=mysql_query($sql,$conn) or die(mysql_error());
$filename="jobs.csv";
$file=fopen($filename,"w");
//$output="sequence,firstname,surname,email,membertype\n";
fwrite($file,$output);
while($result=mysql_fetch_array($rs))
{
    echo $result["description"].','.$result["jobreceived"].'<br>';
    //$output=$result["sequence"].",".$result["name"].","
              .$result["email"].",".$result["country"]."\n";
    $output=proper($result["description"]).",".$result["jobreceived"]
           ."\r\n";
    fwrite($file,$output);
}
fclose($file);
function proper($string)
{
    $first=strtoupper(substr($string,0,1));
    $rest=strtolower(substr($string,1));
    $result=$first.$rest;
    return $result;
}
$sql=“选择描述、已接收作业、timebookedfor、bookedfor、,
现场联系人、现场地址、发票联系人、发票地址、,
报价、结算明细、备注、工单编号”;
$rs=mysql\u query($sql,$conn)或die(mysql\u error());
$filename=“jobs.csv”;
$file=fopen($filename,“w”);
//$output=“序列、名字、姓氏、电子邮件、成员类型\n”;
fwrite($file,$output);
而($result=mysql\u fetch\u数组($rs))
{
回显$result[“description”]。$result[“jobreceived”]。
; //$output=$result[“sequence”]。,“$result[“name”]。,” .$result[“email”]。,“$result[“country”]。\n”; $output=property($result[“description”])。“,”$result[“jobreceived”] .“\r\n”; fwrite($file,$output); } fclose($文件); 函数本身($string) { $first=strtoupper(substr($string,0,1)); $rest=strtolower(substr($string,1)); $result=$first.$rest; 返回$result; }
您可以使用这两个函数来完成

function csv_from_result($query, $delim = ",", $newline = "\n", $enclosure = '"')
{
    if ( ! is_object($query) OR ! method_exists($query, 'list_fields'))
    {
        show_error('You must submit a valid result object');
    }

    $out = '';

    // First generate the headings from the table column names
    foreach ($query->list_fields() as $name)
    {
        $out .= $enclosure.str_replace($enclosure, $enclosure.$enclosure, $name).$enclosure.$delim;
    }

    $out = rtrim($out);
    $out .= $newline;

    // Next blast through the result array and build out the rows
    foreach ($query->result_array() as $row)
    {
        foreach ($row as $item)
        {
            $out .= $enclosure.str_replace($enclosure, $enclosure.$enclosure, $item).$enclosure.$delim;
        }
        $out = rtrim($out);
        $out .= $newline;
    }

    return $out;
}

function write_file($path, $data, $mode = 'wb')
{
    if ( ! $fp = @fopen($path, $mode))
    {
        return FALSE;
    }

    flock($fp, LOCK_EX);
    fwrite($fp, $data);
    flock($fp, LOCK_UN);
    fclose($fp);

    return TRUE;
}
将查询对象传递给函数

$sql="select
          description,
          jobreceived,
          timebookedfor,
          bookedfor,
          site_contact,
          site_address,
          invoice_contact,
          invoice_address,
          quotedprice,
          cleardowndetails,
          notes,
          mo_number
        from jobs";
$rs =   mysql_query($sql,$conn) or die(mysql_error());
现在将资源从结果传递到
csv\u函数

$file_data  =   csv_from_result($rs);
现在您可以编写文件了

$filename   =   "jobs.csv"; 
write_file($filename , $file_data); // $filename can be a complete path
或回显以查看csv结果

echo $file_data;

也许您应该尝试删除fwrite($file,$output);循环中的语句。如果查询结果像预期的那样得到响应,那么问题显然是fwrite语句(可能是文件权限)。 如果结果没有像预期的那样得到响应,那么您的查询结果可能存在问题。
此外,作为一项建议,请确保按照fopen手册的要求为操作系统使用正确的行尾字符。

首先使用PHP内置的fputcsv()函数()错误日志会说什么?错误日志会说什么?我尝试使用fputcsv而不是fopen,但仍然没有,您是否具有对目录和jobs.csv文件的写入权限?(您说它是空的,所以我假设它已经存在)如果(!$file)echo“无法打开jobs.csv进行写入”,您应该通过
检查此项
Hi,当使用其他作者编写的代码时,您能引用您的源代码吗?为什么要编写自己的函数来创建CSV格式的输出,而PHP有非常优秀的内置函数来为您完成这项工作?是的,确保这两个函数在名为
Codeigniter
excel的伟大PHP框架中可用,excel将0001解释为1,所以fputcsv并不像你想象的那么优越。”0001'是一个stringwell。此函数有一些检查,并正确处理错误。人们不希望屏幕上出现任何不希望出现的错误。这只是一次工作。下一次,您将简单地使用它,因为函数是用于此目的的。