-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
47 lines (40 loc) · 1.41 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
<?php
try {
date_default_timezone_set('UTC');
# substitua os dados
$host = "";
$port = "";
$database = "";
$user = "";
$password = "";
$dsn = "pgsql:host=$host;port=$port;dbname=$database;";
$pdoConnection = new PDO($dsn, $user, $password, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
function insertUser($pdoConnection, $cpf, $name, $email, $password)
{
$today = date("Y-m-d");
$sql = "INSERT INTO users(cpf, name, email, password, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?)";
$statement = $pdoConnection->prepare($sql);
$statement->bindValue(1, $cpf);
$statement->bindValue(2, $name);
$statement->bindValue(3, $email);
$statement->bindValue(4, $password);
$statement->bindValue(5, $today);
$statement->bindValue(6, $today);
$statement->execute();
}
function getUsers($pdoConnection)
{
$sql = "SELECT * FROM users";
$result = $pdoConnection->query($sql);
return $result->fetchAll(PDO::FETCH_OBJ);
}
insertUser($pdoConnection, "111.111.111-11", "John Doe", "[email protected]", "johndoe");
$users = getUsers($pdoConnection);
foreach ($users as $user) {
echo $user->name . "\n";
}
} catch (PDOException $e) {
die("Database Error: " . $e->getMessage());
} catch (\Exception $e) {
die("An Error Occurred: " . $e->getMessage());
}