-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_class.db.php
More file actions
69 lines (57 loc) · 1.68 KB
/
_class.db.php
File metadata and controls
69 lines (57 loc) · 1.68 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
<?php
class DB {
protected $dbh;
protected $dbn = '';
protected $user = '';
protected $pass = '';
public function __construct() {
try {
$this->dbh = new PDO('mysql:host=localhost;dbname='.$this->dbn, $this->user, $this->pass, array(
PDO::ATTR_PERSISTENT => true
));
} catch (PDOException $e) {
echo 'Error connecting to database: ' . $e->getMessage();
die();
}
}
public function __destruct() {
$this->dbh = null;
}
public function updateRow($table, $primary_key, $data) {
$params = array();
$update_cols = array();
foreach($data as $key => $value) {
$update_cols[] = $key.' = :'.$key;
$params[':'.$key] = $value;
}
$where_cols = array();
foreach($primary_key as $key => $value) {
$where_cols[] = $key.' = :'.$key;
$params[':'.$key] = $value;
}
$stmt = $this->dbh->prepare('UPDATE '.$table.' SET '.implode(', ', $update_cols).' WHERE '.implode(' AND ', $where_cols));
foreach($params as $key => &$value) {
$stmt->bindParam($key, $value);
}
$stmt->execute();
$err = $stmt->errorInfo();
if($DEV && count($err) > 1) {
return array('table' => $table, 'primary_key' => $primary_key, 'data' => $data, 'params' => $params, 'SQL' => 'UPDATE '.$table.' SET '.implode(', ', $update_cols).' WHERE '.implode(' AND ', $where_cols), 'error' => $err);
}
else {
return true;
}
}
public function insertRow($table, $data) {
$params = array();
$insert_cols = array();
foreach($data as $key => $value) {
$insert_cols[] = $key.' = :'.$key;
$params[':'.$key] = $value;
}
$stmt = $this->dbh->prepare('INSERT INTO '.$table.' SET '.implode(', ', $insert_cols));
$stmt->execute($params);
return $this->dbh->lastInsertId();
}
}
?>