【发布时间】:2017-02-09 22:13:09
【问题描述】:
我正在做一个项目,该项目需要根据多个条件从 MYSQL DB 导出数据。我指的是this:
这是我的源代码:
public function exportExcelData($records)
{
$heading = false;
if (!empty($records))
foreach ($records as $row) {
if (!$heading) {
// display field/column names as a first row
echo implode("\t", array_keys($row)) . "\n";
$heading = true;
}
echo implode("\t", ($row)) . "\n";
}
}
public function fetchDataFromTable()
{
$query =$this->db->get('one_piece_characters'); // fetch Data from table
$allData = $query->result_array(); // this will return all data into array
$dataToExports = [];
foreach ($allData as $data) {
$arrangeData['Charater Name'] = $data['name'];
$arrangeData['Charater Profile'] = $data['profile'];
$arrangeData['Charater Desc'] = $data['description'];
$dataToExports[] = $arrangeData;
}
// set header
$filename = "dataToExport.xls";
header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=\"$filename\"");
$this->exportExcelData($dataToExports);
}
如果我在没有任何 where 子句的情况下使用它,它会给出整个表。
如果我只使用一个 where 子句,效果很好。
Bur,如果我使用多个 where 条件。它给了我一张空白的 Excel 表格。
有没有人知道如何让它在多个 where 条件下工作?
【问题讨论】:
-
您确定您的数据库中有任何适合您的
multiple conditions的数据吗? -
显示你的 where 条件?
-
尝试 echo
$this->db->last_query()与您的多个条件并在您的database或phpmyadmin中执行相同的查询并检查您是否得到任何结果 -
@Zeeshan 查询看起来像这样
select * from dept where deptid=5 and deptname="motor"并且它不返回任何数据。它给出了一个空白的 Excel 表 -
您是否尝试过直接在数据库中运行相同的查询,如果可以得到任何结果
标签: php mysql excel codeigniter export-to-excel