使用PDO OOP选择一个或多个条件

时间:2018-02-06 20:50:09

标签: php pdo

我有以下方法从具有一个或多个条件的数据库中进行选择。但它没有得到任何结果,我在方法var_dump($this) return $this;之前加query并显示:

array(2) {
    ["program"]=> string(10) "INFANTOZOO"
    ["active"]=> string(4) "não"
}
array(2) {
    ["program"]=> string(10) "INFANTOZOO"
    ["active"]=> string(4) "não"
}
object(DB)#3 (5) {
    ["_pdo":"DB":private]=> object(PDO)#4 (0) { }
    ["_query":"DB":private]=> object(PDOStatement)#7 (1) {
        ["queryString"]=> string(53) "SELECT * FROM test WHERE program = ? AND active = ?"
    }
    ["_error":"DB":private]=> bool(false)
    ["_results":"DB":private]=> array(0) { }
    ["_count":"DB":private]=> int(0)
}

我不知道我在这里失踪了什么,有人可以帮助我吗?很抱歉,如果这是一个愚蠢的问题,我是PDO OOP的新人,但我真的很害怕!非常感谢你!

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

    $sql = "SELECT * FROM {$table} WHERE {$set}";
    if (!DB::query($sql, $fields)->error()) {
        return $this;
    }
    return false;
}   

-

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++; 
                var_dump($params);
            }
        } 


        if ($this->_query->execute()) {
            $this->_results = $this->_query->fetchALL(PDO::FETCH_OBJ);
            $this->_count = $this->_query->rowCount();

        } else{
            print_r($this->_query->errorInfo());
            $this->_error = true;
        }
    }
    var_dump($this);
    return $this;


}

这就是我调用方法的方法:

$est = DB::getInstance()->select('test', array('program' => 'INFANTOZOO', 'active' => 'não'));

0 个答案:

没有答案