|
1 |
| -<?php |
2 |
| -// how to connect to a MySQL database and a variety of database functions |
3 |
| - |
4 |
| - |
5 |
| - |
6 |
| -//// Connect to Database //// |
7 |
| - |
8 |
| -// database parameters |
9 |
| -$db_host = "database.google.com"; |
10 |
| -$db_user = "admin"; |
11 |
| -$db_pswd = "password"; |
12 |
| -$db_name = "main"; |
13 |
| - |
14 |
| -// establish database connection |
15 |
| -mysql_connect($db_host, $db_user, $db_pswd); |
16 |
| -mysql_select_db($db_name) |
17 |
| - or die( "Cannot select database " . $db_name . "."); |
18 |
| - |
19 |
| - |
20 |
| - |
21 |
| -//// General Database Functions //// |
22 |
| - |
23 |
| -// standard database query function |
24 |
| -function dbQuery($query, $msg = ""){ |
25 |
| - $result = mysql_query($query); |
26 |
| - dbError(mysql_error(), $msg); |
27 |
| - return $result; |
28 |
| -} |
29 |
| - |
30 |
| -// displaying error from database query |
31 |
| -function dbError($error, $msg = ""){ |
32 |
| - if($error){ |
33 |
| - echo $error . "<br/>"; |
34 |
| - die(); |
35 |
| - }else{ |
36 |
| - echo $msg; |
37 |
| - } |
38 |
| -} |
39 |
| - |
40 |
| -// write information to the database |
41 |
| -function dbWrite($table, $fields, $values){ |
42 |
| - $query = "INSERT INTO " . $table . " (" . $fields . ") VALUES (" . $values . ")"; |
43 |
| - mysql_query($query); |
44 |
| - echo mysql_error(); |
45 |
| -} |
46 |
| - |
47 |
| -// clean input given by user |
48 |
| -function cleanInputs($str){ |
49 |
| - return strip_tags(addslashes($str)); |
50 |
| -} |
51 |
| - |
52 |
| -// clean inputs to be inserted into the database |
53 |
| -function cleanForDB($str){ |
54 |
| - return trim(mysql_real_escape_string(strip_tags($str))); |
55 |
| -} |
56 |
| - |
57 |
| - |
58 |
| - |
59 |
| -//// Miscellaneous Functions //// |
60 |
| - |
61 |
| -// current time, midwest time-zone |
62 |
| -function getTime(){ |
63 |
| - $dateTime = new DateTime(null, new DateTimeZone('America/Chicago')); |
64 |
| - $time = date_format($dateTime, 'Y-m-d H:i:s'); |
65 |
| - return $time; |
66 |
| -} |
67 |
| - |
68 |
| -// give a number of pages, based on how many items and items per page |
69 |
| -function getNumPages($numRows, $n){ |
70 |
| - $tot = ceil($numRows / $n); |
71 |
| - return $tot; |
72 |
| -} |
73 |
| - |
74 |
| - |
75 |
| - |
76 |
| -//// Grab a Database Value //// |
77 |
| - |
78 |
| -// get a name from some ID |
79 |
| -function getName($id){ |
80 |
| - $query = "SELECT name FROM table WHERE id=$id"; |
81 |
| - $result = dbQuery($query); |
82 |
| - while($row = mysql_fetch_array($result)){ |
83 |
| - return $row[0]; |
84 |
| - } |
85 |
| -} |
86 |
| - |
87 |
| -// get a count from some ID |
88 |
| -function getCount($id){ |
89 |
| - $query = "SELECT COUNT(DISTINCT name) FROM table WHERE id=$id"; |
90 |
| - $result = dbQuery($query); |
91 |
| - while($row = mysql_fetch_array($result)){ |
92 |
| - return $row[0]; |
93 |
| - } |
94 |
| - return 0; |
95 |
| -} |
96 |
| - |
97 |
| -// get the most recent time this ID was updated |
98 |
| -function getLastUpdate($id){ |
99 |
| - $query = "SELECT MAX(time) FROM table WHERE id=$id"; |
100 |
| - $result = dbQuery($query); |
101 |
| - while($row = mysql_fetch_array($result)){ |
102 |
| - return $row[0]; |
103 |
| - } |
104 |
| -} |
105 |
| - |
106 |
| - |
107 |
| - |
108 |
| -//// Grab a Database Array //// |
109 |
| - |
110 |
| -// get all ID's for a user |
111 |
| -function getIDs($user){ |
112 |
| - $query = "SELECT DISTINCT id FROM table WHERE user=$user"; |
113 |
| - $result = dbQuery($query); |
114 |
| - $IDs = array(); |
115 |
| - while($row = mysql_fetch_array($result)){ |
116 |
| - array_push($IDs, $row[0]); |
117 |
| - } |
118 |
| - return $IDs; |
119 |
| -} |
120 |
| - |
121 |
| - |
122 |
| - |
123 |
| -//// Grab a Database Boolean //// |
124 |
| - |
125 |
| -// check for item existence |
126 |
| -function getExists($id){ |
127 |
| - $query = "SELECT name FROM table WHERE id=$id"; |
128 |
| - $result = dbQuery($query); |
129 |
| - while($row = mysql_fetch_array($result)){ |
130 |
| - return TRUE; |
131 |
| - } |
132 |
| - return FALSE; |
133 |
| -} |
134 |
| - |
135 |
| -// check to see if enough users are tied to an item (3 or more) |
136 |
| -function getEnoughUsers($id){ |
137 |
| - $query = "SELECT COUNT(DISTINCT user) FROM table WHERE id=$id"; |
138 |
| - $result = dbQuery($query); |
139 |
| - while($row = mysql_fetch_array($result)){ |
140 |
| - if($row[0] > 2) |
141 |
| - return TRUE; |
142 |
| - else |
143 |
| - return FALSE; |
144 |
| - } |
145 |
| - return FALSE; |
146 |
| -} |
147 |
| - |
148 |
| - |
149 |
| - |
150 |
| -//// Modify Database Values //// |
151 |
| - |
152 |
| -// create a new item |
153 |
| -function createItem($id, $location, $time){ |
154 |
| - $table = "table"; |
155 |
| - $fields = "id, file_location, created_at, updated_at"; |
156 |
| - $values = '"' . $id . '", "' . $location . '", "' . $time . '", "' . $time . '"'; |
157 |
| - dbWrite($table, $fields, $values); |
158 |
| -} |
159 |
| - |
160 |
| -// update an item |
161 |
| -function updateItem($id, $location, $time){ |
162 |
| - $query = 'UPDATE table SET file_location="' . $location . '", updated_at="' . $time . '" WHERE id="' . $id . '"'; |
163 |
| - mysql_query($query); |
164 |
| - echo mysql_error(); |
165 |
| -} |
166 |
| - |
167 |
| -// delete an item |
168 |
| -function removeItem($user, $id){ |
169 |
| - $table = "table"; |
170 |
| - $query = "DELETE FROM $table WHERE user=$user AND id=$id"; |
171 |
| - $result = mysql_query($query); |
172 |
| - echo mysql_error(); |
173 |
| -} |
174 |
| - |
175 |
| -?> |
| 1 | +<?php |
| 2 | +// how to connect to a MySQL database and a variety of database functions |
| 3 | + |
| 4 | + |
| 5 | + |
| 6 | +//// Connect to Database //// |
| 7 | + |
| 8 | +// database parameters |
| 9 | +$db_host = "database.google.com"; |
| 10 | +$db_user = "admin"; |
| 11 | +$db_pswd = "password"; |
| 12 | +$db_name = "main"; |
| 13 | + |
| 14 | +// establish database connection |
| 15 | +mysql_connect($db_host, $db_user, $db_pswd); |
| 16 | +mysql_select_db($db_name) |
| 17 | + or die( "Cannot select database " . $db_name . "."); |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | +//// General Database Functions //// |
| 22 | + |
| 23 | +// standard database query function |
| 24 | +function dbQuery($query, $msg = ""){ |
| 25 | + $result = mysql_query($query); |
| 26 | + dbError(mysql_error(), $msg); |
| 27 | + return $result; |
| 28 | +} |
| 29 | + |
| 30 | +// displaying error from database query |
| 31 | +function dbError($error, $msg = ""){ |
| 32 | + if($error){ |
| 33 | + echo $error . "<br/>"; |
| 34 | + die(); |
| 35 | + }else{ |
| 36 | + echo $msg; |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +// write information to the database |
| 41 | +function dbWrite($table, $fields, $values){ |
| 42 | + $query = "INSERT INTO " . $table . " (" . $fields . ") VALUES (" . $values . ")"; |
| 43 | + mysql_query($query); |
| 44 | + echo mysql_error(); |
| 45 | +} |
| 46 | + |
| 47 | +// clean input given by user |
| 48 | +function cleanInputs($str){ |
| 49 | + return strip_tags(addslashes($str)); |
| 50 | +} |
| 51 | + |
| 52 | +// clean inputs to be inserted into the database |
| 53 | +function cleanForDB($str){ |
| 54 | + return trim(mysql_real_escape_string(strip_tags($str))); |
| 55 | +} |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | +//// Miscellaneous Functions //// |
| 60 | + |
| 61 | +// current time, midwest time-zone |
| 62 | +function getTime(){ |
| 63 | + $dateTime = new DateTime(null, new DateTimeZone('America/Chicago')); |
| 64 | + $time = date_format($dateTime, 'Y-m-d H:i:s'); |
| 65 | + return $time; |
| 66 | +} |
| 67 | + |
| 68 | +// give a number of pages, based on how many items and items per page |
| 69 | +function getNumPages($numRows, $n){ |
| 70 | + $tot = ceil($numRows / $n); |
| 71 | + return $tot; |
| 72 | +} |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | +//// Grab a Database Value //// |
| 77 | + |
| 78 | +// get a name from some ID |
| 79 | +function getName($id){ |
| 80 | + $query = "SELECT name FROM table WHERE id=$id"; |
| 81 | + $result = dbQuery($query); |
| 82 | + while($row = mysql_fetch_array($result)){ |
| 83 | + return $row[0]; |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +// get a count from some ID |
| 88 | +function getCount($id){ |
| 89 | + $query = "SELECT COUNT(DISTINCT name) FROM table WHERE id=$id"; |
| 90 | + $result = dbQuery($query); |
| 91 | + while($row = mysql_fetch_array($result)){ |
| 92 | + return $row[0]; |
| 93 | + } |
| 94 | + return 0; |
| 95 | +} |
| 96 | + |
| 97 | +// get the most recent time this ID was updated |
| 98 | +function getLastUpdate($id){ |
| 99 | + $query = "SELECT MAX(time) FROM table WHERE id=$id"; |
| 100 | + $result = dbQuery($query); |
| 101 | + while($row = mysql_fetch_array($result)){ |
| 102 | + return $row[0]; |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | +//// Grab a Database Array //// |
| 109 | + |
| 110 | +// get all ID's for a user |
| 111 | +function getIDs($user){ |
| 112 | + $query = "SELECT DISTINCT id FROM table WHERE user=$user"; |
| 113 | + $result = dbQuery($query); |
| 114 | + $IDs = array(); |
| 115 | + while($row = mysql_fetch_array($result)){ |
| 116 | + array_push($IDs, $row[0]); |
| 117 | + } |
| 118 | + return $IDs; |
| 119 | +} |
| 120 | + |
| 121 | + |
| 122 | + |
| 123 | +//// Grab a Database Boolean //// |
| 124 | + |
| 125 | +// check for item existence |
| 126 | +function getExists($id){ |
| 127 | + $query = "SELECT name FROM table WHERE id=$id"; |
| 128 | + $result = dbQuery($query); |
| 129 | + while($row = mysql_fetch_array($result)){ |
| 130 | + return TRUE; |
| 131 | + } |
| 132 | + return FALSE; |
| 133 | +} |
| 134 | + |
| 135 | +// check to see if enough users are tied to an item (3 or more) |
| 136 | +function getEnoughUsers($id){ |
| 137 | + $query = "SELECT COUNT(DISTINCT user) FROM table WHERE id=$id"; |
| 138 | + $result = dbQuery($query); |
| 139 | + while($row = mysql_fetch_array($result)){ |
| 140 | + if($row[0] > 2) |
| 141 | + return TRUE; |
| 142 | + else |
| 143 | + return FALSE; |
| 144 | + } |
| 145 | + return FALSE; |
| 146 | +} |
| 147 | + |
| 148 | + |
| 149 | + |
| 150 | +//// Modify Database Values //// |
| 151 | + |
| 152 | +// create a new item |
| 153 | +function createItem($id, $location, $time){ |
| 154 | + $table = "table"; |
| 155 | + $fields = "id, file_location, created_at, updated_at"; |
| 156 | + $values = '"' . $id . '", "' . $location . '", "' . $time . '", "' . $time . '"'; |
| 157 | + dbWrite($table, $fields, $values); |
| 158 | +} |
| 159 | + |
| 160 | +// update an item |
| 161 | +function updateItem($id, $location, $time){ |
| 162 | + $query = 'UPDATE table SET file_location="' . $location . '", updated_at="' . $time . '" WHERE id="' . $id . '"'; |
| 163 | + mysql_query($query); |
| 164 | + echo mysql_error(); |
| 165 | +} |
| 166 | + |
| 167 | +// delete an item |
| 168 | +function removeItem($user, $id){ |
| 169 | + $table = "table"; |
| 170 | + $query = "DELETE FROM $table WHERE user=$user AND id=$id"; |
| 171 | + $result = mysql_query($query); |
| 172 | + echo mysql_error(); |
| 173 | +} |
| 174 | + |
| 175 | +?> |
0 commit comments