<p style="box-sizing: border-box; margin-bottom: 10px; font-family: &quot;KoPub Dotum&quot;; text-align: justify; color: #333333; font-size: 14px; background-color: #ffffff;">MySQL DB 데이터를 csv 파일 형태로 저장하는 예제코드다.</p>
<p style="box-sizing: border-box; margin-bottom: 10px; font-family: &quot;KoPub Dotum&quot;; text-align: justify; color: #333333; font-size: 14px; background-color: #ffffff;"><br style="box-sizing: border-box;" /></p>
<table class="txc-table" 맑은="" 고딕",="" sans-serif;font-size:15px"="" width="763" cellspacing="0" cellpadding="0" border="0" style="border-spacing: 0px; background-color: #ffffff; color: #333333; font-family: &quot;KoPub Dotum&quot;; font-size: 14px; border-width: initial; border-style: none; border-color: initial;">
   <tbody style="box-sizing: border-box;">
      <tr style="box-sizing: border-box;">
         <td style="box-sizing: border-box; padding: 0px; text-align: center; border-style: solid;">
            <p style="box-sizing: border-box; margin-bottom: 10px; text-align: justify;"><span style="box-sizing: border-box; font-family: Tahoma;">&lt;?php<br style="box-sizing: border-box;" />$db['host'] = "localhost";<br style="box-sizing: border-box;" />$db['name'] = "db명";<br style="box-sizing: border-box;" />$db['user'] = "사용자id";<br style="box-sizing: border-box;" />$db['pass'] = "비밀번호";<br style="box-sizing: border-box;" />$db['port'] = "3306";<br style="box-sizing: border-box;" />?&gt;</span></p>
            </td>
         </tr>
      <tr style="box-sizing: border-box;">
         <td style="box-sizing: border-box; padding: 0px; text-align: center; border-bottom-style: solid; border-right-style: solid; border-left-style: solid;">
            <p style="box-sizing: border-box; margin-bottom: 10px; text-align: justify;"><span style="box-sizing: border-box; font-family: Tahoma;">&lt;?php<br style="box-sizing: border-box;" />include_once 'dbinfo.php';<br style="box-sizing: border-box;" />$db = isConnectDb($db);<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />function isConnectDb($db)<br style="box-sizing: border-box;" />{<br style="box-sizing: border-box;" />&nbsp;&nbsp;&nbsp; $conn = mysqli_connect($db['host'],$db['user'],$db['pass'],$db['name'],$db['port']);<br style="box-sizing: border-box;" />&nbsp;&nbsp;&nbsp; mysqli_set_charset($conn, "utf8");&nbsp;<br style="box-sizing: border-box;" />&nbsp;&nbsp;&nbsp; if (mysqli_connect_errno()) {<br style="box-sizing: border-box;" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; echo "Failed to connect to MySQL: " . mysqli_connect_error();<br style="box-sizing: border-box;" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit;<br style="box-sizing: border-box;" />&nbsp;&nbsp;&nbsp; } else {<br style="box-sizing: border-box;" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return $conn;<br style="box-sizing: border-box;" />&nbsp;&nbsp;&nbsp; }<br style="box-sizing: border-box;" />}<br style="box-sizing: border-box;" />?&gt;</span></p>
            </td>
         </tr>
      <tr style="box-sizing: border-box;">
         <td style="box-sizing: border-box; padding: 0px; text-align: center; border-bottom-style: solid; border-right-style: solid; border-left-style: solid;">
            <p style="box-sizing: border-box; margin-bottom: 10px; text-align: justify;">&nbsp;<span style="box-sizing: border-box; font-family: Tahoma;">&lt;?php<br style="box-sizing: border-box;" />$file_name = "cate_backup.xls";<br style="box-sizing: border-box;" />header( "Content-type: application/vnd.ms-excel; charset=utf-8");<br style="box-sizing: border-box;" />header( "Cache-Control: must-revalidate, post-check=0,pre-check=0" );<br style="box-sizing: border-box;" />header( "Content-Disposition: attachment; filename=$file_name" );<br style="box-sizing: border-box;" />header( "Content-Description: PHP4 Generated Data" );<br style="box-sizing: border-box;" /><span style="box-sizing: border-box; color: #2f9d27;">//header() 함수를 쓰기전에 어떠한 출력도 있어선 안된다</span><br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />include_once 'dbconect.php'; // DB 연동<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />$tblName = category;&nbsp;&nbsp; &nbsp;// 테이블명<br style="box-sizing: border-box;" />$sql ="SELECT uid, classname, relateduid FROM $tblName ORDER BY uid DESC";<br style="box-sizing: border-box;" /><span style="box-sizing: border-box; color: #2f9d27;">// DESC 내림차순 정렬, ASC 오름차순 정렬</span><br style="box-sizing: border-box;" />$result = mysqli_query($db,$sql);<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><span style="box-sizing: border-box; color: #2f9d27;">// 테이블 상단 만들기</span><br style="box-sizing: border-box;" />$EXCEL_STR = "<br style="box-sizing: border-box;" />&lt;table border='1'&gt;<br style="box-sizing: border-box;" />&lt;tr&gt;<br style="box-sizing: border-box;" />&lt;td align=center BGCOLOR='#9DEEE1'&gt;번호&lt;/td&gt;<br style="box-sizing: border-box;" />&lt;td align=center BGCOLOR='#9DEEE1'&gt;구분자&lt;/td&gt;<br style="box-sizing: border-box;" />&lt;td align=center BGCOLOR='#9DEEE1'&gt;연관번호&lt;/td&gt;<br style="box-sizing: border-box;" />&lt;/tr&gt;";<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />while ($row = mysqli_fetch_array($result) ){<br style="box-sizing: border-box;" />&nbsp;&nbsp; &nbsp;$EXCEL_STR .= "<br style="box-sizing: border-box;" />&nbsp;&nbsp; &nbsp;&lt;tr&gt;<br style="box-sizing: border-box;" />&nbsp;&nbsp; &nbsp;&lt;td align=center&gt;".$row['uid']."&lt;/td&gt;<br style="box-sizing: border-box;" />&nbsp;&nbsp; &nbsp;&lt;td align=center&gt;".$row['classname']."&lt;/td&gt;<br style="box-sizing: border-box;" />&nbsp;&nbsp; &nbsp;&lt;td align=center&gt;".$row['relateduid']."&lt;/td&gt;<br style="box-sizing: border-box;" />&nbsp;&nbsp; &nbsp;&lt;/tr&gt;<br style="box-sizing: border-box;" />&nbsp;&nbsp; &nbsp;";<br style="box-sizing: border-box;" />}<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />$EXCEL_STR .= "&lt;/table&gt;";<br style="box-sizing: border-box;" />echo $EXCEL_STR;<br style="box-sizing: border-box;" />exit;<br style="box-sizing: border-box;" />?&gt;<br style="box-sizing: border-box;" /></span></p>
            </td>
         </tr>
      </tbody>
   </table>
<p><br style="box-sizing: border-box; color: #333333; font-family: &quot;KoPub Dotum&quot;; font-size: 14px; background-color: #ffffff;" /><span style="color: #333333; font-family: &quot;KoPub Dotum&quot;; font-size: 14px; background-color: #ffffff;">출처:&nbsp;</span><a href="https://link2me.tistory.com/1619" style="box-sizing: border-box; background-color: #ffffff; color: #303030; text-decoration-line: none; transition: color 0.2s ease-in-out 0s; font-family: &quot;KoPub Dotum&quot;; font-size: 14px;" target="_blank">https://link2me.tistory.com/1619</a><span style="color: #333333; font-family: &quot;KoPub Dotum&quot;; font-size: 14px; background-color: #ffffff;">&nbsp;[소소한 일상 및 업무TIP 다루기]</span></p>
<p><span style="color: #333333; font-family: &quot;KoPub Dotum&quot;; font-size: 14px; background-color: #ffffff;">&nbsp;</span></p>
<p>&nbsp;</p>
<p style="padding-right: 0px; padding-left: 0px; color: #666666; background-color: #ffffff; font-family: dotum; line-height: 18px; padding-top: 0px !important; padding-bottom: 0px !important;">웹 소스 맨 상단에 추가 해줌.</p>
<p style="padding-right: 0px; padding-left: 0px; color: #666666; background-color: #ffffff; font-family: dotum; line-height: 18px; padding-top: 0px !important; padding-bottom: 0px !important;"><br /></p>
<p style="padding-right: 0px; padding-left: 0px; color: #666666; background-color: #ffffff; font-family: dotum; line-height: 18px; padding-top: 0px !important; padding-bottom: 0px !important;">header("Content-Type: application/vnd.ms-excel");</p>
<p style="padding-right: 0px; padding-left: 0px; color: #666666; background-color: #ffffff; font-family: dotum; line-height: 18px; padding-top: 0px !important; padding-bottom: 0px !important;">header("Content-Disposition: attachment; filename=test.xls");</p>
<p style="padding-right: 0px; padding-left: 0px; color: #666666; background-color: #ffffff; font-family: dotum; line-height: 18px; padding-top: 0px !important; padding-bottom: 0px !important;">header("Content-Description:PHP4 Generated Data");</p>
<p style="padding-right: 0px; padding-left: 0px; color: #666666; background-color: #ffffff; font-family: dotum; line-height: 18px; padding-top: 0px !important; padding-bottom: 0px !important;"><br /></p>
<p style="padding-right: 0px; padding-left: 0px; color: #666666; background-color: #ffffff; font-family: dotum; line-height: 18px; padding-top: 0px !important; padding-bottom: 0px !important;">아래와 같은 헤더도 있으나, 현재까지는 위 3개로만으로도 다운이 가능하다.</p>
<p style="padding-right: 0px; padding-left: 0px; color: #666666; background-color: #ffffff; font-family: dotum; line-height: 18px; padding-top: 0px !important; padding-bottom: 0px !important;">header("Content-Type: application/octet-stream");</p>
<p style="padding-right: 0px; padding-left: 0px; color: #666666; background-color: #ffffff; font-family: dotum; line-height: 18px; padding-top: 0px !important; padding-bottom: 0px !important;">header("Content-Type-Description: File Transfer");</p>
<p style="padding-right: 0px; padding-left: 0px; color: #666666; background-color: #ffffff; font-family: dotum; line-height: 18px; padding-top: 0px !important; padding-bottom: 0px !important;">header("Expires: 0");</p>
<p style="padding-right: 0px; padding-left: 0px; color: #666666; background-color: #ffffff; font-family: dotum; line-height: 18px; padding-top: 0px !important; padding-bottom: 0px !important;">header("Cache-Control: must-revalidate, post-check=0,pre-check=0");</p>
<p style="padding-right: 0px; padding-left: 0px; color: #666666; background-color: #ffffff; font-family: dotum; line-height: 18px; padding-top: 0px !important; padding-bottom: 0px !important;">header("Pragma; public");</p>
<p style="padding-right: 0px; padding-left: 0px; color: #666666; background-color: #ffffff; font-family: dotum; line-height: 18px; padding-top: 0px !important; padding-bottom: 0px !important;"><br /></p>
<p style="padding-right: 0px; padding-left: 0px; color: #666666; background-color: #ffffff; font-family: dotum; line-height: 18px; padding-top: 0px !important; padding-bottom: 0px !important;"><br /></p>
<p style="padding-right: 0px; padding-left: 0px; color: #666666; background-color: #ffffff; font-family: dotum; line-height: 18px; padding-top: 0px !important; padding-bottom: 0px !important;">이걸 응용하여 버튼 클릭시 다운받게 해보자.</p>
<p style="padding-right: 0px; padding-left: 0px; color: #666666; background-color: #ffffff; font-family: dotum; line-height: 18px; padding-top: 0px !important; padding-bottom: 0px !important;"><br /></p>
<p style="padding-right: 0px; padding-left: 0px; color: #666666; background-color: #ffffff; font-family: dotum; line-height: 18px; padding-top: 0px !important; padding-bottom: 0px !important;">버튼 페이지(button.php)</p>
<table class="txc-table" width="494" cellspacing="0" cellpadding="0" border="0" style="color: #666666; background-color: #ffffff; border-spacing: 0px; font-family: dotum; line-height: 18px; border-width: initial; border-style: none; border-color: initial;">
   <tbody>
      <tr>
         <td style="margin: 0px; padding: 0px; font-size: 14px; line-height: 1.5; font-family: &quot;Spoqa Han Sans&quot;, sans-serif; border-style: solid;">
            <p style="padding-right: 0px; padding-left: 0px; padding-top: 0px !important; padding-bottom: 0px !important;">&lt;script&gt;</p>
            <p style="padding-right: 0px; padding-left: 0px; padding-top: 0px !important; padding-bottom: 0px !important;">&nbsp; &nbsp; $.fn.outerHTML = function() {</p>
            <p style="padding-right: 0px; padding-left: 0px; padding-top: 0px !important; padding-bottom: 0px !important;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;var el = $(this);</p>
            <p style="padding-right: 0px; padding-left: 0px; padding-top: 0px !important; padding-bottom: 0px !important;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(!el[0]) return "";</p>
            <p style="padding-right: 0px; padding-left: 0px; padding-top: 0px !important; padding-bottom: 0px !important;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(el[0].outerHTML) {</p>
            <p style="padding-right: 0px; padding-left: 0px; padding-top: 0px !important; padding-bottom: 0px !important;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return el[0].outerHTML;</p>
            <p style="padding-right: 0px; padding-left: 0px; padding-top: 0px !important; padding-bottom: 0px !important;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} else {</p>
            <p style="padding-right: 0px; padding-left: 0px; padding-top: 0px !important; padding-bottom: 0px !important;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;var content = el.wrap('&lt;p/&gt;').parent().html();</p>
            <p style="padding-right: 0px; padding-left: 0px; padding-top: 0px !important; padding-bottom: 0px !important;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;el.unwrap();</p>
            <p style="padding-right: 0px; padding-left: 0px; padding-top: 0px !important; padding-bottom: 0px !important;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return content;</p>
            <p style="padding-right: 0px; padding-left: 0px; padding-top: 0px !important; padding-bottom: 0px !important;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</p>
            <p style="padding-right: 0px; padding-left: 0px; padding-top: 0px !important; padding-bottom: 0px !important;">&nbsp; &nbsp; }</p>
            <p style="margin-left: 2em; padding-right: 0px; padding-left: 0px; padding-top: 0px !important; padding-bottom: 0px !important;">function export_excel() {</p>
            <p style="margin-left: 4em; padding-right: 0px; padding-left: 0px; padding-top: 0px !important; padding-bottom: 0px !important;">var exp=$('#export').outerHTML(); &nbsp;//id 값이 export인 태그부터 가져옴.(위함수)</p>
            <p style="padding-right: 0px; padding-left: 0px; padding-top: 0px !important; padding-bottom: 0px !important;">//&nbsp;var exp=$<span style="font-size: 9pt; line-height: 1.5;">('#export').html(); =&gt; id값이 export 인 태그의 자식 태그들만 가져옴.</span></p>
            <p style="margin-left: 4em; padding-right: 0px; padding-left: 0px; padding-top: 0px !important; padding-bottom: 0px !important;">$('#data').val(exp);</p>
            <p style="margin-left: 4em; padding-right: 0px; padding-left: 0px; padding-top: 0px !important; padding-bottom: 0px !important;">$('#export_data').submit();</p>
            <p style="margin-left: 2em; padding-right: 0px; padding-left: 0px; padding-top: 0px !important; padding-bottom: 0px !important;">}</p>
            <p style="padding-right: 0px; padding-left: 0px; padding-top: 0px !important; padding-bottom: 0px !important;">&lt;/script&gt;</p>
            <p style="padding-right: 0px; padding-left: 0px; padding-top: 0px !important; padding-bottom: 0px !important;">&lt;div&gt;</p>
            <p style="margin-left: 2em; padding-right: 0px; padding-left: 0px; padding-top: 0px !important; padding-bottom: 0px !important;">&lt;input type="button" value="Excel" onclick="export_excel()"&gt;</p>
            <p style="margin-left: 2em; padding-right: 0px; padding-left: 0px; padding-top: 0px !important; padding-bottom: 0px !important;">&lt;form id="export_data" method="post" action="./Excel.php"&gt;</p>
            <p style="margin-left: 4em; padding-right: 0px; padding-left: 0px; padding-top: 0px !important; padding-bottom: 0px !important;">&lt;input type="hidden" id="data" name="data" /&gt;</p>
            <p style="margin-left: 2em; padding-right: 0px; padding-left: 0px; padding-top: 0px !important; padding-bottom: 0px !important;">&lt;/form&gt;</p>
            <p style="padding-right: 0px; padding-left: 0px; padding-top: 0px !important; padding-bottom: 0px !important;">&lt;/div&gt;</p>
            </td>
         </tr>
      </tbody>
   </table>
<p style="padding-right: 0px; padding-left: 0px; color: #666666; background-color: #ffffff; font-family: dotum; line-height: 18px; padding-top: 0px !important; padding-bottom: 0px !important;"><br /></p>
<p style="padding-right: 0px; padding-left: 0px; color: #666666; background-color: #ffffff; font-family: dotum; line-height: 18px; padding-top: 0px !important; padding-bottom: 0px !important;">다운로드 실행 페이지(Excel.php)</p>
<p style="padding-right: 0px; padding-left: 0px; color: #666666; background-color: #ffffff; font-family: dotum; line-height: 18px; padding-top: 0px !important; padding-bottom: 0px !important;">&nbsp;</p>
<table class="txc-table" width="494" cellspacing="0" cellpadding="0" border="0" style="color: #666666; background-color: #ffffff; border-spacing: 0px; line-height: 18px; border-width: initial; border-style: none; border-color: initial; font-family: 돋움;">
   <tbody>
      <tr>
         <td style="margin: 0px; padding: 0px; font-size: 14px; line-height: 1.5; font-family: &quot;Spoqa Han Sans&quot;, sans-serif; border-style: solid;">
            <p style="padding-right: 0px; padding-left: 0px; padding-top: 0px !important; padding-bottom: 0px !important;">header("Content-Type: application/vnd.ms-excel");</p>
            <p style="padding-right: 0px; padding-left: 0px; padding-top: 0px !important; padding-bottom: 0px !important;">header("Content-Disposition: attachment; filename=test.xls");</p>
            <p style="padding-right: 0px; padding-left: 0px; padding-top: 0px !important; padding-bottom: 0px !important;">header("Content-Description:PHP4 Generated Data");</p>
            <p style="padding-right: 0px; padding-left: 0px; padding-top: 0px !important; padding-bottom: 0px !important;"><br /></p>
            <p style="padding-right: 0px; padding-left: 0px; padding-top: 0px !important; padding-bottom: 0px !important;">$data = $_REQUEST['data'];</p>
            <p style="padding-right: 0px; padding-left: 0px; padding-top: 0px !important; padding-bottom: 0px !important;">echo "&lt;meta content=\"application/vnd.ms-excel;charset=UTF-8;\" name=\"Content-type\"&gt;";</p>
            <p style="padding-right: 0px; padding-left: 0px; padding-top: 0px !important; padding-bottom: 0px !important;">echo $data;</p>
            </td>
         </tr>
      </tbody>
   </table>
<p style="padding: 0px; color: #666666; font-family: &quot;Spoqa Han Sans&quot;, sans-serif; font-size: 14px; background-color: #ffffff;">&nbsp;</p>
<p><br style="color: #666666; font-family: &quot;Spoqa Han Sans&quot;, sans-serif; font-size: 14px; background-color: #ffffff;" /><span style="color: #666666; font-family: &quot;Spoqa Han Sans&quot;, sans-serif; font-size: 14px; background-color: #ffffff;">출처:&nbsp;</span><a href="https://blog.goodgods.com/251" style="color: #333333; text-decoration-line: none; font-family: &quot;Spoqa Han Sans&quot;, sans-serif; font-size: 14px; background-color: #ffffff;" target="_blank">https://blog.goodgods.com/251</a><span style="color: #666666; font-family: &quot;Spoqa Han Sans&quot;, sans-serif; font-size: 14px; background-color: #ffffff;">&nbsp;[개발은 너무해]</span></p>