【 PHP で使用するデータベースクラス 】

1. MySQL をターゲットとしたクラス
2. データベースクラスのテスト
3. SQL構文のエラーを表示する
4. 別テーブルの同時読込み
5. 読込みながらの更新
6. SQLServer をターゲットとしたクラス
7. MDB をターゲットとしたクラス
8. COM を使用した MDB の ADO 経由のアクセス
9. PostgreSQL をターゲットとしたクラス

  • PHPでは、MySQLデータベースバックエンドから返されたエラーを警告として表示しませんので
    mysql_errno または mysql_error を使用します
  • # **********************************************************
    # データベースクラス
    # **********************************************************
    class DB {
     
    	var $Connect;
    	var $Result;
     
    	var $Debug;
     
    # **********************************************************
    # コンストラクタ
    # **********************************************************
    	function DB( $Server='localhost', $DbName='lightbox', $User='root', $Password='' ) {
    		$this->Connect = mysql_connect( $Server, $User, $Password );
    		mysql_select_db( $DbName, $this->Connect );
    		$this->Debug = FALSE;
    	}
     
    # **********************************************************
    # 接続解除
    # **********************************************************
    	function Close( ) {
    		mysql_close( $this->Connect );
    	}
     
    # **********************************************************
    # クエリー
    # **********************************************************
    	function Query( $SqlQuery ) {
    		$ret = mysql_query( $SqlQuery,$this->Connect );
    		if ( $this->Debug ) {
    			if ( mysql_errno() != 0 ) {
    				print "<B>" . mysql_error() . "</B><BR>";
    			}
    		}
    		return $ret;
    	}
     
    # **********************************************************
    # フェッチ
    # **********************************************************
    	function Fetch( $Result ) {
    		return mysql_fetch_array( $Result );
    	}
     
    # **********************************************************
    # クエリーとフェッチ
    # **********************************************************
    	function QueryEx( $SqlQuery='' ) {
     
    		if ( $SqlQuery != '' ) {
    			$this->Result = $this->Query( $SqlQuery );
    			if ( !$this->Result ) {
    				return FALSE;
    			}
    			return $this->Fetch ( $this->Result );
    		}
    		else {
    			return $this->Fetch ( $this->Result );
    		}
     
    	}
     
    # **********************************************************
    # 実行
    # **********************************************************
    	function Execute( $SqlExec ) {
    		$ret = mysql_query( $SqlExec,$this->Connect );
    		if ( $this->Debug ) {
    			if ( mysql_errno() != 0 ) {
    				print "<B>" . mysql_error() . "</B><BR>";
    			}
    		}
    		return $ret;
    	}
     
    # **********************************************************
    # バージョン文字列取得
    # **********************************************************
    	function Version( ) {
    		$Field = $this->QueryEx( "show variables like 'version'" );
    		return $Field[1];
    	}
     
    }
    

    <?
    require_once( "./db.php" );
     
    # **********************************************************
    # データベースクラスのテスト
    # **********************************************************
    	$SQL = new DB( );
     
    	$Query = 'select * from 商品マスタ';
     
    	$Column = $SQL->QueryEx( $Query );
     
    	while( $Column ) {
    		print "{$Column['コード']} / {$Column['商品名']} / {$Column['単価']}<BR>\n";
     
    		$Column = $SQL->QueryEx( );
    	}
     
    	$SQL->Close( );
     
    ?>
    

  • 開発中に、SQL構文のエラーを表示したい場合は以下のように記述します
  • <?
    require_once( "./db.php" );
     
    # **********************************************************
    # データベースクラスのテスト
    # **********************************************************
    	$SQL = new DB( );
     
    	$SQL->Debug = TRUE;
     
    	$Query = 'select * from 商品マス';
     
    	$Column = $SQL->QueryEx( $Query );
     
    	while( $Column ) {
    		print "{$Column['コード']} / {$Column['商品名']} / {$Column['単価']}<BR>\n";
     
    		$Column = $SQL->QueryEx( );
    	}
     
    	$SQL->Close( );
     
    ?>
    

  • あるテーブルの読込み中に他のテーブルを読込む事は通常「結合」で処理しますが、もし
    そのような処理を行ないたい場合は以下のように記述します
  • <?
    require_once( "./db.php" );
     
    # **********************************************************
    # データベースクラスのテスト
    # **********************************************************
    	$SQL = new DB( );
     
    	$Query = 'select * from 商品マスタ';
     
    	$Column = $SQL->QueryEx( $Query );
     
    	while( $Column ) {
    		print "{$Column['コード']} / {$Column['商品名']} / {$Column['単価']}<BR>\n";
     
    		$Query = 'select * from 得意先マスタ';
    		$Result = $SQL->Query( $Query );
    		if ( $Result ) {
    			$Column2 = $SQL->Fetch( $Result );
    			if ( $Column2 ) {
    				print "{$Column2['名称']}<BR>";
    			}
    		}
     
    		$Column = $SQL->QueryEx( );
    	}
     
    	$SQL->Close( );
     
    ?>
    

  • 読込みながらの更新は以下のように行います

  • 当然ですが、このタイミングでは更新の内容は既に取得したレコードセットの結果には反映されません
  • <?
    require_once( "./db.php" );
     
    # **********************************************************
    # データベースクラスのテスト
    # **********************************************************
    	$SQL = new DB( );
     
    	$Query = 'select * from 商品マスタ';
     
    	$Column = $SQL->QueryEx( $Query );
     
    	while( $Column ) {
    		print "{$Column['コード']} / {$Column['商品名']} / {$Column['単価']}<BR>\n";
     
    		$Query = 'update 商品マスタ set 単価 = 単価 + 100 where コード = ';
    		$Query .= "'{$Column['コード']}'";
    		$SQL->Execute( $Query );
     
    		$Column = $SQL->QueryEx( );
    	}
     
    	$SQL->Close( );
     
    ?>
    

  • 基本的にはMySQLと同じで、mysql_mssql_ に変更します

  • php.ini の extension_dir エントリに正しいディレクトリを指定します
    ( 例 : extension_dir = "C:\php\extensions" )

  • php.ini の extension=php_mssql.dll をコメントから復帰させて有効にします

  • データベースのエラーは PHP が表示しますので、エラー処理は無くなっていますが、エラー表示を抑制
    したい場合は、error_reporting 関数を使用するか、set_error_handler 関数で、ユーザハンドラを作
    成します

  • テストでは、localhost が使用できなかったので、127.0.0.1 を使用しています

  • バージョン表示も RDBMS 依存となるので変更しています
  • <?
    # **********************************************************
    # データベースクラス
    # **********************************************************
    class DB {
     
    	var $Connect;
    	var $Result;
     
    # **********************************************************
    # コンストラクタ
    # **********************************************************
    	function DB( $Server='127.0.0.1', $DbName='lightbox', $User='sa', $Password='' ) {
    		$this->Connect = mssql_connect( $Server, $User, $Password );
    		mssql_select_db( $DbName, $this->Connect );
    	}
     
    # **********************************************************
    # 接続解除
    # **********************************************************
    	function Close( ) {
    		mssql_close( $this->Connect );
    	}
     
    # **********************************************************
    # クエリー
    # **********************************************************
    	function Query( $SqlQuery ) {
    		$ret = mssql_query( $SqlQuery,$this->Connect );
    		return $ret;
    	}
     
    # **********************************************************
    # フェッチ
    # **********************************************************
    	function Fetch( $Result ) {
    		return mssql_fetch_array( $Result );
    	}
     
    # **********************************************************
    # クエリーとフェッチ
    # **********************************************************
    	function QueryEx( $SqlQuery='' ) {
     
    		if ( $SqlQuery != '' ) {
    			$this->Result = $this->Query( $SqlQuery );
    			if ( !$this->Result ) {
    				return FALSE;
    			}
    			return $this->Fetch ( $this->Result );
    		}
    		else {
    			return $this->Fetch ( $this->Result );
    		}
     
    	}
     
    # **********************************************************
    # 実行
    # **********************************************************
    	function Execute( $SqlExec ) {
    		$ret = mssql_query( $SqlExec,$this->Connect );
    		return $ret;
    	}
     
    # **********************************************************
    # バージョン文字列取得
    # **********************************************************
    	function Version( ) {
    		$Field = $this->QueryEx( "sp_server_info @attribute_id = 2" );
    		return $Field["attribute_value"];
    	}
     
    }
    ?>
    

    <?
    # **********************************************************
    # データベースクラス
    # **********************************************************
    class DB {
     
    	var $Connect;
    	var $Result;
     
    # **********************************************************
    # コンストラクタ
    # **********************************************************
    	function DB( $Dsn='MDB' ) {
    		$this->Connect = odbc_connect( $Dsn, '', '' );
    	}
     
    # **********************************************************
    # 接続解除
    # **********************************************************
    	function Close( ) {
    		odbc_close( $this->Connect );
    	}
     
    # **********************************************************
    # クエリー
    # **********************************************************
    	function Query( $SqlQuery ) {
    		$ret = odbc_exec( $this->Connect, $SqlQuery );
    		return $ret;
    	}
     
    # **********************************************************
    # フェッチ
    # **********************************************************
    	function Fetch( $Result ) {
    		return odbc_fetch_array( $Result );
    	}
     
    # **********************************************************
    # クエリーとフェッチ
    # **********************************************************
    	function QueryEx( $SqlQuery='' ) {
     
    		if ( $SqlQuery != '' ) {
    			$this->Result = $this->Query( $SqlQuery );
    			if ( !$this->Result ) {
    				return FALSE;
    			}
    			return $this->Fetch ( $this->Result );
    		}
    		else {
    			return $this->Fetch ( $this->Result );
    		}
     
    	}
     
    # **********************************************************
    # 実行
    # **********************************************************
    	function Execute( $SqlExec ) {
    		$ret = odbc_exec( $this->Connect, $SqlExec );
    		return $ret;
    	}
     
    # **********************************************************
    # エラーメッセージ取得
    # **********************************************************
    	function GetError() {
    		return odbc_errormsg( $this->Connect );
    	}
     
    }
    ?>
    

  • ADO そのものがクラスなので、あえてラップする必要は無いかもしれません

  • 記述方法は、VBScript で記述する方法とほとんど同じです。

  • Release メソッドは、VBScript では使用しませんが、直ちにオブジェクトが解放されます

  • <?
    # **********************************************************
    # ADO のテスト
    # **********************************************************
    	$Cn = new COM( "ADODB.Connection" );
    	$Rs = new COM( "ADODB.Recordset" );
     
    	$ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;";
    	$ConnectionString .= "Data Source=D:\TEST\サンプル.mdb;";
     
    	$Cn->Open( $ConnectionString );
    	$Rs->Open( 'select * from 商品マスタ', $Cn );
     
     
    	while( !$Rs->EOF ) {
    		print $Rs->Fields['コード']->value . " / ";
    		print $Rs->Fields['商品名']->value . " / ";
    		print $Rs->Fields['単価']->value . "<BR>";
     
    		$Rs->MoveNext();
    	}
     
    	$Rs->Close();
    	$Cn->Close();
     
    	$Rs->Release();
    	$Cn->Release();
     
    ?>
     
    

  • MySQL や MSSQL と違うのは、接続の書き方と query の引数の順序です

  • 接続では、接続文字列を作成するようになっており、query では、先に接続のリソース
    を指定するようになっています

  • また、接続に対して SJIS エンコーディングにするようにしています
  • <?
    # **********************************************************
    # データベースクラス
    # **********************************************************
    class DB {
     
    	var $Connect;
    	var $Result;
     
    # **********************************************************
    # コンストラクタ
    # **********************************************************
    	function DB( $Server='localhost', $DbName='lightbox', $User='lightbox', $Password='' ) {
    		$this->Connect = pg_connect(
    			"host=$Server port=5432 dbname=$DbName user=$User password=$Password"
    		);
    		$this->QueryEx( "SET CLIENT_ENCODING TO 'SJIS'" );
    	}
     
    # **********************************************************
    # 接続解除
    # **********************************************************
    	function Close( ) {
    		pg_close( $this->Connect );
    	}
     
    # **********************************************************
    # クエリー
    # **********************************************************
    	function Query( $SqlQuery ) {
    		$ret = pg_query( $this->Connect, $SqlQuery );
    		return $ret;
    	}
     
    # **********************************************************
    # フェッチ
    # **********************************************************
    	function Fetch( $Result ) {
    		return pg_fetch_array( $Result );
    	}
     
    # **********************************************************
    # クエリーとフェッチ
    # **********************************************************
    	function QueryEx( $SqlQuery='' ) {
     
    		if ( $SqlQuery != '' ) {
    			$this->Result = $this->Query( $SqlQuery );
    			if ( !$this->Result ) {
    				return FALSE;
    			}
    			return $this->Fetch ( $this->Result );
    		}
    		else {
    			return $this->Fetch ( $this->Result );
    		}
     
    	}
     
    # **********************************************************
    # 実行
    # **********************************************************
    	function Execute( $SqlExec ) {
    		$ret = pg_query( $this->Connect, $SqlExec );
    		return $ret;
    	}
     
    # **********************************************************
    # バージョン文字列取得
    # **********************************************************
    	function Version( ) {
    		$Field = $this->QueryEx( "SHOW SERVER_VERSION" );
    		return $Field["server_version"];
    	}
     
    }
    ?>