0

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...

1
  • What's the $this->sql_lause->execute(); Commented Jul 9, 2014 at 8:24

1 Answer 1

1

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().

Sign up to request clarification or add additional context in comments.

2 Comments

Didn't help. Now the query throws a fatal error "Fatal error: Call to a member function fetchAll() on a non-object" when trying to fetch results.
@Avaronald Do a var_dump($this->haku);

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.