php 에서 MSSQL 연동하기 ( FreeTDS 이용)
URL : http://www.freeTDS.org
stable 버전을 다운받으면 된다.
구분하기 쉬우라고 다운받아서 /usr/local/src/APM 아래에 옮겨버렸다.
아 맞다 우선 설치환경부터
OS : CentOS 5.3
php : php-5.2.10
apache : httpd-2.2.11
SQL : MSSQL- ???
디비 버전을 모르겠다. 설치와는 완전 무관하기에 그냥 넘어간다.
다운받은 파일의 압축을 푼다.
# cd /usr/local/src/APM/
# tar xvzf freetds-stable.tgz
압축을 풀면 freetds-0.82 라는 폴더가 생긴다. 이동하자. 현재 버전이 0.82 인가 보다.
# cd freetds-0.82
이제 설치하자
# ./configure --prefix=/usr/local/server/freetds --with-tdsver=8.0 --disable-odbc --disable-debug --enable-msdblib
# make && make install
php를 재설치해야 한다.
# ./configure --prefix=/usr/local/server/php --with-apxs2=/usr/local/server/apache/bin/apxs --with-mysql=/usr/local/server/mysql --with-config-file-path=/usr/local/server/apache/conf --disable-debug --enable-safe-mode --enable-track-vars --enable-sockets --with-charset=utf8 --with-xml --with-language=korean --enable-calendar --enable-sysvsem=yes --enable-sysvshm=yes --enable-ftp --enable-magic-quotes --enable-gd-native-ttf --enable-url-includes --enable-inline-optimization --enable-bcmath --with-jpeg --with-png --with-zlib --with-jpeg-dir=/usr --with-png-dir=/usr/lib --with-freetype-dir=/usr --with-libxml-dir=/usr --enable-exif --with-gd --with-ttf --with-gettext --enable-sigchild --enable-mbstring --with-mssql=/usr/local/server/freetds
기존 옵션에 하나만 더 추가했다.
# make && make install
아파치를 재실행한다.
# /etc/init.d/httpd restart
php 정보를 확인하면 다음과 같은 mssql 이 추가된 것을 볼 수 있다.
한글이 깨지는 것을 막기 위해 php.ini 설정을 다음과 같이 수정해준다.
# cd /usr/local/server/apache/conf/
# vim php.ini
인코딩은 각자의 웹 환경에 맞춰서 입맛대로 바꿔주면 되겠다.
아래와 같은 코드로 테스트를 해보자.
[msdbtest.php]
<html>
<head>
<title>mssql 접속 테스트</title>
</head>
<body>
일본어 데이터들어 분류번호와 분류명을 가져오는걸 테스트해본다.
<br><br>
<?php
$DB_IP ="***.***.***.***:1433"; //기본 포트port 1433
$DB_USERID ="*******";
$DB_USERPW ="**********";
$DB_NAME ="******";
$conn = mssql_connect($DB_IP,$DB_USERID,$DB_USERPW);
$result = mssql_select_db($DB_NAME, $conn);
$result = mssql_query("SELECT term_name, term_remark FROM terms1 WHERE LEN(term_remark)<=5");
?>
<table border=1>
<tr>
<td>번호</td>
<td>분류항목</td>
<td>분류어</td>
</tr>
<?
$i=1;
while ($row = mssql_fetch_row($result)) {
$resultData="<tr><td>".$i++."</td><td>".$row[1]."</td><td>".$row[0]."</td></tr>";
echo $resultData;
}
mssql_close($conn);
?>
</table>
</body>
</html>
현재 구축되어있는 mssql 에 접속해서 읽어왔기 때문에 디비 생성이나 데이터 입력하는 부분은 빠져있다.
이 포스트를 참조하시는 분들은 본인의 환경에 맞춰서 php 코드를 수정하면 결과를 확인할 수 있을것이다.
'프로그래밍 Tip > PHP' 카테고리의 다른 글
[PHP] 주간 날짜 구하기, 이전주, 다음주 (0) | 2021.03.17 |
---|---|
[PHP] Fatal error: Allowed memory size of 134217728 bytes exhausted (0) | 2017.09.25 |
[PHP] ZipArchive 클래스 모듈 설치하기 (0) | 2017.09.21 |
[PHP] php 로 웹페이지 긁어오기 모든 방법 총정리! (0) | 2017.08.23 |
[오류] Assigning the return value of new by reference is deprecated (0) | 2014.09.11 |