Php 如何将搜索结果导出为csv?
我目前正试图弄清楚如何导出搜索完成的表结果Php 如何将搜索结果导出为csv?,php,html,mysql,ajax,Php,Html,Mysql,Ajax,我目前正试图弄清楚如何导出搜索完成的表结果 <!DOCTYPE html> <html> <head> <script type="text/javascript" src="http://gc.kis.v2.scr.kaspersky-labs.com/FD126C42-EBFA-4E12-B309-BB3FDD723AC1/main.js?attr=XeIP$ <script src=&q
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="http://gc.kis.v2.scr.kaspersky-labs.com/FD126C42-EBFA-4E12-B309-BB3FDD723AC1/main.js?attr=XeIP$
<script src="js/jquery-ui-1.8.16.custom.min.js" type="text/javascript"></script>
<title>AJAX Persons Example - Displaying FILES</title>
<script>
$(document).ready(function() {
$( "#AthleteName" ).change(function() {
$.ajax({
url: 'searchPlayer.php',
data: {searchName: $( "#AthleteName" ).val()},
success: function(data){
$('#Nameresult').html(data);
}
});
});
});
</script>
</head>
<body>
<h3>Olypmic Dataset</h3>
<input class="xlarge" id="AthleteName" type="search" size="30" placeholder="sid contains"/>
<div id="Nameresult">Search Result</div>
<br/><br/>
</body>
</html>
"; $
$stmt->close();
}
$db->close();
?>
但是,当搜索结果出来,我尝试导出时,它只会创建一个空的csv文件。我不确定是什么导致了这个问题。在searchPlayer.php上执行的机制是否不正确
<?php
$filename = 'users.csv';
$export_data = unserialize($_POST['export_data']);
// file creation
$file = fopen($filename,"w");
foreach ($export_data as $line){
fputcsv($file,$line);
}
fclose($file);
// download
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=".$filename);
header("Content-Type: application/csv; ");
readfile($filename);
// deleting file
unlink($filename);
exit();
你实际上在哪里创建CSV内容?@NigelRen嗨,我忘了上传那个代码。我已经编辑过,所以它包含下载。phpha你是否检查过$export\u数据在到达foreach()
块时是否是一个数组?也许可以用die('.print\r($export\u data,1)。''
在代码中使用die(mysqli_error($conn));
是一个非常糟糕的主意,因为它可能会泄漏敏感信息。有关更多解释,请参阅本文:
<?php
$filename = 'users.csv';
$export_data = unserialize($_POST['export_data']);
// file creation
$file = fopen($filename,"w");
foreach ($export_data as $line){
fputcsv($file,$line);
}
fclose($file);
// download
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=".$filename);
header("Content-Type: application/csv; ");
readfile($filename);
// deleting file
unlink($filename);
exit();