Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 我的数据库包装器没有';不要让我使用fetchAll或任何其他PDO函数_Php_Oop_Pdo - Fatal编程技术网

Php 我的数据库包装器没有';不要让我使用fetchAll或任何其他PDO函数

Php 我的数据库包装器没有';不要让我使用fetchAll或任何其他PDO函数,php,oop,pdo,Php,Oop,Pdo,我正在尝试循环遍历一个结果集,并使用PDO和我使用教程制作的PHP数据库包装器打印出行的值。当我使用诸如fetchAll()之类的PDO函数时;或获取();我犯了致命的错误 我只是好奇我怎么能用我提供的代码做到这一点。我可以使用下面的代码轻松实现插入、数据更新等功能,但我很难弄清楚如何循环和打印。任何帮助都会令人惊讶的,谢谢 我需要的帮助是代码底部的getOffers函数 我有一个DB.php文件: class DB { private static $_instance = n

我正在尝试循环遍历一个结果集,并使用PDO和我使用教程制作的PHP数据库包装器打印出行的值。当我使用诸如fetchAll()之类的PDO函数时;或获取();我犯了致命的错误

我只是好奇我怎么能用我提供的代码做到这一点。我可以使用下面的代码轻松实现插入、数据更新等功能,但我很难弄清楚如何循环和打印。任何帮助都会令人惊讶的,谢谢

我需要的帮助是代码底部的getOffers函数

我有一个DB.php文件:

    class DB {
    private static $_instance = null;
    private $_pdo,
            $_query,
            $_error = false,
            $_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 {
                $this->_error = true;
            }
        }

        return $this;
    }

    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 = $sql = "{$action} FROM `{$table}` WHERE {$field} {$operator} '{$value}'";;
                if(!$this->query($sql, array($value))->error()) {
                    return $this;
                }
            }
        }
        return false;
    }

    public function get($table, $where) {
        return $this->action('SELECT *', $table, $where);
    }
    //deletes items from the database
    public function delete($table, $where) {
        return $this->action('DELETE', $table, $where);
    }

    public function insert($table, $fields = array()) {
        //check if fields has any data
        if(count($fields)) {
            $keys = array_keys($fields);
            $values = null;
            $x = 1;

            foreach($fields as $field) {
                $values .= "?";
                //check if x is less than the count of fields
                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) {
        $set = '';
        $x = 1;

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

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

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

    public function results() {
        return $this->_results;
    }

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

    }

    public function error() {
        return $this->_error;
    }

    public function count() {
        return $this->_count;
    }
}
classdb{
私有静态$\u实例=null;
私人$(pdo),
$\u查询,
$\u错误=false,
$\u结果,
$\计数=0;
私有函数_u构造(){
试一试{
$this->_pdo=new pdo('mysql:host='.Config::get('mysql/host')。;dbname='.Config::get('mysql/db')、Config::get('mysql/username')、Config::get('mysql/password');
}捕获(PDO$e){
死亡($e->getMessage());
}
}
公共静态函数getInstance(){
if(!isset(self:$\实例)){
self::$_instance=new DB();
}
返回self::$\u实例;
}
公共函数查询($sql,$params=array()){
$this->_error=false;
如果($this->_query=$this->_pdo->prepare($sql)){
$x=1;
如果(计数($params)){
foreach($params作为$param){
$this->_query->bindValue($x,$param);
$x++;
}
}
如果($this->\u query->execute()){
$this->\u results=$this->\u query->fetchAll(PDO::FETCH\u OBJ);
$this->_count=$this->_query->rowCount();
}否则{
$this->_error=true;
}
}
退还$this;
}
公共函数action($action,$table,$where=array()){
如果(计数($where)==3){

$operators=array(“=”、“>”、“=”、“似乎在Offer.php中应该是

$result = $offers->results();
不是


您会遇到哪些致命错误?致命错误:调用未定义的方法DB::fetchAll()。我已更新了上面的代码,以显示我在何处遇到此致命错误。谢谢。您需要添加fetchAll()函数。它目前只属于DB的私有$\u查询变量。强制:@aynber我应该把函数移到DB.php中吗?谢谢你的帮助。谢谢,我自己一开始也这么认为,我的\u results变量是私有的,所以当我试着从Offer.php访问它时,我会遇到一个错误。我应该把函数移到DB.php中吗?有问题吗再看一下你的代码,我已经更新了答案。虽然我会将变量
公开
。否则
返回false;
来自
操作
会在你期望DB类返回值时咬你的背。公开这些变量不会有安全隐患吗?理论上,编码错误的脚本可能会rrupt它自己以前的SQL调用的结果,但我更担心的是通过目前完全打开的
$where
参数数组进行SQL注入的可能性。尝试(或者更确切地说,不要)获取
get('offers',array('1=2;drop table offers;从dual where 1','=','2')
$result = $offers->results();
$result = $offers->fetchAll();