-
Notifications
You must be signed in to change notification settings - Fork 0
/
Database.php
51 lines (47 loc) · 1.27 KB
/
Database.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
class Database
{
/**
* Instance of PDO connection
* @var PDO
*/
private $connection;
/**
* Database constructor.
* @param $login
* @param $password
* @param $databaseName
* @param $host
*/
public function __construct($login, $password, $databaseName, $host)
{
$this->connection = new PDO("mysql:dbname=$databaseName;host=$host", $login, $password);
$this->connection->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->connection->query('SET NAMES utf8');
}
/**
* Function to make query to the Database with or without parameters
* @param $query
* @param bool $params
* @return PDOStatement
*/
public function query($query, $params = false)
{
if ($params) {
$res = $this->connection->prepare($query);
$res->execute($params);
} else {
$res = $this->connection->query($query);
}
return $res;
}
/**
* Function to return the last id inserted int the database
* @return string
*/
public function lastInsertId()
{
return $this->connection->lastInsertId();
}
}