Php 如何将查询结果从表导出到csv?
因此,我有几个页面查询我们的数据库,然后在网页上的一个大表中显示结果。还有一个按钮,允许用户将结果导出到csv文件。第一个代码段工作得非常好。但其他一些页面不起作用,打开一个csv文件时什么都并没有。上周我让它短暂工作,但后来又停止了工作。当我让它工作时,它似乎是一个格式不一致,这解释了为什么它会在一个页面上工作,而不是另一个页面。同样由于这个原因,我认为导出它的js中不存在问题 下面是工作片段:Php 如何将查询结果从表导出到csv?,php,javascript,html,sql,Php,Javascript,Html,Sql,因此,我有几个页面查询我们的数据库,然后在网页上的一个大表中显示结果。还有一个按钮,允许用户将结果导出到csv文件。第一个代码段工作得非常好。但其他一些页面不起作用,打开一个csv文件时什么都并没有。上周我让它短暂工作,但后来又停止了工作。当我让它工作时,它似乎是一个格式不一致,这解释了为什么它会在一个页面上工作,而不是另一个页面。同样由于这个原因,我认为导出它的js中不存在问题 下面是工作片段: </div></td> <td ><div id="la
</div></td>
<td ><div id="layer2">
<p class="bodytext">
<?php
//select the table
$query = "SELECT * FROM CEP_T ORDER BY Cep_ID, Student_ID";
$result = mysql_query($query);
$numberOfCeps = mysql_numrows($result);
?>
<html>
<head>
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
<script type="text/javascript" src="myJavaScripts.js"></script>
<script type="text/javascript" src="tableSort.js"></script>
<title>Display CEPs Info...</title>
<link REL=StyleSheet HREF="formatStyles.css" TYPE="text/css" MEDIA=screen,print>
</head>
<body>
<h3>Display <?php echo $numberOfCeps; ?> CEP designees:</h3>
<div style="text-align: left; font-family: Arial;"><small><big><big>
<!-- <span style="font-family: Helvetica,Arial,sans-serif;"><a href="reports.php">Back To Reports!</a> </span> -->
<form action="getCSV.php" method ="post" >
<input type="hidden" name="csv_text" id="csv_text">
<input type="submit" value="Get CSV File"
onclick="getCSVData()">
</form>
<script type="text/javascript" src="javaExport.js">
</script>
<script type="text/javascript">
function getCSVData(){
var csv_value=$('#displayAllCeps').table2CSV({delivery:'value'});
$("#csv_text").val(csv_value);
}
</script>
<div style="text-align: left; font-family: Arial;"><small><big>
<?php (then it goes on to define the headers and place the results in table cells)
显示CEPs信息。。。
展示CEP指定人员:
函数getCSVData(){
var csv_value=$('#displayAllCeps').table2CSV({delivery:'value'});
$(“#csv_文本”).val(csv_值);
}
好吧,你的坏代码在
标记之前有一些html片段,这绝对不是一件好事。欢迎使用StackOverflow。请不要在新代码中使用mysql_*函数。它们不再被维护,并被正式弃用。而是学习准备好的语句,并使用PDO或MySQLi。这篇文章:将帮助你决定哪一个。数据中是否有逗号、单引号或其他可能破坏CSV的字符?您的导出库是否有任何可以打开的错误报告或调试代码?不必为您的无知道歉,但您应该知道,调试比盯着代码看几天要好。系统地处理问题,以便能够准确地指出问题所在。不管怎样,@MarcB指出了第一个显而易见的问题。如果不是这样,显示输入、显示SQL、检查结果……我知道这有点延迟。仍然习惯于在网站上使用Etiquit。无论如何,问题的原因是javascript函数将报告的全部内容作为单个变量传递(通过获取每行的所有内容)。在报表大小变得太大之前,这一切都很正常。我创建了php函数,再次调用查询并导出csv。现在效果很好。
</div></td>
<td><div id="layer2">
<p class="bodytext">
<?php
$contactType = $_GET['contactType'];
$company = $_GET['company'];
$endingCreateDate='';
$beginningCreateDate='';
$beginningCreateDate = date("Y-m-d",strtotime($_GET['beginningCreateDate']));
$endingCreateDate = date("Y-m-d",strtotime($_GET['endingCreateDate']));
$filterwhere="";
$filterfrom="";
if($contactType!=""){
$filterwhere=" AND ct.Contact_Type_ID=$contactType ".$filterwhere;
//$filterfrom=", CONTACT_T".$filterfrom;
}
if($beginningCreateDate!="1969-12-31"){
$filterwhere=" AND Date_Created > '$beginningCreateDate' ".$filterwhere;
//$filterfrom=", CONTACT_T".$filterfrom;
}
if($endingCreateDate!="1969-12-31"){
$filterwhere=" AND Date_Created < '$endingCreateDate' ".$filterwhere;
//$filterfrom=", CONTACT_T".$filterfrom;
}
if($company!=""){
$filterwhere=" AND Individual_Company='$company' ".$filterwhere;
//$filterfrom=", INDIVIDUAL_WORK_INFO_T".$filterfrom;
}
$query = "SELECT i.Individual_ID,First_Name,Last_Name,Email_Address, Address1,Address2,City,State,Postal_Code,Country,Home_Phone,Mobile_Phone,Individual_Company,Work_Phone,Work_Extension,Region,Date_Created, Last_Meeting_Date, Referred_By, c.Note, ct.Contact_Type, i.Do_Not_Mail
FROM INDIVIDUAL_T i
INNER JOIN CONTACT_T c ON c.Contact_ID=i.Individual_ID
INNER JOIN INDIVIDUAL_WORK_INFO_T iw ON iw.Individual_ID =i.Individual_ID
INNER JOIN CONTACT_TYPE_T ct ON c.Contact_Type=Contact_Type_ID".$filterfrom."
WHERE Is_Student = 'No' AND Is_Contact = 'Yes'".$filterwhere."
ORDER BY Last_Name, First_Name";
$result = mysql_query($query);
$numberOfRows = mysql_numrows($result);
?>
<html>
<head>
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
<script type="text/javascript" src="myJavaScripts.js"></script>
<script type="text/javascript" src="tableSort.js"></script>
<link REL=StyleSheet HREF="formatStyles.css" TYPE="text/css" MEDIA=screen,print>
<title>Display all contacts...</title>
</head>
<body>
<h3>Display <?php echo $numberOfRows; ?> contacts...</h3>
<div style="text-align: left; font-family: Arial;"><small><big><big>
<!-- <span style="font-family: Helvetica,Arial,sans-serif;"><a href="reports.php">Back To Reports!</a> </span> -->
<form action="getCSV.php" method ="post" >
<input type="hidden" name="csv_text" id="csv_text">
<input type="submit" value="Get CSV File"
onclick="getCSVData()">
</form>
<script type="text/javascript" src="javaExport.js">
</script>
<script type="text/javascript">
function getCSVData(){
var csv_value=$('#displayAllContacts').table2CSV({delivery:'value'});
$("#csv_text").val(csv_value);
}
</script>
<div style="text-align: left; font-family: Arial;"><small><big>
<!--<div id="Book1_10219" align=center x:publishsource="Excel">-->
<?php(then it goes on to define the headers and place the results in table cells)