Экспорт PHP / MySQLi Excel не имеет заголовков столбцов

У меня есть код, который я использовал для экспорта в Excel, используя MySQL. Я переключаю синтаксис на MySQLi, и он работает, за исключением того, что он не печатает заголовки столбцов в Excel.

Я новичок в MySQLi, поэтому, пожалуйста, прости меня.

Вот код (я постараюсь пропустить ненужный код):

 <?php
include("../include/database.php");
global $ts;

$ts = date('mdY-His');
session_start();
$where = $_SESSION['where'];

$sql = "SELECT * FROM `main_table` WHERE " . $where . "";
$result = mysqli_query($dbc, $sql) or die(mysqli_error());

header("Content-Type: application/xls");
header("Content-Disposition: attachment; filename=importdetails-".$ts.".xls");
header("Pragma: no-cache");
header("Expires: 0");
header("Content-Transfer-Encoding: binary ");

// here is the formatting for Excel

$sep = "\t"; //tabbed character

// printing the column names

for ($i = 0; $i < mysqli_num_fields($result); $i++) {
echo mysqli_fetch_field($result,$i) . "\t";
}

print("\n");

// I believe the error is in the FOR loop above
// Here is the remaining code

while($row = mysqli_fetch_row($result))
{
$schema_insert = "";
for($j = 0; $j < mysqli_num_fields($result); $j++)
{
if(!isset($row[$j]))
$schema_insert .= "NULL".$sep;
elseif ($row[$j] != "")
$schema_insert .= "$row[$j]".$sep;
else
$schema_insert .= "".$sep;
}
$schema_insert = str_replace($sep."$", "", $schema_insert);
$schema_insert = preg_replace("/\r\n|\n\r|\n|\r/", " ", $schema_insert);
$schema_insert .= "\t";
print(trim($schema_insert));
print "\n";
}
?>

Используя приведенный выше код, я могу заставить его экспортировать документ Excel, но заголовки столбцов отсутствуют. В цикле FOR в выражении ECHO у меня было mysql_field_name, поэтому я переключил его на mysqli_field_name, только понимая, что mysqli_field_name не существует. Поэтому я использовал mysqli_fetch_field.

Кто-нибудь видит, почему я не получаю имена столбцов Excel?

********** РЕДАКТИРОВАННЫЙ — ЭТО ТО, ЧТО РАБОТАЛ ДЛЯ МЕНЯ ***********

 <?php
include("../include/database.php");
global $ts;

$ts = date('mdY-His');
session_start();
$where = $_SESSION['where'];

$sql = "SELECT * FROM `vsl_profile` WHERE " . $where . "";

header("Content-Type: application/xls");
header("Content-Disposition: attachment; filename=importdetails-".$ts.".xls");
header("Pragma: no-cache");
header("Expires: 0");
header("Content-Transfer-Encoding: binary ");

$sep = "\t"; //tabbed character

if($result = $dbc->query($sql))
{
while($finfo = $result->fetch_field())
{
printf($finfo->name . $sep);
}
}

print("\n");

while($row = mysqli_fetch_row($result))
{
$schema_insert = "";
for($j = 0; $j < mysqli_num_fields($result); $j++)
{
if(!isset($row[$j]))
$schema_insert .= "NULL".$sep;
elseif ($row[$j] != "")
$schema_insert .= "$row[$j]".$sep;
else
$schema_insert .= "".$sep;
}
$schema_insert = str_replace($sep."$", "", $schema_insert);
$schema_insert = preg_replace("/\r\n|\n\r|\n|\r/", " ", $schema_insert);
$schema_insert .= "\t";
print(trim($schema_insert));
print "\n";
}
?>

0

Решение

Я считаю, что mysql_fetch_field принимает только один параметр.

Попробуйте что-то вроде:

while ($finfo = mysqli_fetch_field($result)) {
echo $finfo->name;
}

Взятые с этой страницы документа страницы: http://php.net/manual/en/mysqli-result.fetch-field.php

1

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector