0

I am little new in PHP and trying to get data from mysql database and want export it as excel. I am following this answer for same.

Its little old and using MYSQL connection instead I want it as mysqli, I have changed code like below

if (isset($_POST["submit"]))
{

$select = "SELECT * FROM `records`";

$export = mysqli_query($mysqli, $select); 
//$fields = mysql_num_rows($export); // thanks to Eric
$fields = mysqli_num_fields($export); // by KAOSFORGE
$col_title="";
$data="";
for ($i = 0; $i < $fields; $i++) {
    $col_title .= '<Cell ss:StyleID="2"><Data ss:Type="String">'.mysqli_fetch_field_direct($export, $i).'</Data></Cell>';
}

$col_title = '<Row>'.$col_title.'</Row>';

while($row = mysqli_fetch_row($export)) {
    $line = '';
    foreach($row as $value) {
        if ((!isset($value)) OR ($value == "")) {
            $value = '<Cell ss:StyleID="1"><Data ss:Type="String"></Data></Cell>\t';
        } else {
            $value = str_replace('"', '', $value);
            $value = '<Cell ss:StyleID="1"><Data ss:Type="String">' . $value . '</Data></Cell>\t';
        }
        $line .= $value;
    }
    $data .= trim("<Row>".$line."</Row>")."\n";
}

$data = str_replace("\r","",$data);

header("Content-Type: application/vnd.ms-excel;");
header("Content-Disposition: attachment; filename=export.xls");
header("Pragma: no-cache");
header("Expires: 0");

$xls_header = '<?xml version="1.0" encoding="utf-8"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40">
<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
<Author></Author>
<LastAuthor></LastAuthor>
<Company></Company>
</DocumentProperties>
<Styles>
<Style ss:ID="1">
<Alignment ss:Horizontal="Left"/>
</Style>
<Style ss:ID="2">
<Alignment ss:Horizontal="Left"/>
<Font ss:Bold="1"/>
</Style>

</Styles>
<Worksheet ss:Name="Export">
<Table>';

$xls_footer = '</Table>
<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
<Selected/>
<FreezePanes/>
<FrozenNoSplit/>
<SplitHorizontal>1</SplitHorizontal>
<TopRowBottomPane>1</TopRowBottomPane>
</WorksheetOptions>
</Worksheet>
</Workbook>';

print $xls_header.$col_title.$data.$xls_footer;
exit;
}

but its giving me error in line 16 which is like below

$col_title .= '<Cell ss:StyleID="2"><Data ss:Type="String">'.mysqli_fetch_field_direct($export, $i).'</Data></Cell>';

Let me know if someone can help me for solve the issue. Thanks

Raju Bhatt
  • 385
  • 5
  • 17

1 Answers1

1

You need to access the name property instead of trying to echo the object.

mysqli_fetch_field_direct($export, $i)->name

This will deliver the column name as desired.

Read the manual: https://php.net/manual/en/mysqli-result.fetch-field-direct.php

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • Beyond that, I would virtually rewrite your script because there are plenty of things to refine. – mickmackusa Apr 27 '19 at 12:23
  • To name a few things, @Raju , the `!isset($value)` check is useless and should be removed. In fact the entire `if` branch can be removed because if `$value` is empty, the else branch provides the identical outcome. `trim()` is also useless because you aren't hardcoding any spaces at the start or end of the string. – mickmackusa Apr 27 '19 at 12:32