oop中的错误(SQLSTATE [HY000]:常规错误)

时间:2018-09-09 22:49:38

标签: php pdo

当我在数据库中添加某些内容时,我开始出现此错误,我知道是什么问题,但是我试图解决它,但是我无法完成

数据库类的一部分:

public function query($sql, $params = array()) {
    $this->_error = FALSE;
    $this->_query = $this->_conn->prepare($sql);
    if ($this->_query) {
        if (count($params)) {
            $x = 1;
            foreach ($params as $param) {
                $this->_query->bindValue($x, $param);
                $x++;
            }
        }
        if ($this->_query->execute()) {
            $this->_result = $this->_query->fetchAll(PDO::FETCH_OBJ);
            $this->_count = $this->_query->rowCount();
        } else {
            return $this->_error = TRUE;
        }
    }
    return $this;
}
 public function insert($table, $fields = array()){
    $keys = array_keys($fields);
    $value = NULL;
    $x = 1;
    foreach ($fields as $field) {
        $value .= '?';
        if ($x < count($fields)) {
            $value .=' , ';
        }
        $x++;
    }
    $sql = 'INSERT INTO ' . $table . '(`' . implode('`,`', $keys) . '`) VALUES (' . $value . ')';

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

    return FALSE;
}

当我在数据库中插入数据时

try {
            $db->insert('users',  array('username' => $_POST['username'], 
                                        'password' => $pass_hash, 
                                        'Group' => 0));
        } catch (Exception $ex) {
            die($ex->getMessage());
        }

一切正常,代码将数据插入数据库,但是问题我得到了这个错误(SQLSTATE [HY000]:一般错误),因为我试图获取数据,但我不应该获取数据,但是我希望我的类更加动态,我该如何解决此问题?谢谢你

此行出现问题

$this->_result = $this->_query->fetchAll(PDO::FETCH_OBJ);

0 个答案:

没有答案
相关问题