※ COM を使った MDB のアクセスはクラス化しています。
※ hanbaic.mdb を同梱しています。
※ 使用例です
特徴として、MDB は SHIFT_JIS で゜アクセスするので、その変換を
クラス内で自動的に行っています。
EUC_JP でプログラムを書いている場合は、行データの連想配列には
SHIFT_JIS と EUC_JP 両方のデータがセットされます。
db_mdb.php
<?
# **********************************************************
# MDB database class
# **********************************************************
class DB {
var $connect;
var $result;
var $fieldCount;
var $rowCount;
var $error;
var $cn;
var $rs;
var $connectionString;
# **********************************************************
#
# **********************************************************
function DB( $Server='default' ) {
$Server = $Server == 'default' ? $GLOBALS['conf_db_host'] : $Server;
$this->cn = new COM( "ADODB.Connection" );
$this->cn->CursorLocation = 3;
$this->rs = new COM( "ADODB.Recordset" );
$Server = $Server == '' ? realpath( "hanbaib.mdb" ) : $Server;
$this->connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;";
$this->connectionString .= "Data Source=$Server;";
try {
$this->cn->Open( $this->connectionString );
}
catch (Exception $e) {
$this->error =
mb_convert_encoding(
$e->getMessage(),
$GLOBALS['conf_client_charset'],
$GLOBALS['conf_db_charset']
);
$this->connect = false;
}
}
# **********************************************************
#
# **********************************************************
function Close( ) {
@$this->cn->Close();
}
# **********************************************************
#
# **********************************************************
function Query( $SqlQuery ) {
if ( $GLOBALS['conf_db_charset'] != '' ) {
$SqlQuery =
mb_convert_encoding(
$SqlQuery,
$GLOBALS['conf_db_charset'],
$GLOBALS['conf_client_charset']
);
}
try {
$this->rs->Open( $SqlQuery, $this->cn );
$ret = !$this->rs->EOF;
}
catch (Exception $e) {
$ret = false;
$this->error =
mb_convert_encoding(
$e->getMessage(),
$GLOBALS['conf_client_charset'],
$GLOBALS['conf_db_charset']
);
}
return $ret;
}
# **********************************************************
#
# **********************************************************
function Fetch( $result ) {
$ret = array();
for( $i = 0; $i < $this->rs->Fields->count; $i++ ) {
$ret[$i] = $this->rs->Fields[$i]->value;
$ret[$this->rs->Fields[$i]->name] = $this->rs->Fields[$i]->value;
}
if ( $GLOBALS['conf_db_charset'] != '' ) {
if ( $ret ) {
$ret2 = array();
foreach( $ret as $Key => $Value ) {
$ret2[$Key] =
mb_convert_encoding(
$Value,
$GLOBALS['conf_client_charset'],
$GLOBALS['conf_db_charset']
);
$Key2 =
mb_convert_encoding(
$Key,
$GLOBALS['conf_client_charset'],
$GLOBALS['conf_db_charset']
);
$ret2[$Key2] =
mb_convert_encoding(
$Value,
$GLOBALS['conf_client_charset'],
$GLOBALS['conf_db_charset']
);
}
}
}
else {
$ret2 = $ret;
}
return $ret2;
}
# **********************************************************
#
# **********************************************************
function FieldName( $idx ) {
$ret = $this->rs->Fields[$idx]->name;
if ( $GLOBALS['conf_db_charset'] != '' ) {
$ret =
mb_convert_encoding(
$ret,
$GLOBALS['conf_client_charset'],
$GLOBALS['conf_db_charset']
);
}
return $ret;
}
# **********************************************************
#
# **********************************************************
function QueryEx( $SqlQuery='' ) {
if ( $SqlQuery != '' ) {
$this->result = $this->Query( $SqlQuery );
if ( !$this->result ) {
return FALSE;
}
$this->fieldCount = $this->rs->Fields->count;
$this->rowCount = $this->rs->RecordCount;
}
else {
$this->rs->MoveNext();
if ( $this->rs->EOF ) {
return FALSE;
}
}
return $this->Fetch ( $this->result );
}
# **********************************************************
#
# **********************************************************
function Execute( $SqlExec ) {
if ( $GLOBALS['conf_db_charset'] != '' ) {
$SqlExec =
mb_convert_encoding(
$SqlExec,
$GLOBALS['conf_db_charset'],
$GLOBALS['conf_client_charset']
);
}
$ret = TRUE;
try {
$this->cn->Execute( $SqlExec );
}
catch (Exception $e) {
$ret = false;
$this->error =
mb_convert_encoding(
$e->getMessage(),
$GLOBALS['conf_client_charset'],
$GLOBALS['conf_db_charset']
);
}
return $ret;
}
}
?>