Php 数据库类,OOP-连接到mysql

Php 数据库类,OOP-连接到mysql,php,mysql,database,pdo,bindparam,Php,Mysql,Database,Pdo,Bindparam,这是一个数据库类: DB.php <?php class DB { public static $instance = null; private $_pdo = null, $_query = null, $_error = false, $_results = null, $_c

这是一个数据库类:

DB.php

<?php
    class DB {
        public static $instance = null;

        private     $_pdo = null,
                    $_query = null,
                    $_error = false,
                    $_results = null,
                    $_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(PDOExeption $e) {
                die($e->getMessage());
            }
        }

        public static function getInstance() {
            // Already an instance of this? Return, if not, create.
            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 {
                    $this->_error = true;
                }
            }

            return $this;
        }

        public function get($table, $where) {
            return $this->action('SELECT *', $table, $where);
        }

        public function delete($table, $where) {
            return $this->action('DELETE', $table, $where);
        }

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

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

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

                    if(!$this->query($sql, array($value))->error()) {
                        return $this;
                    }

                }

                return false;
            }
        }

        public function insert($table, $fields = array()) {
            $keys   = array_keys($fields);
            $values = null;
            $x      = 1;

            foreach($fields as $value) {
                $values .= "?";
                if($x < count($fields)) {
                    $values .= ', ';
                }
                $x++;
            }

            $sql = "INSERT INTO {$table} (`" . implode('`, `', $keys) . "`) VALUES ({$values})";

            if(!$this->query($sql, $fields)->error()) {
                return true;
            }

            return false;
        }

        public function update($table, $id, $fields = array()) {
            $set    = null;
            $x      = 1;

            foreach($fields as $name => $value) {
                $set .= "{$name} = ?";
                if($x < count($fields)) {
                    $set .= ', ';
                }
                $x++;
            }

            $sql = "UPDATE users SET {$set} WHERE id = {$id}";

            if(!$this->query($sql, $fields)->error()) {
                return true;
            }

            return false;
        }

        public function results() {
            // Return result object
            return $this->_results;
        }

        public function first() {
            return $this->_results[0];
        }

        public function count() {
            // Return count
            return $this->_count;
        }

        public function error() {
            return $this->_error;
        }
    }
当我这样写的时候,分页就起作用了。但我需要一张搜索表。。。那是行不通的

$sql= DB::getInstance()->query(
    "SELECT * FROM users
     WHERE (category='admin')
     LIMIT " . (($page* 5)-5) . ",5");

看看这个类,
query
函数的第二个参数是一个可选的参数数组,因此使用它来传递请求的参数:

$params = array(':request' => 'lolcats');
$limit = $page - 1 * 50;
$query = sprintf(
   "SELECT * FROM users 
    WHERE concat(name, ' ',lastname, ' ', user_id) 
    LIKE :request 
    LIMIT %d,50",
    $limt
);
$results = DB::getInstance()->query($query, $params);

@保罗很接近,但你还有一个问题:

检查课程的这一部分:

$x = 1;
if(count($params)) {
   foreach($params as $param) {
       $this->_query->bindValue($x, $param);
       $x++;
    }
}
它不与命名的占位符绑定,您需要更改代码:

$limit = ($page * 50)-50;
$params = array('%lolcats%', $limit);
$query = 
   "SELECT * FROM users 
    WHERE concat(name, ' ',lastname, ' ', user_id) 
    LIKE ? 
    LIMIT ?,50";
$results = DB::getInstance()->query($query, $params);
或者将类代码更改为按占位符绑定,如下所示:

#$params = array(':request' =>'%lolcats%', ':limit'=>$limit);
if(count($params)) {
   foreach($params as $key=>$value) {
       $this->_query->bindValue($key, $value);
    }
}

当人们不熟悉OOP时,我在PHP中看到的第一件事就是在构造函数中创建一个PDO对象。将其作为参数注入构造函数,然后将其指定为
$this->pdo
。然后将特定于项目/数据库的逻辑保留在代码库之外,并保存在它所属的配置文件中。我得到以下错误:警告:PDOStatement::execute():SQLSTATE[HY093]:无效参数号:DB.php第52行中未定义参数我认为不能将limit用作绑定参数,因为它将被转义为IIRC,但是参数绑定不正确是一个好地方!
#$params = array(':request' =>'%lolcats%', ':limit'=>$limit);
if(count($params)) {
   foreach($params as $key=>$value) {
       $this->_query->bindValue($key, $value);
    }
}