Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
错误:使用php将mysqli查询结果导出到excel时出错_Php_Excel_Mysqli - Fatal编程技术网

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

错误:使用php将mysqli查询结果导出到excel时出错,php,excel,mysqli,Php,Excel,Mysqli,我正在尝试将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_

我正在尝试将
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";

现在我在调试这段代码时遇到了问题。

请查看说明和示例

这个mysqli\u fetch\u field\u direct返回一个对象

或者尝试类型转换(字符串)mysqli\u fetch\u field\u direct($export,$i)


我找到了问题的解决方案,而不是执行此
$header.=mysqli\u fetch\u field\u direct($export,$I)。“\t”现在我使用的是
$header.=mysqli\u fetch\u field\u direct($export,$i)->name。“\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";