Skip to content

Instantly share code, notes, and snippets.

@svenluijten
Created February 28, 2016 21:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save svenluijten/38a7fa1cae88f2c3e86e to your computer and use it in GitHub Desktop.
Save svenluijten/38a7fa1cae88f2c3e86e to your computer and use it in GitHub Desktop.
<?php
class Database {
// Properties:
private $host;
private $dbname;
private $user;
private $pass;
private $error;
private $stmt;
private $con;
// Methods:
public function __construct($host, $dbname, $user, $pass) {
$this->host = $host;
$this->dbname = $dbname;
$this->user = $user;
$this->pass = $pass;
try {
$this->con = new PDO("mysql:host=".$this->host.";dbname=".$this->dbname, $this->user, $this->pass);
$this->con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
$this->error = $e->getMessage();
return $this->error;
}
} // end "__construct".
# $query is the SQL query to send off to the database.
# $bind is the associative array with all the bound parameters
public function query($query, $bind = null) {
$this->stmt = $this->con->prepare($query);
if ($bind) {
$this->stmt->execute($bind);
} else {
$this->stmt->execute();
}
$check = explode(" ", $query);
if ($check[0] == "SELECT") {
$result = $this->stmt->fetchAll(PDO::FETCH_OBJ);
if (isset($result[0])) {
return $result;
} else {
return false;
}
} else {
return true;
}
} // end method "query".
} // end class.
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment