对于查询:
include_once 'dbconfig.php';
$query = "SELECT * FROM users";
$crud->save($query);
file.csv正确导出,但我还想包括从中取出的值的MySQL表列的名称。
提前感谢!我在将桌子的头张贴到CSV时遇到问题,所以我采取了另一种方法。希望它可以帮助任何正在尝试做我正在做的事情的人:
public function export($table)
{
$stmt2 = $this->db->prepare("DESCRIBE ".$table);
$stmt2->execute();
if ($stmt2->rowCount()>0) {
while ($row = $stmt2->fetch(PDO::FETCH_ASSOC)) {
$csv_output .= $row['Field'].", ";
$i++;
}
}
$csv_output .= "\n";
$stmt3 = $this->db->prepare("SELECT * FROM ".$table);
$stmt3->execute();
while ($rowr = $stmt3->fetch(PDO::FETCH_BOTH)) {
for ($j=0;$j<$i;$j++) {
$csv_output .= $rowr[$j].", ";
}
$csv_output .= "\n";
}
$filename = "raport_proiecte_".date("Y-m-d_H-i",time()).".csv";
header("Content-Type: application/xls");
header("Content-Disposition: attachment; filename=$filename");
header("Pragma: no-cache");
header("Expires: 0");
print $csv_output;
exit;
}