forked from grinnellplans/grinnellplans-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbfunctions.php
executable file
·93 lines (93 loc) · 3.19 KB
/
dbfunctions.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
<?php
require_once ('Plans.php');
/*
*Connects to the Database and returns the database handler.
*Establishes a persistant connection.
*/
function db_connect() {
$dbh = @mysqli_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS, MYSQL_DB);
if (!$dbh) {
print "Something's wrong with the database. You could report the error to [email protected]. <br/><pre>" . mysqli_error($dbh) . "</pre>";
exit;
}
$GLOBALS['dbh'] = $dbh;
@mysqli_query($dbh, "SET NAMES 'utf8mb4';");
return $dbh;
}
/*
*Given the database handler, closes the connection to the database.
*/
function db_disconnect($dbh) {
mysqli_close($dbh);
}
/*
*Adds a row to a table in the database.
*Takes the row as an array that represent the different columns.
*/
function add_row($dbh, $table, $row) {
$row = array_map(function($v) use ($dbh) { return '"'.mysqli_real_escape_string($dbh, $v).'"'; }, $row);
$joined_row = join(',', $row);
if (!mysqli_query($dbh, "INSERT INTO $table VALUES($joined_row)")) {
echo "Error adding entry to $table";
echo $joined_row . "<br />";
mysqli_close($dbh);
exit();
}
}
/*
*Gets a single item from the database. Returns a single item.
*/
function get_item($dbh, $get_column, $table, $search_column, $search_item) {
$search_item = mysqli_real_escape_string($dbh,$search_item);
$my_result = mysqli_query($dbh,"Select $get_column From $table where
$search_column = '$search_item'");
$my_row = mysqli_fetch_array($my_result);
return $my_row[0];
}
/*
*Returns multiple items from a database.
*Returns an 2-d array. The first index represents rows in
*the database. The second represents columns in the database.
*/
function get_items($dbh, $get_column, $table, $search_column, $search_item) {
$all = array();
$search_item = mysqli_real_escape_string($dbh,$search_item);
$my_result = mysqli_query($dbh,"Select $get_column From $table where
$search_column = '$search_item'");
while ($new_row = mysqli_fetch_row($my_result)) {
$all[] = $new_row;
}
return $all;
}
/*
*Removes a row from the database.
*/
function delete_item($dbh, $table, $search_column, $search_item) {
$search_item = mysqli_real_escape_string($dbh,$search_item);
mysqli_query($dbh,"DELETE FROM $table WHERE
$search_column = '$search_item'");
}
/*
*Changes an item in the database.
*/
function set_item($dbh, $dtable, $dcolumn_change, $dcolumn_value, $dsearch_column, $dsearch_item) {
$dcolumn_value = mysqli_real_escape_string($dbh,$dcolumn_value);
$dsearch_item = mysqli_real_escape_string($dbh,$dsearch_item);
mysqli_query($dbh,"UPDATE $dtable SET $dcolumn_change = '$dcolumn_value' WHERE
$dsearch_column = '$dsearch_item'");
}
/*
*Searches the database for a partial term and returns those parts
*that contain the term
*/
function partial_search($dbh, $get_column, $table, $search_column, $search_item, $orderby) {
$all = array();
$search_item = mysqli_real_escape_string($dbh,$search_item);
$my_result = mysqli_query($dbh,"Select $get_column From $table where
$search_column RLIKE '$search_item' ORDER by $orderby");
while ($new_row = mysqli_fetch_row($my_result)) {
$all[] = $new_row;
}
return $all;
}
?>