forked from mitchgre/gregsList
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengineer.php
executable file
·116 lines (93 loc) · 2.45 KB
/
engineer.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
109
110
111
112
113
114
115
116
<?php
/*
This file contains some utility functions to simplify queries and database connections.
*/
/*
Interface for sending a query using prepared statements.
Returns true if the query is successful, false otherwise.
This function is used for insertions as opposed to extractions.
*/
function preparedStatement($query)
{
$mysqli = connectToDB();
$mysqli -> set_charset("utf8");
if( $sql = $mysqli->prepare($query))
{
$sql->execute();
if ($sql->affected_rows >= 1)
{
mysqli_close($mysqli);
return true;
}
return false;
}
// mysqli_close($mysqli);
return false;
}
/*
This function can return a single column from mysql. Useful for" generalizing quick searches, but not good for building entire tables
up from the database.
*/
function returnStuff($query)
{
$mysqli = connectToDB();
$container = []; // empty container
if ($statement=$mysqli->prepare($query))
{
$statement->execute();
// bind results
$statement->bind_result($i);
while($statement->fetch()) // loop over results and push into array
array_push($container,$i);
mysqli_close($mysqli);
return $container;
}
else
{
mysqli_close($mysqli);
return ["error"];
}
}
/*
Establish a connection to the gregsList database, and return the connection.
*/
function connectToDB()
{
$config = parse_ini_file('config.ini');
$mysqli = new mysqli($config['sqlhostname'], $config['sqlusername'], $config['sqlpassword'], "gregsList");
if ($mysqli->connect_errno)
{
echo "Failed to connect to MySQL: (" .
$mysqli->connect_errno. ") " .
$mysqli->connect_error;
}
return $mysqli;
}
// http://stackoverflow.com/questions/2280394/how-can-i-check-if-a-url-exists-via-php
function validURL($url)
{
return strpos(@get_headers($url)[0],'200') === false ? false : true;
}
function booleanEcho($query)
{
if (preparedStatement($query))
{
echo json_encode(true);
}
else
{
echo json_encode(false);
}
}
function booleanReturn($query)
{
if (preparedStatement($query))
{
return true;
}
else
{
return false;
}
}
?>