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