mercredi 9 décembre 2015

SQLite vs MySQL ->rowCount()

I'm working on a CRUD class that shoulde ease my work. Now i'm trying to make it work with "SQlite" and "MySQL". The CRUD for MySQL works without problems, but ever since i'm trying to get data from the SQLite database it making my life hard :).

The code below is the working MySQL CRUD function for getting everything from a given table.

public function getAll($table){        
    $stmt = $this->db->prepare("SELECT * FROM $table");
    $stmt->execute();

    if($stmt->rowCount()){
        $item = $stmt->fetchAll(PDO::FETCH_ASSOC);
        return $item;
    }


    return false;
}

If i try the function from above on the SQLite database it doenst return anything.

However when i alter the code it does return the requested items. The function below works for the SQLite database but there is very litle of verification.

public function getAll($table){        
    $stmt = $this->db->prepare("SELECT * FROM $table");
    $stmt->execute();

    $items = $stmt->fetchAll(PDO::FETCH_ASSOC);
    return $items;

}

So my conclusion is that the rowCount() function doesn't work for SQLite. How can i solve this so that the SQLite CRUD first goes for a look if anything is truly returnd ? If its returnd , i can return the items. If the rows ar empty it should return false .

Hope you guys understand what i'm trying to tell.

Best regards

Aucun commentaire:

Enregistrer un commentaire