PHP 5.3.20 から MSSQL (sqlserver 2008) への 接続について
Ubuntu で PHP 5.3.20 から MSSQL(SqlServer)への接続について
以下の設定で行うことができます。 ※PHP 5.3 以降は本来、Microsoftからとってくるのが良いようです。
- unixODBC install download from ftp://ftp.unixodbc.org/pub/unixODBC/unixODBC-2.3.1.tar.gz
tar xvzf unixODBC-2.3.1.tar.gz cd unixODBC-2.3.1 ./configure make make install
- freetds install download from ftp://ftp.astron.com/pub/freetds/stable/freetds-stable.tgz (freetds-0.91) 2013/1/17
tar xvzf freetds-stable.tgz cd freetds-0.91 ./configure --with-unixodbc=/usr/local make make install
- ここで、ライブラリをロードします
/sbin/ldconfig
- envrionment file settings 4.1. /usr/local/etc/odbcinst.ini
[FreeTDS] Description = TDS driver Driver = /usr/local/lib/libtdsodbc.so.0 Setup = /usr/lib/libtdsS.so CPTimeout = CPReuse = FileUsage = 1
4.2. /usr/local/etc/odbc.ini
[SQLSERVER] Driver = FreeTDS Description = sqlserver Trace = No Servername = backend Database = testdb
4.3. /usr/local/etc/freetds.conf の最後に追加
[SQLSERVER] host = backend port = 1433 tds version = 7.2 charset = cp932 client charset = UTF-8 language = english
- ODBC設定の確認
#isql -v 識別子 接続ユーザー 接続パスワード isql -v SQLSERVER sa admin +---------------------------------------+ | Connected! | | | | sql-statement | | help [tablename] | | quit | | | +---------------------------------------+ SQL>
- php install download from http://www.php.net/get/php-5.3.20.tar.gz/from/jp.php.net/mirror
./configure --prefix=/usr/local/php-5.3.20 --enable-mbstring --with-apxs2=/usr/local/httpd/bin/apxs --with-mssql --with-pdo-odbc=unixODBC make make install
となります。
<?php $dbh= new PDO('odbc:dbname', 'user', 'pass'); $stmt = $dbh->prepare("select * from selecttable"); $stmt->execute(); while ($row = $stmt->fetch()) { print_r($row); } unset($dbh); unset($stmt); ?>
で記述します。
結構古い記事が多かったので改めて整理してみました。