-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPersistanceManager.php
59 lines (51 loc) · 1.81 KB
/
PersistanceManager.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
52
53
54
55
56
57
58
59
<?php
class PersistanceManager {
private $pdo;
private $dbHost = 'localhost';
private $dbUserName = 'root';
private $dbPassword = 'root';
private $dbName = 'uor_pms';
public function __construct() {
try {
$this->pdo = new PDO("mysql:host=$this->dbHost;dbname=$this->dbName", $this->dbUserName, $this->dbPassword);
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
} catch (PDOException $e) {
echo $e->getMessage();
}
}
public function run($query, $param = null, $fetchFirstRecOnly = false) {
$result = $this->executeQuery($query, $param, $fetchFirstRecOnly);
return $result;
}
public function insertAndGetLastRowId($query, $param = null) {
$result = $this->executeQuery($query, $param, true, true);
return $result;
}
private function executeQuery($query, $param = null, $fetchFirstRecOnly = false, $getLastInsertedId = false) {
try {
$stmt = $this->pdo->prepare($query);
$t = $stmt->execute($param);
if ($getLastInsertedId) {
return $this->pdo->lastInsertId();
}
if (strtoupper(substr(trim($query), 0, 6)) == "SELECT") {
if ($fetchFirstRecOnly)
$result = $stmt->fetch(PDO::FETCH_ASSOC);
else
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
$stmt->closeCursor();
return $result;
}
$stmt->closeCursor();
if ($t) {
return 1;
} else {
return 0;
}
} catch (PDOException $e) {
echo $e->getMessage();
}
}
}
?>