This repository has been archived by the owner on Aug 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdepedency_injection.php
124 lines (96 loc) · 2.21 KB
/
depedency_injection.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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
interface dbConnection
{
public function query();
public function result();
}
class mysqlConnection implements dbConnection
{
private $username;
private $password;
private $query;
private $table;
private $conn;
public function __construct($username, $password, $db)
{
$this->username = $username;
$this->password = $password;
$conn = mysql_connect('localhost', $username, $password);
mysql_select_db($db);
}
public function query($query = NULL)
{
$this->query = mysql_query($query);
return $this;
}
public function resultArray()
{
$tmp = array();
while ($row = mysql_fetch_array($this->query))
{
$tmp[] = $row;
}
return $tmp;
}
public function result()
{
$tmp = array();
while ($row = mysql_fetch_object($this->query))
{
$tmp[] = $row;
}
return $tmp;
}
public function get($table = NULL)
{
$this->table = $table;
return $this;
}
public function insert($table, $data)
{
$this->table = $table;
$field = "(" . implode(",", array_keys($data)) . ")";
$value = "('" . implode("','", $data) . "')";
$sql = "INSERT INTO {$table} {$field} VALUES {$value} ";
$query = $this->query($sql);
}
public function update($table, $data, $where = NULL)
{
$this->table = $table;
$dataUpdate = NULL;
foreach($data as $key => $value)
{
$dataUpdate[] =" $key = '{$value}' ";
}
$sql = "UPDATE {$this->table} SET " .implode(",", $dataUpdate) . " WHERE {$where}";
$query = $this->query($sql);
}
}
class Register
{
private $db;
public function __construct(dbConnection $db)
{
$this->db = $db;
}
public function getUser()
{
$query = $this->db->query('SELECT * FROM users');
return $query->result();
}
public function newUser($data)
{
$result = $this->db->insert('users', $data);
return $result;
}
public function updateByName($data, $name)
{
$query = $this->db->update('users', $data, "user_name = '{$name}' ");
}
}
$mysqlConnection = new mysqlConnection('root', '', 'db_skeddo');
$register = new Register($mysqlConnection);
$result = $register->getUser();
$data = array('user_name' => 'xxx123', 'user_password' => 'ridwan');
$register->newUser($data);
$register->updateByName(array('user_password' => 'ridwan sayang kamu'), 'xxx123');