LoginSignup
14
22

More than 5 years have passed since last update.

Windows 環境の PHP から Oracle へ接続する

Last updated at Posted at 2015-05-18

Windows 環境の PHP から Oracle へ接続する

Linux 向けの記事はたくさんありましたが、 Windows 向けの記事は見かけなかったので。

準備

Windows 版 PHP のインストール

WebMatrixWeb Platform Installer を使用すると良いでしょう。

ここでは PHP5.5.x を例にします。他のバージョンを使用する場合は、適宜読み替えてください。

Oracle Instant Client のダウンロード

Instant Clientのダウンロード より、 Instant Client for Microsoft Windows (32-bit) をダウンロードします。
64bit 版の Windows でも 32bit 版の Instant Client を使用することに注意してください。

今回は Version 11.2.0.3.0 を例にします。他のバージョンを使用する場合は、適宜読み替えてください。

セットアップ

ZIP の展開と DLL のコピー

Instant Client の ZIP ファイルを適当な場所に展開します。

展開したフォルダ直下にある、以下の DLL を PHP のインストールされたフォルダにコピーします。

  • oci.dll
  • ociw32.dll
  • orannzsbb11.dll
  • oraocci11.dll
  • oraociei11.dll
  • orasql11.dll

WebMatrix や Web Platform Installer を使用して、 PHP5.5.x をインストールした場合は、以下のフォルダにコピーします。

C:\Program Files (x86)\IIS Express\PHP\v5.5

php.ini の編集

C:\Program Files (x86)\IIS Express\PHP\v5.5\php.ini の末尾に以下を追加します。

;; extension=php_oci8.dll    ;; Oracle 11g より前のバージョンはこちらを有効に
extension=php_oci8_11g.dll   ;; Oracle 11g 以降のバージョンはこちらを有効に
extension=php_pdo_oci.dll

Oracle 11g をインストールしていても、 環境によっては Oracle 10g のクライアントが有効になっている場合があります。
その場合は、php_oci8.dll を有効にしないと接続に失敗しますので注意してください。

接続方法

http://php.net/manual/ja/function.oci-connect.php のサンプルが詳しいです。

Doctrine DBAL で、サービス名を指定して接続する例です。

<?php
$config = new \Doctrine\DBAL\Configuration();
//..
$connectionParams = array(
    'dbname' => '<SERVICE_NAME>',
    'user' => 'user',
    'password' => 'secret',
    'host' => 'localhost',
    'driver' => 'oci8',
    'port' => 1521,
    'charset' => 'AL32UTF8',
    'db_event_subscribers' => 'Doctrine\DBAL\Event\Listeners\OracleSessionInit',
    'service' => true
);
$conn = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config);
14
22
0

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
  3. You can use dark theme
What you can do with signing up
14
22