LoginSignup
7

More than 5 years have passed since last update.

posted at

updated at

Linux PHPからODBCでSQL Serverに接続

はじめに

LInuxからSQL ServerにODBCで接続を読んで、isqlで接続確認していることを想定します。
設定していない方は上記のサイトで設定を行って下さい。

条件

  • isqlでSQL Serverに接続確認済み
  • DSN名: 2008R2
  • DB username: sa
  • DB password: password
  • sudo tasksel lamp-serverでApacheとPHPがインストール設定済み

php5-odbcドライバーをインストール

下記のようにphpのodbcドライバーをインストールします。

$ sudo apt-get install php5-odbc

wwwrootに移動

$ cd /var/www

phpファイルを作成

$ sudo vi odbc_2008R2.php

odbc_2008R2.php
<?php

$conn=odbc_connect(
"2008R2","sa","password");

if($conn){
 print "Conneted!</br>";
}
else
{
 print "error";
}
$query_result=odbc_exec($conn,"SELECT * FROM [日本語テーブル達].[dbo].[名前リスト]");
$row=odbc_fetch_array($query_result);

if($row !=FALSE)
{
echo "{$row['名前']}<br />";
}
odbc_free_result($query_result);
odbc_close($conn);

?>

以下のように表示されていれば成功です。

ブラウザー上の表示
Conneted!
織田 信長

PHP7.0の場合

PHP7の場合は下記のパッケージを使えばPHPでODBCに接続することができる。

Ubuntu/Debian
php7.0-odbc

debian/ubuntu
~# apt-get install php7.0-odbc

RedHat/CentOS
php7.0w-odbc

redhat/centos
~# yum install php7.0w-odbc

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
7