1

We have this class for database. Calling the query function works fine if the query string is SELECT.. but it doesn't wotk for UPDATE... queries.

The issue is $stmt->fetchAll();, it can't fetchAll on UPDATE... and it make sense.

How can I tell if the query can fetchAllable or not.

class DB 
{
    public $db = null;
    public function __construct() { 
        try {
            $this->db = new PDO('mysql:host=' . DBHOST . ';dbname=' . DBNAME, DBUSER, DBPWORD);
            $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $this->db->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, 1);
        } catch(PDOException $e) {

        }
    }

    public function query( $query = "" , $params = [] ) {
        $stmt = $this->db->prepare( $query );

        foreach( $params as $param ) {
            $stmt->bindParam($param[0], $param[1]);
        }

        $stmt->execute();

        $records = $stmt->fetchAll(); //How can I tell if I can do this.
        return $records;

        return true;
    }
}

Thanks for the help.

Miro
  • 225
  • 2
  • 10
  • That's a very good question actually. And a very good function as well. I would strongly recommend to make this function return `$stmt` instead of fetchAll(). You'd be amazed how much more flexible this function will become! Let alone it will solve your current problem as well. Please refer to the examples section my article, [Simple yet efficient PDO wrapper](https://phpdelusions.net/pdo/pdo_wrapper#function) and see how much different uses this function can have if only it returns $stmt – Your Common Sense Oct 23 '19 at 08:20

0 Answers0