Repository navigation
Expand file tree
/
Copy pathDatabase.php
More file actions
79 lines (70 loc) · 1.9 KB
/
Copy pathDatabase.php
File metadata and controls
79 lines (70 loc) · 1.9 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
class Database
{
private $dbHost = DB_HOST;
private $dbUser = DB_USER;
private $dbPasw = DB_PASW;
private $dbName = DB_NAME;
private $statement;
private $dbHandler;
private $error;
public function __construct()
{
$conn = 'mysql:host=' . $this->dbHost . ';dbname=' . $this->dbName;
$options = array(
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
try {
$this->dbHandler = new PDO($conn, $this->dbUser, $this->dbPasw, $options);
} catch (PDOException $e) {
$this->error = $e->getMessage();
echo $this->error;
}
}
// Allows to create Querys
public function query($sql)
{
$this->statement = $this->dbHandler->prepare($sql);
}
// Bind values
public function bind($parameter, $value, $type = null)
{
switch (is_null($type)) {
case is_int($value):
$type = PDO::PARAM_INT;
break;
case is_bool($value):
$type = PDO::PARAM_BOOL;
break;
case is_null($value):
$type = PDO::PARAM_NULL;
break;
default:
$type = PDO::PARAM_STR;
}
$this->statement->bindValue($parameter, $value, $type);
}
// Execute the prepared statement
public function execute()
{
return $this->statement->execute();
}
// Return array
public function resultSet()
{
$this->execute();
return $this->statement->fetchAll(PDO::FETCH_OBJ);
}
// Return a spicific row as an object
public function single()
{
$this->execute();
return $this->statement->fetch(PDO::FETCH_OBJ);
}
//Get's the row count
public function rowCount()
{
return $this->statement->rowCount();
}
}