发布于 2015-01-04 01:28:58 | 129 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的PHP设计模式,程序狗速度看过来!

PHP开源脚本语言

PHP(外文名: Hypertext Preprocessor,中文名:“超文本预处理器”)是一种通用开源脚本语言。语法吸收了C语言、Java和Perl的特点,入门门槛较低,易于学习,使用广泛,主要适用于Web开发领域。PHP的文件后缀名为php。


本文是一个基于php和mysql的简单的dao类实现crud操作功能,感兴趣的同学参考下.


<?php
    //require_once('FirePHPCore/FirePHP.class.php');
    //$firephp = FirePHP::getInstance(true); // debugger in firefox
    class SimpleDao {
        private $_table = null;
        private static $_con = null;

        public function SimpleDao() {
            if ($this->_con == null) {
                $this->_con = @mysql_connect("localhost", "root", "123456");
                if ($this->_con == FALSE) {
                    echo("connect to db server failed.");
                    $this->_con = null;
                    return;
                }
                //$firephp->log("new DAO object");
                @mysql_select_db("swan", $this->_con);
            }
        }

        public function table($tablename) {
            $this->_table = $tablename;
            return $this;
        }

        public function query($sql) {
            $result = @mysql_query($sql);
            $ret = [];
            if ($result) {
                while ($row = mysql_fetch_array($result)) {
                    $ret[] = $row;
                }
            }
            return $ret;
        }

        public function get($where = null) {
            $sql = "select * from ".$this->_table;
            $sql = $sql.$this->_getWhereString($where);
            //echo "[get]".$sql."<br>";
            return $this->query($sql);
        }

        public function insert($params) {
            if ($params == null || !is_array($params)) {
                return -1;
            }
            $keys = $this->_getParamKeyString($params);
            $vals = $this->_getParamValString($params);
            $sql = "insert into ".$this->_table."(".$keys.") values(".$vals.")";
            //echo "[insert]".$sql."<br>";
            $result = @mysql_query($sql);
            if (! $result) {
                return -1;
            }
            return @mysql_insert_id();
        }

        public function update($params, $where = null) {
            if ($params == null || !is_array($params)) {
                return -1;
            }
            $upvals = $this->_getUpdateString($params);
            $wheres = $this->_getWhereString($where);
            $sql = "update ".$this->_table." set ".$upvals." ".$wheres;
            //echo "[update]".$sql."<br>";
            $result = @mysql_query($sql);
            if (! $result) {
                return -1;
            }
            return @mysql_affected_rows();
        }

        public function delete($where) {
            $wheres = $this->_getWhereString($where);
            $sql = "delete from ".$this->_table.$wheres;
            //echo "[delete]".$sql."<br>";
            $result = @mysql_query($sql);
            if (! $result) {
                return -1;
            }
            return @mysql_affected_rows();
        }

        protected function _getParamKeyString($params) {
            $keys = array_keys($params);
            return implode(",", $keys);
        }

        protected function _getParamValString($params) {
            $vals = array_values($params);
            return "'".implode("','", $vals)."'";
        }

        private function _getUpdateString($params) {
            //echo "_getUpdateString";
            $sql = "";
            if (is_array($params)) {
                $sql = $this->_getKeyValString($params, ",");
            }
            return $sql;
        }

        private function _getWhereString($params) {
            //echo "_getWhereString";
            $sql = "";
            if (is_array($params)) {
                $sql = " where ";
                $where = $this->_getKeyValString($params, " and ");
                $sql = $sql.$where;
            }
            return $sql;
        }

        private function _getKeyValString($params, $split) {
            $str = "";
            if (is_array($params)) {
                $paramArr = array();
                foreach($params as $key=>$val) {
                    $valstr = $val;
                    if (is_string($val)) {
                        $valstr = "'".$val."'";
                    }
                    $paramArr[] = $key."=".$valstr;
                }
                $str = $str.implode($split, $paramArr);
            }
            return $str;
        }

        public function release() {
            @mysql_close();
        }
    }

    function T($table) {
        return (new SimpleDao())->table($table);
    }
?>



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务