Part on Refactoring Section3 finished
This commit is contained in:
parent
594b8a64fa
commit
672ef6a949
5 changed files with 41 additions and 24 deletions
22
Database.php
22
Database.php
|
|
@ -2,6 +2,7 @@
|
|||
// connect to MySQL database.
|
||||
class Database {
|
||||
public $connection;
|
||||
public $statement;
|
||||
|
||||
public function __construct($config, $username, $password)
|
||||
{
|
||||
|
|
@ -9,8 +10,23 @@ class Database {
|
|||
$this->connection = new PDO($dsn, $username, $password, [PDO::ATTR_DEFAULT_FETCH_MODE=> PDO::FETCH_ASSOC]);
|
||||
}
|
||||
public function query($query, $params = []) {
|
||||
$statement = $this->connection->prepare($query);
|
||||
$statement->execute($params);
|
||||
return $statement;
|
||||
$this->statement = $this->connection->prepare($query);
|
||||
$this->statement->execute($params);
|
||||
return $this;
|
||||
}
|
||||
public function find() {
|
||||
return $this->statement->fetch();
|
||||
}
|
||||
public function findOrFail() {
|
||||
$result = $this->find();
|
||||
if (! $result) {
|
||||
abort();
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function get() {
|
||||
return $this->statement->fetchAll();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue