-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.php
63 lines (54 loc) · 1.17 KB
/
database.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
<?php
require_once('db_credentials.php');
function db_connect()
{
$connection = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
confirm_db_connect();
return $connection;
}
function db_disconnect($connection)
{
if (isset($connection)) {
mysqli_close($connection);
}
}
function confirm_db_connect()
{
if (mysqli_connect_errno()) {
$msg = "Database connection failed: ";
$msg .= mysqli_connect_error();
$msg .= " (" . mysqli_connect_errno() . ")";
exit($msg);
}
}
function confirm_result_set($result_set)
{
if (!$result_set) {
exit("Database query failed.");
}
}
// Add this function to database.php to perform a query
function db_query($connection, $query)
{
$result = mysqli_query($connection, $query);
confirm_result_set($result);
return $result;
}
// Function to fetch the single row of result
function db_fetch_single($result)
{
return mysqli_fetch_assoc($result);
}
// Function to get the number of rows returned
function db_num_rows($result)
{
return mysqli_num_rows($result);
}
function db_free_result($result)
{
mysqli_free_result($result);
}
function db_fetch_array($result)
{
return mysqli_fetch_array($result);
}