doulan6150 2014-08-26 10:55
浏览 42
已采纳

使用php从数据库导出xml文件

I tried the following code for exporting csv, xls, txt & xml files from the mysql database.

<?php
    include("includes/config.php"); 

    if($_POST["frmDownloadFiles"]){ 
        $output = "";
        $line_termineted="
";

        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>&nbsp;</p>
        <div class="text" align="center">
          <p class="det">DOWNLOAD FILES</p>
        </div>
        <p>&nbsp;</p>
      </div>
      <p>&nbsp;</p>
      <p>&nbsp;</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>&nbsp;</p>
      </div>
      <p>&nbsp;</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>&nbsp;</p>
      </div>
      <p>&nbsp;</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>&nbsp;</p>
      </div>
      <p>&nbsp;</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>&nbsp;</p>
      </div>
      </div>
    <p>&nbsp;</p>
    </div>
</div>
  </div>
  </form>
</body>
</html>

The first three works correctly, but xml cannot export from database. Please find my error and help me. Thanks in advance

  • 写回答

2条回答 默认 最新

  • dongmingxiang0312 2014-08-26 11:04
    关注

    For exporting data from mysql in the xml using php, try this code:

    <?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();
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler