-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb_oop.php
More file actions
64 lines (50 loc) · 1.54 KB
/
db_oop.php
File metadata and controls
64 lines (50 loc) · 1.54 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
<?php
class DbWrapper {
private $connection;
private static $instance;
static function getInstance()
{
if(!is_null(self::$instance)){
self::$instance = new self();
}
return self::$instance;
}
private function __construct()
{
try {
$this->connection = new PDO("mysql:dbname=fstk_test;host=localhost", 'root', '');
} catch (Exception $e){
echo $e->getMessage();
exit;
}
}
private function __clone(){}
private function __wake(){}
// C R U D
public function insert($table, $data)
{
$q = "INSERT INTO $table {{cols}} VALUES {{vals}}";
$keys = array_keys($data);
$q = str_replace("{{cols}}", implode(',', $keys), $q);
$q = str_replace("{{vals}}", ':' . implode(', :', $keys), $q);
echo $q;
$stmt = $this->connection->prepare($q);
foreach ($data as $k => $v){
if(is_int($v)){
$stmt->bindParam(":{$k}", $data[$k], PDO::PARAM_INT);
} else if (is_string($v)){
$stmt->bindParam(":{$k}", $data[$k], PDO::PARAM_STR);
} else {
$stmt->bindParam(":{$k}", $data[$k]);
}
}
$stmt->execute();
return $this->connection->lastInsertId();
}
public function select(){}
public function update(){}
public function delete(){}
}
$db = DbWrapper::getInstance();
$u = ['name' => 'Pit', 'password' => sha1("123"), 'email' => 'e'];
$db->insert('users', $u);