-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataAccess.php
More file actions
73 lines (63 loc) · 2.3 KB
/
dataAccess.php
File metadata and controls
73 lines (63 loc) · 2.3 KB
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
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$db_user = "ku46587";
$db_name = "db_ku46587";
$db_password = "queighod";
$pdo = new
PDO("mysql:host=localhost;dbname=$db_name",
$db_user,$db_password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
function getAllFriends()
{
global $pdo;
$statement = $pdo->prepare('SELECT id,givenName,surname,address,grade FROM friends');
$statement->execute();
$result = $statement->fetchAll(PDO::FETCH_CLASS, 'Friend');
return $result;
}
function getFriendsByGivenName($nameToSearchFor)
{
global $pdo;
$statement = $pdo->prepare("SELECT id,givenName,surname,address,grade FROM friends WHERE givenName = ?");
$statement->execute([$nameToSearchFor]);
$result = $statement->fetchAll(PDO::FETCH_CLASS, 'Friend');
return $result;
}
function getFriendsByBothGivenNameAndSurname($firstName,$secondName)
{
global $pdo;
$statement = $pdo->prepare("SELECT id,givenName,surname,address,grade FROM friends WHERE surname = ? AND givenName = ?");
$statement->execute([$secondName,$firstName]);
$result = $statement->fetchAll(PDO::FETCH_CLASS, 'Friend');
return $result;
}
function getFriendsBySurname($nameToSearchFor)
{
global $pdo;
$statement = $pdo->prepare("SELECT id,givenName,surname,address,grade FROM friends WHERE surname = ?");
$statement->execute([$nameToSearchFor]);
$result = $statement->fetchAll(PDO::FETCH_CLASS, 'Friend');
return $result;
}
function getFriendsByGrade($gradeToSearchFor)
{
global $pdo;
$statement = $pdo->prepare("SELECT id,givenName,surname,address,grade FROM friends WHERE surname = ?");
$statement->execute([$gradeToSearchFor]);
$result = $statement->fetchAll(PDO::FETCH_CLASS, 'Friend');
return $result;
}
function getFriendById($idToSearchFor)
{
global $pdo;
$statement = $pdo->prepare("SELECT id,givenName,surname,address,grade FROM friends WHERE id = ?");
$statement->execute([$idToSearchFor]);
// get by ID will only ever return a single result, so no point doing a fetchAll.
// fetchObject will return a single object of the specified class.
$result = $statement->fetchObject('Friend');
// this is less wasteful than fetchAll and then just returning result[0]
return $result;
}
?>