-1

I have got a function for querying my database:

public function query($sql, $params = array()) {
    // reset error back to false
    $this->_error = false;

    // check query to prepare
    if($this->_query = $this->_pdo->prepare($sql)) {
        $x = 1;
        if(count($params)) {
            foreach($params as $param) {
                $this->_query->bindValue($x, $param);
                $x++;
            }
            print_r($this->_pdo->errorInfo());
        }
        // check query execution
        if ($this->_query->execute()) {
            $this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
            $this->_count = $this->_query->rowCount();
        } else {
            // error
            $this->_error = true;
        }
    }

    return $this;
}

But there is something not working. The line $this->_query->bindValue($x, $param); is not doing what it ought to do. There is no binding happening. The array with parameters thrown into the function seems fine. The $sql statement is fine as well and is returning:

INSERT INTO responders (`username`, `password`, `salt`) VALUES (?, ?, ?)

That should be perfect for using the bind method. But after going through the foreach loop nothing has changed and the _query var is still the same. So no entry to the DB will be made at all. I also tree to use the errorInfo and got back:

Array ( [0] => 00000 [1] => [2] => ) 

Can anyone give me a clue what I am missing here?

Adrian Cid Almaguer
  • 7,815
  • 13
  • 41
  • 63
konturgestaltung
  • 467
  • 6
  • 19

1 Answers1

-2

without reading all that messy code, here is how this function have to be

public function query($sql, $params = array()) {
    $query = $this->_pdo->prepare($sql);
    $query->execute($params);
    return $query;
}

And don't forget to set PDO in exceptions mode

Community
  • 1
  • 1
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • 2
    Well... I am sure, I am not the greatest PHP-coder. But have at least a sense of politeness. There is nothing wrong to ask questions if you are the one who wants to advance... – konturgestaltung Apr 27 '14 at 10:58