-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathweek11-pdoConnect.php
108 lines (96 loc) · 3.08 KB
/
week11-pdoConnect.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
<?php
//PDO connect
$db = NULL;
// Let's connect to a database
// Order of db connection: Heroku mySQL Database --> Local mySQL Database
// Check to see if we are on a Heroku Server by checking for an environmental variable with db data
if ((getenv('JAWSDB_URL') != null)) {
$dbparts = parse_url(getenv('JAWSDB_URL'));
$hostname = $dbparts['host'];
$username = $dbparts['user'];
$password = $dbparts['pass'];
$database = ltrim($dbparts['path'],'/');
try {
$db = new PDO("mysql:host=$hostname;dbname=$database", $username, $password, array(PDO::MYSQL_ATTR_FOUND_ROWS => true));
// set the PDO error mode to exception
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//echo "Connected successfully";
}
catch(PDOException $e) {
http_error(500, "Internal Server Error", "We couldn't connect to a Heroku MySQL database.");
//echo "Connection failed: " . $e->getMessage();
}
}
else {
$hostname = "localhost";
$username = "root";
$password = "mypassword";
$database = "is218demo";
try {
$db = new PDO("mysql:host=$hostname;dbname=$database", $username, $password, array(PDO::MYSQL_ATTR_FOUND_ROWS => true));
}
catch (PDOException $e) {
http_error(500, "Internal Server Error", "We couldn't connect to a local(host) MySQL database.");
}
}
// Prepares and executes the given sql query with the provided parameters array.
// also removes the need for having quotes and washing via prepare
// fetchAll - if true fetches all rows, if false only fetches first row.
function prepareAndExecute($sql, $params = null, $fetchAll = false, $returnNoAffected = false) {
global $db;
try {
// Clean up character if using mySQL using next line
$sql = str_replace("\"", "", $sql);
$q = $db->prepare($sql);
$q->execute($params);
if (strpos($sql, "INSERT") !== false || strpos($sql, "DELETE") !== false || strpos($sql, "UPDATE") !== false) {
if (!$returnNoAffected) {
return $db->lastInsertId();
} else {
return $q->rowCount();
}
} else {
if ($fetchAll) {
return $q->fetchAll(PDO::FETCH_ASSOC);
} else {
return $q->fetch(PDO::FETCH_ASSOC);
}
}
} catch (PDOException $e) {
if (true) { // change to false when deploying
http_error(500, "Internal Server Error", "There was a SQL error:\n\n" . $e->getMessage());
} else {
http_error(500, "Internal Server Error", "Something went wrong.");
}
}
}
// begins transaction
function beginTransaction() {
global $db;
$db->beginTransaction();
}
// commits changes
function commit() {
global $db;
$db->commit();
}
// rollbacks any changes
function rollBack() {
global $db;
$db->rollBack();
}
/**
* Checks if the specified parameter exists in the $_POST array.
* @param String $paramName the name of the parameter
*/
function checkParameterOrDie($paramName) {
if (!isset($_REQUEST[$paramName])) {
header('HTTP/1.1 500 Internal Server Error');
exit("ERROR: There was an error receiving '$paramName' from the form submitter.");
}
}
function http_error($code, $status, $message) {
header("HTTP/1.1 $code $status");
header("Content-type: text/plain");
die($message);
}