16. 10. 24.

PHP에서 ODBC 이용 MS-SQL 연동

<?php
 $ListArr = array();
 $conn = odbc_connect('dbserver', 'ID', 'PASSWORD');
 if($conn){
  $sql = "select top 10".PHP_EOL.
    "m_code,m_id,convert(char(10),m_regdate,120) as m_regdate".PHP_EOL.
    "from tbl_member".PHP_EOL.
    "order by m_code desc".PHP_EOL;
  $rs = odbc_exec($conn, $sql);
  while (odbc_fetch_row($rs)){
   array_push($ListArr,
    array(
     odbc_result($rs,"m_code"),
     iconv('euc-kr','utf-8',odbc_result($rs,"m_id")),
     odbc_result($rs,"m_regdate")
    )
   );
  }
  odbc_close($conn);
 }
?>
<table border="1">
<thead><tr><th>CODE</th><th>ID</th><th>DATE</th></tr></thead>
<tbody>
<?php
 if(count($ListArr)>0){
  foreach($ListArr as $arr1){
   echo "<tr><td>".$arr1[0]."</td><td>".$arr1[1]."</td><td>".$arr1[2]."</td></tr>";
  }
 }else{
  echo "<tr><td colspan=\"3\" align=\"center\">No Exist</td></tr>";
 }
?>
</tbody>
</table>

댓글 없음:

댓글 쓰기