もう、いまどき PHP + MDB なんてのは、仕事でしか書かないですが・・・ こんなことする人もまれだとは思いますが、知ってると結構便利だと思いますよ。
<?php header( "Content-Type: text/html; charset=utf-8" ); session_cache_limiter('nocache'); session_start(); require_once("db.php"); $connect_string = "Provider=MSDASQL;"; $connect_string .= "Driver={Microsoft Access Driver (*.mdb, *.accdb)};"; $connect_string .= "dbq=C:\\Users\\sworc\\Downloads\\hanbaic_masters.mdb;"; $db = new MDB( $connect_string, "UTF-8" ); $query = "select * from [社員マスタ]"; $column = $db->query_ex( $query ); while ( $column ) { print "<pre>"; print_r( $column ); print "</pre>"; $column = $db->query_ex( ); }; $db->close(); ?>
db.php
<?php // *************************** // データベースクラス // *************************** class MDB { var $connect; var $result; var $encoding; // *************************** // コンストラクタ // *************************** function MDB( $connect_string, $encoding="MS932", $user='', $password='' ) { $this->encoding = $encoding; $this->connect = odbc_connect($connect_string, $user, $password); } // *************************** // 接続解除 // *************************** function close( ) { odbc_close( $this->connect ); } // *************************** // クエリー // *************************** function query( $sql_query ) { if ( $this->encoding != "MS932" ) { $sql_query = mb_convert_encoding($sql_query,"MS932",$this->encoding); } $ret = odbc_exec( $this->connect, $sql_query ); return $ret; } // *************************** // フェッチ // *************************** function fetch( $result ) { $ret = odbc_fetch_array( $result ); if ($ret) { if ( $this->encoding != "MS932" ) { foreach($ret as $_key => $_value) { $ret2[mb_convert_encoding($_key,$this->encoding,"MS932")] = mb_convert_encoding($_value,$this->encoding,"MS932"); } } else { $ret2 = $ret; } $cnt = 0; foreach($ret as $_key => $_value) { if ( $this->encoding != "MS932" ) { $ret2[$cnt] = mb_convert_encoding($_value,$this->encoding,"MS932"); } else { $ret2[$cnt] = $_value; } $cnt++; } return $ret2; } else { return false; } } // *************************** // クエリーとフェッチ // *************************** function query_ex( $sql_query='' ) { if ( $sql_query != '' ) { $this->result = $this->query( $sql_query ); if ( !$this->result ) { return false; } return $this->fetch( $this->result ); } else { return $this->fetch( $this->result ); } } // *************************** // 実行 // *************************** function Execute( $sql_exec ) { if ( $this->encoding != "MS932" ) { $sql_exec = mb_convert_encoding($sql_exec,"MS932",$this->encoding); } $ret = odbc_exec( $this->connect, $sql_exec ); return $ret; } } ?>
▼ 出力結果の例
Array ( [社員コード] => 0001 [氏名] => 浦岡 友也 [フリガナ] => ウラオカ トモヤ [所属] => 0003 [性別] => 0 [作成日] => 2005-09-12 00:00:00 [更新日] => 2005-11-28 00:00:00 [給与] => 270000 [手当] => 9000 [管理者] => [生年月日] => 2000-01-01 00:00:00 [社員区分] => 0 [年代] => A [0] => 0001 [1] => 浦岡 友也 [2] => ウラオカ トモヤ [3] => 0003 [4] => 0 [5] => 2005-09-12 00:00:00 [6] => 2005-11-28 00:00:00 [7] => 270000 [8] => 9000 [9] => [10] => 2000-01-01 00:00:00 [11] => 0 [12] => A ) Array ( [社員コード] => 0002 [氏名] => 山村 洋代 [フリガナ] => ヤマムラ ヒロヨ [所属] => 0003 [性別] => 1 [作成日] => 2005-06-17 00:00:00 [更新日] => 2005-09-18 00:00:00 [給与] => 300000 [手当] => [管理者] => [生年月日] => 2000-01-02 00:00:00 [社員区分] => 0 [年代] => A [0] => 0002 [1] => 山村 洋代 [2] => ヤマムラ ヒロヨ [3] => 0003 [4] => 1 [5] => 2005-06-17 00:00:00 [6] => 2005-09-18 00:00:00 [7] => 300000 [8] => [9] => [10] => 2000-01-02 00:00:00 [11] => 0 [12] => A )
昔仕事場の古い SQLServer がぶっ壊れた時、新しい OS やら新しい SQLServer やらで結局 PHP からアクセスする DB のドライバは ODBC で総とっかえしました。これが一番簡単で安全だという結果になっています