简体   繁体   English

PHP / MySQLi Excel导出没有列标题

[英]PHP / MySQLi Excel export has no column headers

I have some code I used to export to Excel using MySQL. 我有一些使用MySQL导出到Excel的代码。 I'm switching the syntax over to MySQLi, and it works except it's not printing the column headers in Excel. 我将语法切换到MySQLi,并且除了不在Excel中打印列标题外,它都有效。

I am new to MySQLi. 我是MySQLi的新手。

Here is the code (I'll try to leave out unnecessary code): 这是代码(我将尝试省略不必要的代码):

 <?php
 include("../include/database.php");
 global $ts;

 $ts = date('mdY-His');
 session_start();
 $where = $_SESSION['where'];

 $sql = "SELECT * FROM `main_table` WHERE " . $where . "";
 $result = mysqli_query($dbc, $sql) or die(mysqli_error());

 header("Content-Type: application/xls"); 
 header("Content-Disposition: attachment; filename=importdetails-".$ts.".xls");  
 header("Pragma: no-cache"); 
 header("Expires: 0");
 header("Content-Transfer-Encoding: binary ");

 // here is the formatting for Excel

 $sep = "\t"; //tabbed character

 // printing the column names

 for ($i = 0; $i < mysqli_num_fields($result); $i++) {
   echo mysqli_fetch_field($result,$i) . "\t";
 }

 print("\n");   

 // I believe the error is in the FOR loop above
 // Here is the remaining code

 while($row = mysqli_fetch_row($result))
 {
   $schema_insert = "";
   for($j = 0; $j < mysqli_num_fields($result); $j++)
   {
     if(!isset($row[$j]))
       $schema_insert .= "NULL".$sep;
     elseif ($row[$j] != "")
       $schema_insert .= "$row[$j]".$sep;
     else
       $schema_insert .= "".$sep;
   }
   $schema_insert = str_replace($sep."$", "", $schema_insert);
   $schema_insert = preg_replace("/\r\n|\n\r|\n|\r/", " ", $schema_insert);
   $schema_insert .= "\t";
   print(trim($schema_insert));
   print "\n";
 }
 ?>

Using the code above, I can get it to export an Excel document, but there are no column headers. 使用上面的代码,我可以导出Excel文档,但是没有列标题。 In the FOR loop, in the ECHO statement, I had mysql_field_name, so I switched it to mysqli_field_name, only realizing mysqli_field_name doesn't exist. 在FOR循环中,在ECHO语句中,我有mysql_field_name,因此我将其切换为mysqli_field_name,仅意识到mysqli_field_name不存在。 So I used mysqli_fetch_field. 所以我用了mysqli_fetch_field。

Why am I not getting the Excel column names? 为什么我没有得到Excel列名?

Edit 编辑

This is what worked for me: 这对我有用:

 <?php
 include("../include/database.php");
 global $ts; 

 $ts = date('mdY-His');
 session_start();
 $where = $_SESSION['where'];

 $sql = "SELECT * FROM `vsl_profile` WHERE " . $where . ""; 

 header("Content-Type: application/xls"); 
 header("Content-Disposition: attachment; filename=importdetails-".$ts.".xls");  
 header("Pragma: no-cache"); 
 header("Expires: 0");
 header("Content-Transfer-Encoding: binary ");

 $sep = "\t"; //tabbed character

 if($result = $dbc->query($sql))
 {
   while($finfo = $result->fetch_field())
   {
     printf($finfo->name . $sep);
   }
 }

 print("\n");    

 while($row = mysqli_fetch_row($result))
 {
   $schema_insert = "";
   for($j = 0; $j < mysqli_num_fields($result); $j++)
   {
     if(!isset($row[$j]))
       $schema_insert .= "NULL".$sep;
     elseif ($row[$j] != "")
       $schema_insert .= "$row[$j]".$sep;
     else
       $schema_insert .= "".$sep;
   }
   $schema_insert = str_replace($sep."$", "", $schema_insert);
   $schema_insert = preg_replace("/\r\n|\n\r|\n|\r/", " ", $schema_insert);
   $schema_insert .= "\t"; 
   print(trim($schema_insert));
   print "\n";
 }
 ?>

I believe mysql_fetch_field only takes one parameter. 我相信mysql_fetch_field只接受一个参数。

Try something like: 尝试类似:

while ($finfo = mysqli_fetch_field($result)) {
    echo $finfo->name;
}

Taken from this page doc page: http://php.net/manual/en/mysqli-result.fetch-field.php 从此页面文档页面获取: http : //php.net/manual/en/mysqli-result.fetch-field.php

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

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