-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path002.php
151 lines (127 loc) · 3.91 KB
/
002.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
/*
*
002: Demonstrate using PHP how you would connect to a MySQL or PostgreSQL database and query
for a specific record with the following fields: “id_user”, “name”, “age”, “job_title”, “inserted_on”,
“last_updated”) from a table called 'users', using the ‘id_user’ field. Indicate how you would create the
table taking into consideration that we are going to access to the table data only by ‘id_user’. Provide an
example of how you would write a record in this same table with the user data that comes from a form
send by an HTTP POST request. Don’t need to create the form, but you can create it if it helps you.
Think about what in the database design can be improved, given the current set of data.
*/
class Database {
/**
* @var PDO
*/
protected $connection;
public function __construct()
{
$this->connect();
}
protected function connect()
{
//This would usually be from a config file/array
$servername = "localhost";
$username = "username";
$password = "password";
$db = "DB";
try {
$this->connection = new PDO("mysql:host={$servername};dbname={$db}", $username, $password);
// set the PDO error mode to exception
$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
}
/**
* @return PDO
*/
protected function getConnection()
{
if($this->connection){
return $this->connection;
}
$this->connect();
return $this->connection;
}
}
class UserRepository extends Database {
public function buildUserTable()
{
/**
* @var $connection PDO
*/
$connection = $this->getConnection();
$connection->query("
CREATE TABLE users (
id_user int PRIMARY KEY UNIQUE AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
age INT(3) NULL,
job_title VARCHAR(255) NULL,
inserted_on DATETIME,
last_updated DATETIME
);
");
}
public function byIdOrFail($userId)
{
/**
* @var $connection PDO
*/
$connection = $this->getConnection();
$query = $connection->prepare("
SELECT
id_user,
name,
age,
job_title,
inserted_on,
last_updated
FROM
users
WHERE
id_user=:id_user
;");
$query->bindParam(':id_user', $userId);
$query->execute();
return $query->fetch(PDO::FETCH_ASSOC);
}
public function create($name, $age, $jobTitle)
{
/**
* @var $connection PDO
*/
$connection = $this->getConnection();
$query = $connection->prepare("
INSERT INTO users
(
name,
age,
job_title,
inserted_on
)
values
(
:name,
:age,
:job_title,
now()
);
;");
$newUserId = null;
try {
$connection->beginTransaction();
$query->bindParam(':name', $name);
$query->bindParam(':age', $age);
$query->bindParam(':job_title', $jobTitle);
$query->execute();
$newUserId = $connection->lastInsertId();
$connection->commit();
} catch(PDOExecption $e) {
$connection->rollback();
}
return $newUserId;
}
}