2 include_once( 'config.php' );
3 include_once( 'Settings.class.php' );
12 function __construct( $table, $property = null, $value = null ) {
13 $this->settings = Settings::factory();
15 $this->table = $this->settings->tbl_prefix.$table;
17 $this->dbh = @mysql_connect( $this->settings->db_host , $this->settings->db_user, $this->settings->db_pass );
18 if( $this->dbh === FALSE ) throw new exception( "construct:データベースに接続できない" );
20 $sqlstr = "use ".$this->settings->db_name;
21 $res = $this->__query($sqlstr);
22 if( $res === false ) throw new exception("construct: " . $sqlstr );
23 $sqlstr = "set NAMES utf8";
24 $res = $this->__query($sqlstr);
26 if( ($property == null) || ($value == null) ) {
27 // レコードを特定する要素が指定されない場合はid=0として空のオブジェクトを作成する
31 $sqlstr = "SELECT * FROM ".$this->table.
32 " WHERE ".mysql_real_escape_string( $property ).
33 "='".mysql_real_escape_string( $value )."'";
35 $res = $this->__query( $sqlstr );
36 $arr = mysql_fetch_array( $res , MYSQL_ASSOC );
37 if( $arr === FALSE ) throw new exception( "construct:無効な行" );
39 $this->id = $arr['id'];
45 function createTable( $tblstring ) {
46 $sqlstr = "use ".$this->settings->db_name;
47 $res = $this->__query($sqlstr);
48 if( $res === false ) throw new exception("createTable: " . $sqlstr );
49 $sqlstr = "CREATE TABLE IF NOT EXISTS ".$this->table." (" .$tblstring.") DEFAULT CHARACTER SET 'utf8'";
50 $result = $this->__query( $sqlstr );
51 if( $result === false ) throw new exception( "createTable:テーブル作成失敗" );
54 protected function __query( $sqlstr ) {
55 $res = @mysql_query( $sqlstr, $this->dbh );
56 if( $res === FALSE ) throw new exception( "__query:DBクエリ失敗:".$sqlstr );
60 function fetch_array( $property , $value, $options = null ) {
63 $sqlstr = "SELECT * FROM ".$this->table.
64 " WHERE ".mysql_real_escape_string( $property ).
65 "='".mysql_real_escape_string( $value )."'";
67 if( $options != null ) {
68 $sqlstr .= "AND ".$options;
70 $res = $this->__query( $sqlstr );
71 while ($row = mysql_fetch_array($res, MYSQL_ASSOC)) {
72 array_push( $retval, $row );
78 function __set( $property, $value ) {
79 if( $property == "id" ) throw new exception( "set:idの変更は不可" );
81 if( $this->id == 0 ) {
82 $sqlstr = "INSERT INTO ".$this->table." VALUES ( )";
83 $res = $this->__query( $sqlstr );
84 $this->id = mysql_insert_id();
86 $sqlstr = "UPDATE ".$this->table." SET ".
87 mysql_real_escape_string($property)."='".
88 mysql_real_escape_string($value)."' WHERE id='".$this->id."'";
89 $res = $this->__query( $sqlstr );
90 if( $res == FALSE ) throw new exception("set:セット失敗" );
93 function __get( $property ) {
94 if( $this->id == 0 ) throw new exception( "get:無効なid" );
95 if( $property == "id" ) return $this->id;
97 $sqlstr = "SELECT ".mysql_real_escape_string($property)." FROM ".$this->table." WHERE id='".$this->id."'";
98 $res = $this->__query($sqlstr);
99 $arr = mysql_fetch_row( $res );
100 if( $arr === FALSE ) throw new exception( "get:".$property."は存在しない" );
102 return stripslashes($arr[0]);
106 if( $this->id == 0 ) throw new exception( "delete:無効なid" );
108 $sqlstr = "DELETE FROM ".$this->table." WHERE id='".$this->id."'";
109 $this->__query( $sqlstr );
114 static function countRecords( $table, $options = "" ) {
116 $tbl = new self( $table );
117 $sqlstr = "SELECT COUNT(*) FROM " . $tbl->table ." " . $options;
118 $result = $tbl->__query( $sqlstr );
120 catch( Exception $e ) {
123 if( $result === false ) throw new exception("COUNT失敗");
124 $retval = mysql_fetch_row( $result );
128 // DBRecordオブジェクトを返すstaticなメソッド
129 static function createRecords( $table, $options = "" ) {
133 $tbl = new self( $table );
134 $sqlstr = "SELECT * FROM ".$tbl->table." " .$options;
135 $result = $tbl->__query( $sqlstr );
137 catch( Exception $e ) {
140 if( $result === false ) throw new exception("レコードが存在しません");
141 while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
142 array_push( $retval, new self( $table, 'id', $row['id'] ) );
147 function __destruct() {