Mercurial > epgrec.yaz
annotate DBRecord.class.php @ 37:e5f9aa34d06f
change: modify all script for web base setting
| author | yoneda <epgrec@park.mda.or.jp> |
|---|---|
| date | Tue, 28 Jul 2009 00:00:04 +0900 |
| parents | 01a8fdc0cebb |
| children | 8965ef108821 |
| rev | line source |
|---|---|
| 1 | 1 <?php |
| 2 include_once( 'config.php' ); | |
|
32
01a8fdc0cebb
testing: Web base setting page.
Sushi-k <epgrec@park.mda.or.jp>
parents:
17
diff
changeset
|
3 include_once( 'Settings.class.php' ); |
| 1 | 4 |
| 5 class DBRecord { | |
| 6 protected $table; | |
| 7 | |
| 8 protected $dbh; | |
| 9 public $id; | |
| 10 | |
| 11 function __construct( $table, $property = null, $value = null ) { | |
|
32
01a8fdc0cebb
testing: Web base setting page.
Sushi-k <epgrec@park.mda.or.jp>
parents:
17
diff
changeset
|
12 $settings = Settings::factory(); |
|
01a8fdc0cebb
testing: Web base setting page.
Sushi-k <epgrec@park.mda.or.jp>
parents:
17
diff
changeset
|
13 |
|
37
e5f9aa34d06f
change: modify all script for web base setting
yoneda <epgrec@park.mda.or.jp>
parents:
32
diff
changeset
|
14 $this->table = $settings->tbl_prefix.$table; |
| 1 | 15 |
|
32
01a8fdc0cebb
testing: Web base setting page.
Sushi-k <epgrec@park.mda.or.jp>
parents:
17
diff
changeset
|
16 $this->dbh = @mysql_connect( $settings->db_host , $settings->db_user, $settings->db_pass ); |
| 1 | 17 if( $this->dbh === FALSE ) throw new exception( "construct:データベースに接続できない" ); |
| 18 | |
|
32
01a8fdc0cebb
testing: Web base setting page.
Sushi-k <epgrec@park.mda.or.jp>
parents:
17
diff
changeset
|
19 $sqlstr = "use ".$settings->db_name; |
| 1 | 20 $res = $this->__query($sqlstr); |
| 21 if( $res === false ) throw new exception("construct: " . $sqlstr ); | |
| 22 $sqlstr = "set NAMES utf8"; | |
| 23 $res = $this->__query($sqlstr); | |
| 24 | |
| 25 if( ($property == null) || ($value == null) ) { | |
| 26 // レコードを特定する要素が指定されない場合はid=0として空のオブジェクトを作成する | |
| 27 $this->id = 0; | |
| 28 } | |
| 29 else { | |
| 30 $sqlstr = "SELECT * FROM ".$this->table. | |
| 31 " WHERE ".mysql_real_escape_string( $property ). | |
| 32 "='".mysql_real_escape_string( $value )."'"; | |
| 33 | |
| 34 $res = $this->__query( $sqlstr ); | |
| 35 $arr = mysql_fetch_array( $res , MYSQL_ASSOC ); | |
| 36 if( $arr === FALSE ) throw new exception( "construct:無効な行" ); | |
| 37 // 最初にヒットした行のidを使用する | |
| 38 $this->id = $arr['id']; | |
| 39 } | |
| 40 | |
| 41 return; | |
| 42 } | |
| 43 | |
| 44 function createTable( $tblstring ) { | |
|
32
01a8fdc0cebb
testing: Web base setting page.
Sushi-k <epgrec@park.mda.or.jp>
parents:
17
diff
changeset
|
45 $settings = Settings::factory(); |
|
01a8fdc0cebb
testing: Web base setting page.
Sushi-k <epgrec@park.mda.or.jp>
parents:
17
diff
changeset
|
46 |
|
01a8fdc0cebb
testing: Web base setting page.
Sushi-k <epgrec@park.mda.or.jp>
parents:
17
diff
changeset
|
47 $sqlstr = "use ".$settings->db_name; |
| 1 | 48 $res = $this->__query($sqlstr); |
| 49 if( $res === false ) throw new exception("createTable: " . $sqlstr ); | |
| 50 $sqlstr = "CREATE TABLE IF NOT EXISTS ".$this->table." (" .$tblstring.") DEFAULT CHARACTER SET 'utf8'"; | |
| 51 $result = $this->__query( $sqlstr ); | |
| 52 if( $result === false ) throw new exception( "createTable:テーブル作成失敗" ); | |
| 53 } | |
| 54 | |
| 55 protected function __query( $sqlstr ) { | |
| 56 $res = @mysql_query( $sqlstr, $this->dbh ); | |
| 57 if( $res === FALSE ) throw new exception( "__query:DBクエリ失敗:".$sqlstr ); | |
| 58 return $res; | |
| 59 } | |
| 60 | |
| 61 function fetch_array( $property , $value, $options = null ) { | |
| 62 $retval = array(); | |
| 63 | |
| 64 $sqlstr = "SELECT * FROM ".$this->table. | |
| 65 " WHERE ".mysql_real_escape_string( $property ). | |
|
17
d3ee3927eb3a
fix: fetch_array() in DBRecord.class.php
Sushi-k <epgrec@park.mda.or.jp>
parents:
1
diff
changeset
|
66 "='".mysql_real_escape_string( $value )."'"; |
| 1 | 67 |
| 68 if( $options != null ) { | |
|
17
d3ee3927eb3a
fix: fetch_array() in DBRecord.class.php
Sushi-k <epgrec@park.mda.or.jp>
parents:
1
diff
changeset
|
69 $sqlstr .= "AND ".$options; |
| 1 | 70 } |
| 71 $res = $this->__query( $sqlstr ); | |
| 72 while ($row = mysql_fetch_array($res, MYSQL_ASSOC)) { | |
| 73 array_push( $retval, $row ); | |
| 74 } | |
| 75 | |
|
17
d3ee3927eb3a
fix: fetch_array() in DBRecord.class.php
Sushi-k <epgrec@park.mda.or.jp>
parents:
1
diff
changeset
|
76 return $retval; |
| 1 | 77 } |
| 78 | |
| 79 function __set( $property, $value ) { | |
| 80 if( $property == "id" ) throw new exception( "set:idの変更は不可" ); | |
| 81 // id = 0なら空の新規レコード作成 | |
| 82 if( $this->id == 0 ) { | |
| 83 $sqlstr = "INSERT INTO ".$this->table." VALUES ( )"; | |
| 84 $res = $this->__query( $sqlstr ); | |
| 85 $this->id = mysql_insert_id(); | |
| 86 } | |
| 87 $sqlstr = "UPDATE ".$this->table." SET ". | |
| 88 mysql_real_escape_string($property)."='". | |
| 89 mysql_real_escape_string($value)."' WHERE id='".$this->id."'"; | |
| 90 $res = $this->__query( $sqlstr ); | |
| 91 if( $res == FALSE ) throw new exception("set:セット失敗" ); | |
| 92 } | |
| 93 | |
| 94 function __get( $property ) { | |
| 95 if( $this->id == 0 ) throw new exception( "get:無効なid" ); | |
| 96 if( $property == "id" ) return $this->id; | |
| 97 | |
| 98 $sqlstr = "SELECT ".mysql_real_escape_string($property)." FROM ".$this->table." WHERE id='".$this->id."'"; | |
| 99 $res = $this->__query($sqlstr); | |
| 100 $arr = mysql_fetch_row( $res ); | |
| 101 if( $arr === FALSE ) throw new exception( "get:".$property."は存在しない" ); | |
| 102 | |
| 103 return stripslashes($arr[0]); | |
| 104 } | |
| 105 | |
| 106 function delete() { | |
| 107 if( $this->id == 0 ) throw new exception( "delete:無効なid" ); | |
| 108 | |
| 109 $sqlstr = "DELETE FROM ".$this->table." WHERE id='".$this->id."'"; | |
| 110 $this->__query( $sqlstr ); | |
| 111 $this->id = 0; | |
| 112 } | |
| 113 | |
| 114 // countを実行する | |
| 115 static function countRecords( $table, $options = "" ) { | |
| 116 try{ | |
| 117 $tbl = new self( $table ); | |
| 118 $sqlstr = "SELECT COUNT(*) FROM " . $tbl->table ." " . $options; | |
| 119 $result = $tbl->__query( $sqlstr ); | |
| 120 } | |
| 121 catch( Exception $e ) { | |
| 122 throw $e; | |
| 123 } | |
| 124 if( $result === false ) throw new exception("COUNT失敗"); | |
| 125 $retval = mysql_fetch_row( $result ); | |
| 126 return $retval[0]; | |
| 127 } | |
| 128 | |
| 129 // DBRecordオブジェクトを返すstaticなメソッド | |
| 130 static function createRecords( $table, $options = "" ) { | |
| 131 $retval = array(); | |
| 132 $arr = array(); | |
| 133 try{ | |
| 134 $tbl = new self( $table ); | |
| 135 $sqlstr = "SELECT * FROM ".$tbl->table." " .$options; | |
| 136 $result = $tbl->__query( $sqlstr ); | |
| 137 } | |
| 138 catch( Exception $e ) { | |
| 139 throw $e; | |
| 140 } | |
| 141 if( $result === false ) throw new exception("レコードが存在しません"); | |
| 142 while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { | |
| 143 array_push( $retval, new self( $table, 'id', $row['id'] ) ); | |
| 144 } | |
| 145 return $retval; | |
| 146 } | |
| 147 | |
| 148 function __destruct() { | |
| 149 $this->id = 0; | |
| 150 } | |
| 151 } | |
|
32
01a8fdc0cebb
testing: Web base setting page.
Sushi-k <epgrec@park.mda.or.jp>
parents:
17
diff
changeset
|
152 ?> |
