简体   繁体   中英

Need help to find error SQLSTATE[HY000]: General error

I am inserting a row into the database. It is working perfectly but when I enable PDO error message, it give me this error. I want to know exactly why this error coming so I can keep error messaging enabled during development. Here is my code:

Enabling error I put in my DB class

$this->_pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

Database connection in my class

try {
    $this->_pdo = new PDO('mysql:host=' . Config::get('mysql/host') . ';dbname=' . Config::get('mysql/db'), config::get('mysql/username'), config::get('mysql/password'));
    $this->_prefix = '';
} catch (PDOException $e) {
    die($e->getMessage());
}

Here is the method where I call the query

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;
} 

This is the insert function in my DB class

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

    foreach ($fields as $field) {
        $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;
}

This is the function in my tour class which saves all data to DB by calling insert function.

public function create($fields= array()){
    if(!$this->_db->insert("tours", $fields)){
        throw new Exception('There was a problem creating Tours.' .print_r($this->_db->error()));
    }
} 

The reason to keep ATTR_ERRMODE enable is because it help me to debug during my development and I have still many pages to develop. I have already visited many similar questions but they are related to error. I don't normally get any errors but only when I enabled this error messaging system which tells me everything in detail.

This is different from other question as it is not giving me error but it insert row successfully but when I enable error mode that it give me error. Another I am using dynamic parameters so can't add colon : to rectify it. As same function used to query database.

My humble apologies to Aynber, as he was right I could not understand answer I have to remove fetchAll from my function. So I have created now two function one for insertion/updating and another for just query. As answer was there but I was seeing other answers.

I just removed following line from my query function

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

Thanks now my class is good as it is doing proper error handling.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM