-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPdoDbFactory.php
57 lines (47 loc) · 1.64 KB
/
PdoDbFactory.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
<?php
class PdoDbFactory{
public static function getDbConnection() {
$dsn = "mysql:host=localhost;dbname=budget_db";
$opt = array(
// any occurring errors wil be thrown as PDOException
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
// an SQL command to execute when connecting
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'"
);
try {
$pdo = new PDO($dsn, 'root', '', $opt);
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
return $pdo;
}
public static function closeDbConnection($pdo) {
$pdo = null;
}
public static function queryDb($sql, $parameters = array(), $datatypes = array()) {
$dataObj = array();
try {
$pdo = self::getDbConnection();
$ps = $pdo->prepare($sql);
// Parameters and datatypes are bound by reference,
// so do not delete the 2 arrays until execute has happened.
foreach($parameters as $key=>$value) {
$ps->bindParam(':'.$key, $parameters[$key], $datatypes[$key]);
}
$ps->execute();
if ($ps->columnCount()) {
$dataObj = $ps->fetchAll(PDO::FETCH_OBJ);
} else {
if ($pdo->lastInsertId() > 0) {
$dataObj['newid'] = $pdo->lastInsertId();
}
}
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
return $dataObj;
}
}
?>