Mercurial > epgrec.yaz
view DBRecord.class.php @ 125:ef3df87f3c7d
mod: チャンネルスキップの大まかな実装終了
author | Sushi-k <epgrec@park.mda.or.jp> |
---|---|
date | Wed, 17 Mar 2010 18:12:01 +0900 |
parents | 8965ef108821 |
children | 49145003e6a3 |
line wrap: on
line source
<?php include_once( 'config.php' ); include_once( 'Settings.class.php' ); class DBRecord { protected $table; protected $settings; protected $dbh; public $id; function __construct( $table, $property = null, $value = null ) { $this->settings = Settings::factory(); $this->table = $this->settings->tbl_prefix.$table; $this->dbh = @mysql_connect( $this->settings->db_host , $this->settings->db_user, $this->settings->db_pass ); if( $this->dbh === FALSE ) throw new exception( "construct:若帥若鴻・膓с" ); $sqlstr = "use ".$this->settings->db_name; $res = $this->__query($sqlstr); if( $res === false ) throw new exception("construct: " . $sqlstr ); $sqlstr = "set NAMES utf8"; $res = $this->__query($sqlstr); if( ($property == null) || ($value == null) ) { // 潟若劫荀膣絎翫id=0腥冴吾с篏 $this->id = 0; } else { $sqlstr = "SELECT * FROM ".$this->table. " WHERE ".mysql_real_escape_string( $property ). "='".mysql_real_escape_string( $value )."'"; $res = $this->__query( $sqlstr ); $arr = mysql_fetch_array( $res , MYSQL_ASSOC ); if( $arr === FALSE ) throw new exception( "construct:≦鴻茵" ); // 茵id篏睡 $this->id = $arr['id']; } return; } function createTable( $tblstring ) { $sqlstr = "use ".$this->settings->db_name; $res = $this->__query($sqlstr); if( $res === false ) throw new exception("createTable: " . $sqlstr ); $sqlstr = "CREATE TABLE IF NOT EXISTS ".$this->table." (" .$tblstring.") DEFAULT CHARACTER SET 'utf8'"; $result = $this->__query( $sqlstr ); if( $result === false ) throw new exception( "createTable:若篏紊掩" ); } protected function __query( $sqlstr ) { $res = @mysql_query( $sqlstr, $this->dbh ); if( $res === FALSE ) throw new exception( "__query:DB紊掩:".$sqlstr ); return $res; } function fetch_array( $property , $value, $options = null ) { $retval = array(); $sqlstr = "SELECT * FROM ".$this->table. " WHERE ".mysql_real_escape_string( $property ). "='".mysql_real_escape_string( $value )."'"; if( $options != null ) { $sqlstr .= "AND ".$options; } $res = $this->__query( $sqlstr ); while ($row = mysql_fetch_array($res, MYSQL_ASSOC)) { array_push( $retval, $row ); } return $retval; } function __set( $property, $value ) { if( $property == "id" ) throw new exception( "set:id紊眼筝" ); // id = 0腥冴域潟若篏 if( $this->id == 0 ) { $sqlstr = "INSERT INTO ".$this->table." VALUES ( )"; $res = $this->__query( $sqlstr ); $this->id = mysql_insert_id(); } $sqlstr = "UPDATE ".$this->table." SET ". mysql_real_escape_string($property)."='". mysql_real_escape_string($value)."' WHERE id='".$this->id."'"; $res = $this->__query( $sqlstr ); if( $res == FALSE ) throw new exception("set:祉紊掩" ); } function __get( $property ) { if( $this->id == 0 ) throw new exception( "get:≦鴻id" ); if( $property == "id" ) return $this->id; $sqlstr = "SELECT ".mysql_real_escape_string($property)." FROM ".$this->table." WHERE id='".$this->id."'"; $res = $this->__query($sqlstr); $arr = mysql_fetch_row( $res ); if( $arr === FALSE ) throw new exception( "get:".$property."絖" ); return stripslashes($arr[0]); } function delete() { if( $this->id == 0 ) throw new exception( "delete:≦鴻id" ); $sqlstr = "DELETE FROM ".$this->table." WHERE id='".$this->id."'"; $this->__query( $sqlstr ); $this->id = 0; } // count絎茵 static function countRecords( $table, $options = "" ) { try{ $tbl = new self( $table ); $sqlstr = "SELECT COUNT(*) FROM " . $tbl->table ." " . $options; $result = $tbl->__query( $sqlstr ); } catch( Exception $e ) { throw $e; } if( $result === false ) throw new exception("COUNT紊掩"); $retval = mysql_fetch_row( $result ); return $retval[0]; } // DBRecord吾с菴static<純 static function createRecords( $table, $options = "" ) { $retval = array(); $arr = array(); try{ $tbl = new self( $table ); $sqlstr = "SELECT * FROM ".$tbl->table." " .$options; $result = $tbl->__query( $sqlstr ); } catch( Exception $e ) { throw $e; } if( $result === false ) throw new exception("潟若絖障"); while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { array_push( $retval, new self( $table, 'id', $row['id'] ) ); } return $retval; } function __destruct() { $this->id = 0; } } ?>