简体   繁体   中英

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

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

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