Created
February 26, 2016 08:59
-
-
Save KCreate/74f45a438028f115aa7f to your computer and use it in GitHub Desktop.
EasyQuery
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment