-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.php
More file actions
108 lines (87 loc) · 2.78 KB
/
db.php
File metadata and controls
108 lines (87 loc) · 2.78 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
/*
* DB Helper class. Uses PDO internally. Abstract, ie no relation to the app data. Maybe I should move more code into model layer...
*/
class DB{
private $pdo;
public $error;
function __construct($db, $host, $usr, $pass){
try {
$this->pdo = new PDO("mysql:dbname=$db;host=$host", $usr, $pass);
$this->error = false;
}
catch(PDOException $e){
$this->error = true;
}
}
//Update/Delete/Insert, return rows affected or FALSE incase of Db problem
function modify($sql, $data = NULL){
$stmt = $this->makeS($sql);
if($stmt->execute($data) === FALSE)
return FALSE;
return $stmt->rowCount();
}
//Read data, return as assoc array, or FALSE
function read($sql, $data = NULL){
$stmt = $this->makeS($sql);
if($stmt->execute($data) === FALSE)
return FALSE;
$data = array();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
$data[] = $row;
}
return $data;
}
//Build placeholder string ie (?, ?, ?), (?, ?, ?) for prepared statements.
function buildDataPH(&$data, $perRow = NULL){
$datalen = count($data);
$newData = array();
if($perRow == NULL)
$perRow = $datalen;
$sql = "";
$rows = $datalen/$perRow;
for($i = 0; $i < $rows; $i++){
$sql .= "(";
for($j = 0; $j < $perRow; $j++){
$currData = $data[$i*$perRow + $j];
if($currData == '__DEFAULT') {
$sql .= 'DEFAULT, ';
}
else{
$sql .= '?, ';
$newData[] = $currData;
}
}
$sql = substr($sql, 0, -2);
$sql .= "), ";
}
return array(substr($sql, 0, -2), $newData);
}
//Get data by field and/or value. Do not put external data in the add section, since they are not escaped!
function simpleRead($table, $val = NULL, $field = 'id', $returnfields = '*', $add = ''){
$sql = "SELECT $returnfields FROM $table WHERE 1 ";
$data = NULL;
if($val !== NULL){
$sql .= "AND $field = ? ";
$data[] = $val;
}
$sql .= $add;
return $this->read($sql, $data);
}
function simpleWrite($table, $data, $columns = NULL){
$sql = "INSERT INTO $table VALUES ";
$dt = $this->buildDataPH($data, $columns);
$sql .= $dt[0];
return $this->modify($sql, $dt[1]);
}
function lastInsertedId(){
return $this->pdo->lastInsertId();
}
function closeConnection(){
$this->pdo = NULL;
}
private function makeS($sql){
return $this->pdo->prepare($sql);
}
}
?>