Avaronald
Avaronald

Reputation: 25

PHP PDO queries not working

I have a dbfunctions class that has the functions to retrieve and write data to database. However, the retrieval doesn't work and I can't understand why it doesn't work.

The read functions looks like this:

public function haeParametreittaTietokannasta($f_sql) {
    try {
        $this->sql_lause = $this->db->prepare($f_sql);
        $this->haku = $this->sql_lause->execute();
    } catch (PDOException $ex) {
        die("Tapahtui virhe tietoja noudettaessa : " . $ex->getMessage());
    }
    if(!$this->haku) {
        $this->tulos = $this->haku->fetchAll();
        return $this->tulos;
    } else {
        return false;
    }
} // END FUNCTION

And this is how I call the function:

$countries = $dabase->haeParametreittaTietokannasta("SELECT * FROM countries");

The query always returns false, and I've tried showing error info and it says: array ( 0 => '00000', 1 => NULL, 2 => NULL, ) (And yes, I have created a new object in the main code.)

I've just started to learn PHP and there might be a simple error I just can't see...

Upvotes: 0

Views: 50

Answers (1)

hjpotter92
hjpotter92

Reputation: 80657

You have the wrong if condition:

if(!$this->haku) {

should be

if($this->haku) {
    $this->tulos = $this->sql_lause->fetchAll();

PDOStatement::execute() returns a boolean value. You can't use it for fetchAll().

Upvotes: 1

Related Questions