dtx6087 2017-06-07 07:20
浏览 63

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

I am trying to export MySQL query results into excel. Therefore I wrote the following PHP script:

<?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 ) . "
";
 }
$data = str_replace( "" , "" , $data );
if ( $data == "" )
{
  $data = "
(0) Records Found!
";
}
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=random.xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$header
$data";

But I am getting this error:

PHP Recoverable fatal error:  
Object of class stdClass could not be converted to string in this line
$header .= mysqli_fetch_field_direct($export,$i) . "\t";

and now I'm facing problem in debugging this code.

  • 写回答

2条回答 默认 最新

  • doushiposong30622 2017-06-07 07:28
    关注

    Look to description and examples.

    This mysqli_fetch_field_direct returns an object

    Or try typecasting : (string)mysqli_fetch_field_direct($export,$i)

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

    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集