Мне нужно загрузить запись MySQL в CSV-файл в браузере, но, согласно моему коду, он не работает, как ожидалось. Вот мой код:
$scope.downloadAllCustomerData=function(){
var url2='../service/admin/customer/customer.php?action=downloadAllCustomerData';
var method='GET';
var data1='';
DataService.connectToServerSideScript(method,url2,data1)
.then(function(response) {
console.log('response',response);
},function(error) {
})
}
Вот мой код на стороне клиента, который отправляет запрос на загрузку файла .CSV
.
if ($_REQUEST["action"]=="downloadAllCustomerData") {
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=data.csv');
$output = fopen("php://output", "w");
fputcsv($output, array('ID', 'Name', 'Email', 'MObile No'));
$sql = "SELECT user_id,name,email,mobile from cb_customer_info ORDER BY user_id DESC";
$stmt = $db->prepare($sql);
if ($stmt->execute()) {
$row = $stmt->fetchAll();
$number_of_rows= count($row);
if ($number_of_rows > 0) {
fputcsv($output, $row);
$data=array("status"=>1,"msg"=>"Downloaded all data successfully.");
}else{
$data=array("status"=>0,"msg"=>"No record found.");
}
fclose($output);
}
echo json_encode($data);
}
Здесь я пытаюсь скачать mysqlзапись в файл .csv. Мне нужно, когда пользователь нажмет на кнопку, эти записи будут загружены в браузере.