-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnect.php
67 lines (59 loc) · 1.79 KB
/
connect.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
<?php
$q = $_GET['arg'];
if($q == "insertTombstone") {
$date = $_GET['dod'];
$name = $_GET['name'];
$mile = $_GET['mile'];
$msg = $_GET['msg'];
}
if($q == "insertScore") {
$username = $_GET['usr'];
$points = $_GET['points'];
$rating = $_GET['rating'];
}
function connect($db)
{
$conn = @mysql_connect("studentdb-maria.gl.umbc.edu", "mansha1", "mansha1") or die("Could not connect to MySQL");
$rs = @mysql_select_db($db, $conn) or die("Could not connect select $db database");
return $conn;
}
function executeQuery($sql, $conn) {
$result = mysql_query($sql, $conn);
if ($error = mysql_error()) die('Error, query failed with: ' . $error);
return $result;
}
function parseResult($result, $start_index) {
$resultArray = array();
while($row = mysql_fetch_row($result)) {
for($i = $start_index; $i < count($row); $i++) {
//echo $row[$i] . "  ";
}
//echo "<br>";
$resultArray[] = $row;
}
echo json_encode($resultArray);
}
if(empty($connection)) {
$connection = connect("mansha1");
}
if($q == "getTopScores") {
$sql = "SELECT * FROM high_score ORDER BY points DESC LIMIT 10";
$result = executeQuery($sql, $connection);
parseResult($result, 1);
}
if($q == "getTombstones") {
$sql = "SELECT * FROM tombstones ORDER BY mile";
$result = executeQuery($sql, $connection);
parseResult($result, 2);
}
if($q == "insertTombstone") {
$sql = "INSERT INTO tombstones (DOD, name, mile, message) VALUES ('" . $date . "', '" . $name . "', '" . $mile . "', '" . $msg . "')";
echo $sql;
$result = executeQuery($sql, $connection);
}
if($q == "insertScore") {
$sql = "INSERT INTO high_score (username, points, rating) VALUES ('" . $username . "', '" . $points . "', '" . $rating . "')";
echo $sql;
$result = executeQuery($sql, $connection);
}
?>