继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

Active Recode PHP

慕UI0519722
关注TA
已关注
手记 296
粉丝 85
获赞 267

     <?php  
    define('DBHOST', 'localhost');  
    define('DBUSER', 'root');  
    define('DBPASS', '');  
    define('DBNAME', 'test');  
    define('TABLE_PREFIX', 't_');  
    class ActiveRecord {  
        private $tablepre;  
        private $class;  
        private $table;  
        private static $link;  
        private $data;  
          
        public $primaryKey = 'id';  
          
        public function __construct() {  
            $this->tablepre = TABLE_PREFIX;  
            $this->class = get_class($this);   
            $this->table = $this->tablepre . strtolower($this->class);  
            $this->data = array();  
            $this->connect();  
        }  
          
        private function connect() {  
            if(!self::$link) {  
                self::$link = mysql_connect(DBHOST, DBUSER, DBPASS);  
                mysql_select_db(DBNAME);  
            }  
            return self::$link;  
        }  
          
        public function __set($name, $value) {  
            $this->data[$name] = $value;  
        }  
          
        private function implodefields($cond) {  //组合修改时调用  
            $fields = array();  
            foreach($cond as $key => $value) {  
                $value = mysql_real_escape_string($value);   
                $fields[] = "`$key`='$value'";  
            }  
            return implode(', ', $fields);       
        }  
          
        public function add() {               
            $fields = $this->implodefields($this->data);       
            $sql = "INSERT INTO `{$this->table}` SET $fields";  
            $this->query($sql);  
        }  
          
        public function findById($id) {  
            $sql = "SELECT * FROM `{$this->table}` WHERE `{$this->primaryKey}`='$id' LIMIT 1";  
            $data = $this->getOne($sql);  
            return $this->makeObjFromArray($data);  
        }  
          
        private function makeObjFromArray($data) {  
            $obj = new $this->class;  
            foreach($data as $key => $value) {     
                $obj->$key = $value;  
            }  
            return $obj;  
        }  
          
        private function query($sql) {  
            echo $sql . "\n";  
            return mysql_query($sql, self::$link);  
        }  
          
        private function getOne($sql) {  
            $data = $this->query($sql);  
            if($data) {  
                $item = mysql_fetch_assoc($data);  
                return $item;  
            }  
            return false;  
        }  
    }  
      
    class User extends ActiveRecord {  
        var $primaryKey = 'id';  
    }  
      
    $user = new User();  
    $user->name = '热电影';  
    $user->email = 'www.redianying.com';  
    $user->add();  
      
    $user = $user->findById(1);  
    print_r($user);  

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP