我尝试使用以下代码导出csv,xls,txt和amp;来自mysql数据库的xml文件。
<?php
include("includes/config.php");
if($_POST["frmDownloadFiles"]){
$output = "";
$line_termineted="\n";
if( $_POST["frmDownloadFiles"] =="CSV") {
$field_termineted= ",";
}
if( $_POST["frmDownloadFiles"] =="XLS") {
$field_termineted= "\t";
}
if ($_POST["frmDownloadFiles"] =="TXT") {
$field_terminated= " ";
}
if ($_POST["frmDownloadFiles"] =="XML") {
$field_terminated= "\t";
}
$enclosed='';
$escaped="\\";
$export_schema = "Name".$field_termineted."Code".$field_termineted."Email".$field_termineted."Designation".$field_termineted."Salary";
$dataQuery = doSelectCsv();
//$handle = fopen($dataQuery, "w+");
// while ($strBookData = fputcsv($handle, 10000, ",")) { // To get Array from CSV
// $strDatas[] = $strBookData;
// }
// printArray($strDatas); exit;
$strDatas = array();
$strDatas = $dataQuery;
//printArray($strDatas); exit;
$output.= $export_schema;
//printArray($field_termineted);
$p=0;
for($k=0; $k<count($strDatas); $k++) {
$p++;
if( $_POST["frmDownloadFiles"] =="CSV") {
$output.= $line_termineted;
}
if( $_POST["frmDownloadFiles"] =="XLS") {
$output.= $line_termineted;
}
if( $_POST["frmDownloadFiles"] =="TXT") {
$output.= $line_termineted;
}
echo "<employee_details>";
if( $_POST["frmDownloadFiles"] =="XML") {
echo "<Row>";
echo "<name>" . $strDatas[$k]['0'] . "</name>";
echo "<code>" . $strDatas[$k]['1'] . "</code>";
echo "<mail>" . $strDatas[$k]['2'] . "</mail>";
echo "<designation>" . $strDatas[$k]['3'] . "</designation>";
echo "<salary>" . $strDatas[$k]['4'] . "</salary>";
echo "</Row>";
}
echo "</employee_details>";
$output.=$enclosed.$strDatas[$k]['0'].$enclosed.$field_termineted;
$output.=$enclosed.$strDatas[$k]['1'].$enclosed.$field_termineted;
$output.=$enclosed.$strDatas[$k]['2'].$enclosed.$field_termineted;
$output.=$enclosed.$strDatas[$k]['3'].$enclosed.$field_termineted;
$output.=$enclosed.$strDatas[$k]['4'].$enclosed.$field_termineted;
}
header("Content-Description: File Transfer");
if( $_POST["frmDownloadFiles"] =="CSV"){
header("Content-Type: application/csv");
header("Content-Disposition: attachment; filename=report".date("d_m_Y_H_i_s").".csv");
}
if( $_POST["frmDownloadFiles"] =="XLS") {
header("Content-Type: application/vnd.ms-excel");
header("Content-disposition: attachment; filename=report".date("d_m_Y_H_i_s").".xls");
}
if( $_POST["frmDownloadFiles"] =="TXT") {
header("Content-Type: application/txt");
header("Content-disposition: attachment; filename=report".date("d_m_Y_H_i_s").".txt");
}
if( $_POST["frmDownloadFiles"] =="XML") {
header("Content-Type: application/xml");
header("Content-disposition: attachment; filename=report".date("d_m_Y_H_i_s").".xml");
}
header("Content-Transfer-Encoding: binary");
header("Expires: 0");
header("Cache-Control: must-revalidate");
header("Pragma: public");
header("Content-Length: ".strlen($output));
ob_clean();
flush();
echo $output;
exit;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Export Files</title>
<link href="css/export.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<form id="frmEmployee" name="frmEmployee" enctype="multipart/form-data" method="post" action="" onsubmit="return validation();">
<div class="all">
<div class="alls">
<div class="main">
<div class="inner">
<div class="top">
<p> </p>
<div class="text" align="center">
<p class="det">DOWNLOAD FILES</p>
</div>
<p> </p>
</div>
<p> </p>
<p> </p>
<div class="nnn">
<div class="name">Download CSV file:</div>
<div class="field">
<label>
<input type="submit" name="frmDownloadFiles" id="frmDownloadFiles" value="CSV" class="subb" />
</label>
</div>
<p> </p>
</div>
<p> </p>
<div class="nnn">
<div class="name">Download excel file:</div>
<div class="field">
<label>
<input type="submit" name="frmDownloadFiles" id="frmDownloadFiles" value="XLS" class="subb" />
</label>
</div>
<p> </p>
</div>
<p> </p>
<div class="nnn">
<div class="name">Download text file:</div>
<div class="field">
<label>
<input type="submit" name="frmDownloadFiles" id="frmDownloadFiles" value="TXT" class="subb" />
</label>
</div>
<p> </p>
</div>
<p> </p>
<div class="nnn">
<div class="name">Download xml file:</div>
<div class="field">
<label>
<input type="submit" name="frmDownloadFiles" id="frmDownloadFiles" value="XML" class="subb" />
</label>
</div>
<p> </p>
</div>
</div>
<p> </p>
</div>
</div>
</div>
</form>
</body>
</html>
前三个工作正常,但xml无法从数据库导出。请找我的错误并帮助我。提前致谢
答案 0 :(得分:0)
这是因为对于除XML之外的其他方法,您将结果存储在$output
变量(最后打印的变量)中。但是对于XML格式,您只需回显结果(例如,不使用$ouput .= '<Row>'
)。
在任何先前输出之前使用PHP中的header
函数(例如,由echo
生成)。
替换你的:
echo "<employee_details>";
if( $_POST["frmDownloadFiles"] =="XML") {
echo "<Row>";
echo "<name>" . $strDatas[$k]['0'] . "</name>";
echo "<code>" . $strDatas[$k]['1'] . "</code>";
echo "<mail>" . $strDatas[$k]['2'] . "</mail>";
echo "<designation>" . $strDatas[$k]['3'] . "</designation>";
echo "<salary>" . $strDatas[$k]['4'] . "</salary>";
echo "</Row>";
}
echo "</employee_details>";
使用
$output .= "<employee_details>";
if( $_POST["frmDownloadFiles"] =="XML") {
$output .= "<Row>";
$output .= "<name>" . $strDatas[$k]['0'] . "</name>";
$output .= "<code>" . $strDatas[$k]['1'] . "</code>";
$output .= "<mail>" . $strDatas[$k]['2'] . "</mail>";
$output .= "<designation>" . $strDatas[$k]['3'] . "</designation>";
$output .= "<salary>" . $strDatas[$k]['4'] . "</salary>";
$output .= "</Row>";
}
$output .= "</employee_details>";
答案 1 :(得分:0)
使用php从xml导出mysql数据, 试试这段代码:
<?php
$dbh=mysql_connect($localhost, $username, $password) or die ('I cannot connect to the database because: ' . mysql_error());
$result = mysql_query("SELECT * FROM 12345_flv.flv WHERE enabled = '1' ORDER BY id DESC") or die('Could not connect: ' . mysql_error());
$string = '<videos><updated>2010-07-20T00:00:00Z</updated><video>';
while ($row = mysql_fetch_array($result)) {
$id=$row['id'];
$title=$row['title'];
$string .='<id>'.$id.'</id>';
$string .='<title>'.$title.'</title>';
}
$string .='</video></videos>';
$xml = new SimpleXMLElement($string);
Header('Content-type: text/xml');
echo $xml->asXML();
?>