错误:使用php将mysqli查询结果导出到excel中

时间:2017-06-07 07:20:54

标签: php excel mysqli

我正在尝试将protocol HeaderDisplayable { func setTitle(_ string: String) } class MyView: UILabel, HeaderDisplayable { func setTitle(_ string: String) { self.text = string } } func foo<T: UILabel>(view:T) where T: HeaderDisplayable{ view.setTitle("HEY") } foo(MyView()) 个查询结果导出到excel中。因此我编写了以下MySQL脚本:

PHP

但是我收到了这个错误:

<?php
$conn = mysqli_connect('localhost','root','xyz','abc');
$select = "SELECT * FROM table_name";
$export = mysqli_query( $conn,$select) or die ( "Sql error : " . 
mysqli_error($conn) );
$fields = mysqli_num_rows ( $export );
for ( $i = 0; $i < $fields; $i++ )
{
//But this line giving error continuously 
$header .= mysqli_fetch_field_direct($export,$i) . "\t";
 }
while( $row = mysqli_fetch_row( $export ) )
{
 $line = '';
 foreach( $row as $value )
 {
  if ( ( !isset( $value ) ) || ( $value == "" ) )
  {
    $value = "\t";
  }
  else
  {
     $value = str_replace( '"' , '""' , $value );
     $value = '"' . $value . '"' . "\t";
  }
  $line .= $value;
 }
$data .= trim( $line ) . "\n";
 }
$data = str_replace( "\r" , "" , $data );
if ( $data == "" )
{
  $data = "\n(0) Records Found!\n";
}
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=random.xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data";

现在我在调试此代码时遇到问题。

2 个答案:

答案 0 :(得分:1)

查看说明和示例。

mysqli_fetch_field_direct 会返回一个对象

或尝试类型转换(字符串)mysqli_fetch_field_direct($ export,$ i)

http://php.net/manual/en/mysqli-result.fetch-field-direct.php

答案 1 :(得分:0)

我找到了问题的解决方案,而不是现在使用$header .= mysqli_fetch_field_direct($export,$i) . "\t"; $header .= mysqli_fetch_field_direct($export,$i)->name . "\t";。{
1}} 现在工作正常。