从mysql数据库导出csv文件

时间:2014-08-25 09:26:47

标签: php html mysql

以下代码是将csv文件导入数据库。

<?php
    include("includes/config.php"); 
    if ($_POST['frmSubmit']) {  
        $file = $_FILES['frmUpload']['tmp_name'];           //  Get Temporary filename
        if ($file) {
            $handle = fopen($file,"r");                         //  Open the file and read
            while ($strBookData = fgetcsv($handle, 10000, ",")) {       //  To get Array from CSV
                $strDatas[] = $strBookData;
                $strTableColumn = count($strBookData);              // To Get Column count
            }

            if ($strDatas) {
                $strInsertRecords = 0;
                $strDuplicationRecords = 0;
                $duplicateEmails = array();
                $strDup = "";
                if ($strTableColumn == 5) {
                    for($k=1; $k<count($strDatas); $k++) {
                        $strStatus = doCheckDuplication($strDatas[$k]['2']);                    
                        if ($strStatus==0) {
                            // Insert Code              
                            doInsertEmployeeDetails($strDatas[$k]['0'], $strDatas[$k]['1'], $strDatas[$k]['2'], $strDatas[$k]['3'], $strDatas[$k]['4']);
                            $strInsertRecords++;            // To Get Inserted Records Count.
                        } else {
                            $strDuplicationRecords++;       // To Get Duplication Records Count. 
                            $duplicateEmails[$strDuplicationRecords] = $strDatas[$k]['2'];
                            $strDup.= $duplicateEmails[$strDuplicationRecords]. "\n";
                        }
                    }
                    //printArray($duplicateEmails);
                    if  (count($strDatas)-1 == $strInsertRecords) {
                        $strMsg = 'Employee details inserted successfully';                                                                 
                        $strClass = 'Succes';
                    }
                    if  (count($strDatas)-1 != $strInsertRecords) {
                        $strMsg = 'Employee details inserted successfully, some of names already exists';                                                                   
                        $strClass = 'Error';
                    }
                    if  (count($strDatas)-1 == $strDuplicationRecords) {
                        $strMsg = 'Employee details are already exists';                                                                    
                        $strClass = 'Error';
                    }                   

                } else {
                    $strMsg = 'Column mis-match, Please verify the file.';                                                                  
                    $strClass = 'Error';
                }
            }
        } else {
            $strMsg = 'Please upload the valid file.';                                                                  
            $strClass = 'Error';
        }
    }
?>

<!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>Employee Details</title>
<link href="css/employee.css" rel="stylesheet" type="text/css"/>
<script src="js/employee.js" type="text/javascript"></script>
</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">EMPLOYEE DETAILS</p>
        </div>
        <p>&nbsp;</p>
      </div>
      <p>&nbsp;</p>
      <div align="center"><p class="<?php echo $strClass; ?>"><?php echo $strMsg; ?></p></div>
      <p>&nbsp;</p>
      <div class="ntop">
      <div class="nnn">
        <div class="name">CSV Upload:</div>
        <div class="field">
            <label>
              <input type="file" name="frmUpload" id="frmUpload" onblur="checkEmpty('frmUpload', 'error_file', 'Please upload your file');"/>
            </label>
        </div>
        <p>&nbsp;</p>
      </div>
      <div class="span">
        <div class="span2"><span id="error_file" class="spans"></span></div>
        <p>&nbsp;</p>
        </div>
        </div>
      <p>&nbsp;</p>
      <p>&nbsp;</p>
      <div class="submit">
        <div class="sub">
          <div class="but">
            <label>
            <input type="submit" name="frmSubmit" id="frmSubmit" value="Submit" class="subb" />
            </label>
          </div>
          <div class="but">
            <label>
            <input type="reset" name="frmReset" id="frmReset" value="Reset" class="subb" />
            </label>
          </div>
        </div>
        <p>&nbsp;</p>
        </div>
      </div>
    <p>&nbsp;</p>
    <?php  if ($_POST['frmSubmit']) { ?>
    <div class="info" id="one">
      <table width="59%" border="1" bordercolor="#DEDEDE" class="tabb">
        <tr>
          <td width="72%"><p class="rec">Total Records:</p> </td>
          <td width="28%"><p class="rec"><?php echo count($strDatas)-1; ?></p></td>
        </tr>
        <tr>
          <td><p class="rec">Inserted Records:</p></td>
          <td><p class="rec"><?php echo $strInsertRecords; ?></p></td>
        </tr>
        <tr>
          <td><p class="rec">Duplicate Records:</p></td>
          <td><p class="rec"><?php echo $strDuplicationRecords; ?></p></td>
        </tr>
      </table>
      </div>
      <div class="dup">
    <div class="duplicate">
      <div class="hea">
        <p class="rec">Duplicate Records</p>
        </div>
      <p style="height: 10px;"></p>
      <div style="padding-left: 10px;"><?php echo $strDup. "<br>"; ?></div>
      </div>
    </div>
      <?php } ?>
    </div>
    </div>
  </div>
  </form>
</body>
</html>

现在我想从数据库中导出它。请帮帮我。

1 个答案:

答案 0 :(得分:-1)

你可以从这个&#34; http://www.adminer.org/&#34;下载adminer.php。那将为你做这份工作。它具有以不同格式(如XML,CSV和文本)下载数据库的所有选项。

相关问题