Created
February 28, 2016 21:24
-
-
Save svenluijten/38a7fa1cae88f2c3e86e to your computer and use it in GitHub Desktop.
This file contains 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
<?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