0

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.

Kamal Panhwar
  • 2,345
  • 3
  • 21
  • 37
  • 3
    Possible duplicate of [PDO error: " SQLSTATE\[HY000\]: General error " When updating database](http://stackoverflow.com/questions/12979510/pdo-error-sqlstatehy000-general-error-when-updating-database) – aynber Aug 08 '16 at 16:05
  • Please explain what you mean by duplicate, it is running one time only. – Kamal Panhwar Aug 08 '16 at 16:14
  • They are suggesting that this question has already been answered. Click on the link to view the question they think will solve your problem. – Henders Aug 08 '16 at 16:15
  • Don't bind values dynamically.See [**PDO info**](http://stackoverflow.com/tags/pdo/info). Use "lazy" binding when possible - passing data into execute will dramatically shorten your code. – david strachan Aug 08 '16 at 16:52
  • @KamalPanhwar Can you post your final code so we can see what you did to fix this problem? – kejo Dec 10 '20 at 02:08

1 Answers1

0

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.

Kamal Panhwar
  • 2,345
  • 3
  • 21
  • 37
  • can you post your final code so we can see what you did to correct this problem? – kejo Dec 10 '20 at 02:06
  • oh that is 4 years ago! I just removed thie line from above code so my final code would be without this on top codes. Sorry now using Ruby and my machine not setup with mysql/php stuff otherwise I will help you. – Kamal Panhwar Dec 10 '20 at 02:15