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

我想MySQL的查询结果到Excel中,因此我写了下面的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"; 

但是我得到这个错误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"; 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"; 现在我面临着debugging这个代码的问题,任何帮助将是有帮助的,

看看描述和例子。

这个mysqli_fetch_field_direct返回一个对象

或者尝试types转换(string)mysqli_fetch_field_direct($ export,$ i)

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

我find了我的问题的解决scheme,而不是做这个$header .= mysqli_fetch_field_direct($export,$i) . "\t"; $header .= mysqli_fetch_field_direct($export,$i) . "\t"; 现在我正在使用$header .= mysqli_fetch_field_direct($export,$i)->name . "\t"; $header .= mysqli_fetch_field_direct($export,$i)->name . "\t";
现在它工作正常。