-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
45 lines (38 loc) · 1.33 KB
/
functions.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
<?php
function connectDb()
{
try {
$conn = new PDO("mysql:host=169.254.0.5:3307;dbname=authentication", 'root', 'admin');
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $conn;
} catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
return null;
}
}
function logUser($email, $password)
{
$connexion = connectDb();
$sql = 'SELECT * FROM users WHERE email = "' . $email . '" AND password = "' .$password . '"';
$stmt = $connexion->prepare($sql);
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_OBJ);
}
function getUser($id) {
$connexion = connectDb();
if (is_numeric($_POST['id']) && abs($_POST['id']) == $_POST['id']) {
$sql = 'SELECT * FROM users WHERE id = '.$_POST['id'];
}
else{echo "Sorry, only positive integers allowed here!";}
$stmt = $connexion->prepare($sql);
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_OBJ);
}
function saveUser($email, $username, $password) {
$connexion = connectDb();
$sql = 'INSERT INTO users(username,email,password) VALUES("'.$email.'","'.$username.'","'.$password.'")';
$stmt = $connexion->prepare($sql);
return $stmt->execute();
}