PDO准备类DB

时间:2015-03-10 13:38:41

标签: php mysql pdo

我有一个类'DB',我从在线教程中获取。它有一些SELECT查询但不是INSERT。

到目前为止它的效果非常好,但我想进一步开发以供将来使用。

class DB {
    private static $_instance = null;
    private $_pdo,
                    $_query,
                    $_error = false,
                    $_errorMsg,
                    $_results,
                    $count = 0;

    private function __construct(){
            try{
                    $this->_pdo = new PDO('mysql:host=' . Config::get('mysql/host') . ';dbname=' . Config::get('mysql/db'), Config::get('mysql/username'), Config::get('mysql/password'));

            }catch(PDOException $e){
                    die($e->getMessage());
            }
    }

    public static function getInstance() {
            if(!isset(self::$_instance)) {
                    self::$_instance = new DB();
            }
            return self::$_instance;
    }

    public function query($sql, $params = array()) {
            $this->_error = false;
            if($this->_query = $this->_pdo->prepare($sql)) {
                    $x = 1;
                    if(count($params)) {
                            foreach($params as $param){
                                    $this->_query->bindValue($x, $param);
                                    $x++;
                            }
                    }

                    if($this->_query->execute()) {
                            $this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
                            $this->_count = $this->_query->rowCount();
                    } else {
                            // return error & write to log
                            $this->_error = true;
                            //get Error message and save to log
                            $errormsg = $this->_query->errorInfo();
                            $message = date('l jS \of F Y h:i:s A') . "\n";
                            $message .= 'Invalid query: ' . $errormsg[2] . "\n";
                            $message .= 'Whole query: ' . $sql . "\n";
                            $message .= '------------------------------------------------------' . "\n";
                            $this->createLog($message);

                            $this->_errorMsg = $errormsg[2];
                    }
            }

            return $this;
    }

    public function action($action, $table, $where = array(), $and = array()) {
            if(count($where) === 3){
                    $operators = array('=', '>', '<', '>=', '<=');

                    $field          = $where[0];
                    $operator       = $where[1];
                    $value          = $where[2];

                    // Check if AND is set
                    $and = array_filter($and);
                    if ( !empty($and) && $and != '' ) {
                        $andOR              = $and[0];
                        $andField           = $and[1];
                        $andFieldOperator   = $and[2];
                        $andValue           = $and[3];

                        if(in_array($operator, $operators)) {
                            $sql = "{$action} FROM {$table} WHERE {$field} {$operator} ? {$andOR} `{$andField}` {$andFieldOperator} '{$andValue}'";

                            if( !$this->query($sql, array($value) )->error()) {
                                //print_r( $this->query($sql, array($value)));
                                return $this;
                            } else {
                                print_r('ERROR: Check error log - <a href="'.config::get('url/baseurl') . 'log_error.txt'.'">Click here</a>');

                            }
                        }

                    } else {
                        if(in_array($operator, $operators)) {
                            $sql = "{$action} FROM {$table} WHERE {$field} {$operator} ?";

                            if( !$this->query($sql, array($value) )->error()) {
                                    return $this;
                            } else {
                                print_r('ERROR: Check error log - <a href="'.config::get('url/baseurl') . 'log_error.txt'.'">Click here</a>');
                            }
                        }
                    }



            } else {
                $sql = "{$action} FROM {$table}";
                if( !$this->query($sql )->error()) {
                        return $this;
                }
            }
            return false;
    }

如何使用PDO prepare添加一个“INSERT”函数来将数据添加到我的数据库?

0 个答案:

没有答案