错误:使用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";