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.