SecureCloud-biz
6/9/2017 - 3:25 PM

EasyQuery

EasyQuery

// Easier sql queries
class EasyQuery {
    private $dbname;
    private $db;

    public function __construct($dbname) {
        $this->dbname = $dbname;

        // Check if the db file exists
        if (!file_exists($this->dbname)) {
            return false;
        }

        // Try to load the db
        try {
            $this->db = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb, *.accdb)}; DBQ=".$this->dbname."; UID=Admin;");
        } catch (Exception $e) {
            echo $e;
            return false;
        }

        return $this;
    }

    public function r($sql, $values) {
        // Keep track of all results
        $results = (object)[
            'status' => false,
            'rows' => []
        ];

        // Prepare and execute the query
        $query = $this->db->prepare($sql);
        $results->status = $query->execute($values);

        // Copy all the returned rows to the result object
        while ($row = $query->fetch()) {
            array_push($results->rows, $row);
        }
        return $results;
    }
}