-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.php
60 lines (53 loc) · 1.58 KB
/
index.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
<?php
function initializer($context)
{
$pdo = null;
try {
$pdo = new_pdo($context);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "CREATE TABLE IF NOT EXISTS users (
id VARCHAR(64) NOT NULL,
name VARCHAR(128) NOT NULL,
PRIMARY KEY(id))";
$pdo->beginTransaction();
$pdo->exec($sql);
$pdo->commit();
} catch (PDOException $e) {
if (!empty($pdo)) {
$pdo->rollback();
}
echo $e->getMessage();
}
return "The table `users` has been created!\r\n";
}
function handler($event, $context)
{
$pdo = null;
try {
$pdo = new_pdo($context);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->beginTransaction();
$pdo->exec("REPLACE INTO users (`id`,`name`) values('4','too')");
$pdo->commit();
$stmt = $pdo->query("SELECT * FROM users");
$users = '';
while ($row = $stmt->fetch()) {
$users = $users . ", " . $row['name'];
}
return $users;
} catch (PDOException $e) {
if (!empty($pdo)) {
$pdo->rollback();
}
return $e->getMessage();
}
}
function new_pdo($context)
{
$mysql_host = $_ENV['MYSQL_HOST'];
$mysql_port = $_ENV['MYSQL_PORT'];
$mysql_user = $_ENV['MYSQL_USER'];
$mysql_password = $_ENV['MYSQL_PASSWORD'];
$mysql_dbname = $_ENV['MYSQL_DBNAME'];
return new PDO("mysql:host=$mysql_host;port=$mysql_port;dbname=$mysql_dbname", $mysql_user, $mysql_password);
}