Mercurial > epgrec.yaz
annotate DBRecord.class.php @ 164:254d8a21bf36
fix: 予約済みの未実行録画にrecomplete.phpが必要なので戻した
author | Sushi-k <epgrec@park.mda.or.jp> |
---|---|
date | Tue, 06 Apr 2010 11:03:45 +0900 |
parents | 8965ef108821 |
children | 49145003e6a3 |
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; | |
39
8965ef108821
change: modify all scripts for web base setting.
Sushi-k <epgrec@park.mda.or.jp>
parents:
37
diff
changeset
|
7 protected $settings; |
1 | 8 |
9 protected $dbh; | |
10 public $id; | |
11 | |
12 function __construct( $table, $property = null, $value = null ) { | |
39
8965ef108821
change: modify all scripts for web base setting.
Sushi-k <epgrec@park.mda.or.jp>
parents:
37
diff
changeset
|
13 $this->settings = Settings::factory(); |
32
01a8fdc0cebb
testing: Web base setting page.
Sushi-k <epgrec@park.mda.or.jp>
parents:
17
diff
changeset
|
14 |
39
8965ef108821
change: modify all scripts for web base setting.
Sushi-k <epgrec@park.mda.or.jp>
parents:
37
diff
changeset
|
15 $this->table = $this->settings->tbl_prefix.$table; |
1 | 16 |
39
8965ef108821
change: modify all scripts for web base setting.
Sushi-k <epgrec@park.mda.or.jp>
parents:
37
diff
changeset
|
17 $this->dbh = @mysql_connect( $this->settings->db_host , $this->settings->db_user, $this->settings->db_pass ); |
1 | 18 if( $this->dbh === FALSE ) throw new exception( "construct:若帥若鴻・膓с" ); |
19 | |
39
8965ef108821
change: modify all scripts for web base setting.
Sushi-k <epgrec@park.mda.or.jp>
parents:
37
diff
changeset
|
20 $sqlstr = "use ".$this->settings->db_name; |
1 | 21 $res = $this->__query($sqlstr); |
22 if( $res === false ) throw new exception("construct: " . $sqlstr ); | |
23 $sqlstr = "set NAMES utf8"; | |
24 $res = $this->__query($sqlstr); | |
25 | |
26 if( ($property == null) || ($value == null) ) { | |
27 // 潟若劫荀膣絎翫id=0腥冴吾с篏 | |
28 $this->id = 0; | |
29 } | |
30 else { | |
31 $sqlstr = "SELECT * FROM ".$this->table. | |
32 " WHERE ".mysql_real_escape_string( $property ). | |
33 "='".mysql_real_escape_string( $value )."'"; | |
34 | |
35 $res = $this->__query( $sqlstr ); | |
36 $arr = mysql_fetch_array( $res , MYSQL_ASSOC ); | |
37 if( $arr === FALSE ) throw new exception( "construct:≦鴻茵" ); | |
38 // 茵id篏睡 | |
39 $this->id = $arr['id']; | |
40 } | |
41 | |
42 return; | |
43 } | |
44 | |
45 function createTable( $tblstring ) { | |
39
8965ef108821
change: modify all scripts for web base setting.
Sushi-k <epgrec@park.mda.or.jp>
parents:
37
diff
changeset
|
46 $sqlstr = "use ".$this->settings->db_name; |
1 | 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:若篏紊掩" ); | |
52 } | |
53 | |
54 protected function __query( $sqlstr ) { | |
55 $res = @mysql_query( $sqlstr, $this->dbh ); | |
56 if( $res === FALSE ) throw new exception( "__query:DB紊掩:".$sqlstr ); | |
57 return $res; | |
58 } | |
59 | |
60 function fetch_array( $property , $value, $options = null ) { | |
61 $retval = array(); | |
62 | |
63 $sqlstr = "SELECT * FROM ".$this->table. | |
64 " 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
|
65 "='".mysql_real_escape_string( $value )."'"; |
1 | 66 |
67 if( $options != null ) { | |
17
d3ee3927eb3a
fix: fetch_array() in DBRecord.class.php
Sushi-k <epgrec@park.mda.or.jp>
parents:
1
diff
changeset
|
68 $sqlstr .= "AND ".$options; |
1 | 69 } |
70 $res = $this->__query( $sqlstr ); | |
71 while ($row = mysql_fetch_array($res, MYSQL_ASSOC)) { | |
72 array_push( $retval, $row ); | |
73 } | |
74 | |
17
d3ee3927eb3a
fix: fetch_array() in DBRecord.class.php
Sushi-k <epgrec@park.mda.or.jp>
parents:
1
diff
changeset
|
75 return $retval; |
1 | 76 } |
77 | |
78 function __set( $property, $value ) { | |
79 if( $property == "id" ) throw new exception( "set:id紊眼筝" ); | |
80 // id = 0腥冴域潟若篏 | |
81 if( $this->id == 0 ) { | |
82 $sqlstr = "INSERT INTO ".$this->table." VALUES ( )"; | |
83 $res = $this->__query( $sqlstr ); | |
84 $this->id = mysql_insert_id(); | |
85 } | |
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:祉紊掩" ); | |
91 } | |
92 | |
93 function __get( $property ) { | |
94 if( $this->id == 0 ) throw new exception( "get:≦鴻id" ); | |
95 if( $property == "id" ) return $this->id; | |
96 | |
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."絖" ); | |
101 | |
102 return stripslashes($arr[0]); | |
103 } | |
104 | |
105 function delete() { | |
106 if( $this->id == 0 ) throw new exception( "delete:≦鴻id" ); | |
107 | |
108 $sqlstr = "DELETE FROM ".$this->table." WHERE id='".$this->id."'"; | |
109 $this->__query( $sqlstr ); | |
110 $this->id = 0; | |
111 } | |
112 | |
113 // count絎茵 | |
114 static function countRecords( $table, $options = "" ) { | |
115 try{ | |
116 $tbl = new self( $table ); | |
117 $sqlstr = "SELECT COUNT(*) FROM " . $tbl->table ." " . $options; | |
118 $result = $tbl->__query( $sqlstr ); | |
119 } | |
120 catch( Exception $e ) { | |
121 throw $e; | |
122 } | |
123 if( $result === false ) throw new exception("COUNT紊掩"); | |
124 $retval = mysql_fetch_row( $result ); | |
125 return $retval[0]; | |
126 } | |
127 | |
128 // DBRecord吾с菴static<純 | |
129 static function createRecords( $table, $options = "" ) { | |
130 $retval = array(); | |
131 $arr = array(); | |
132 try{ | |
133 $tbl = new self( $table ); | |
134 $sqlstr = "SELECT * FROM ".$tbl->table." " .$options; | |
135 $result = $tbl->__query( $sqlstr ); | |
136 } | |
137 catch( Exception $e ) { | |
138 throw $e; | |
139 } | |
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'] ) ); | |
143 } | |
144 return $retval; | |
145 } | |
146 | |
147 function __destruct() { | |
148 $this->id = 0; | |
149 } | |
150 } | |
32
01a8fdc0cebb
testing: Web base setting page.
Sushi-k <epgrec@park.mda.or.jp>
parents:
17
diff
changeset
|
151 ?> |