Skip to content

Latest commit

 

History

History
173 lines (130 loc) · 3.43 KB

database.php

File metadata and controls

173 lines (130 loc) · 3.43 KB
 
Jul 1, 2010
Jul 1, 2010
1
2
3
4
5
<?php
// This file adds a little bit of a wrapper over MySQL, and a little
// bit of convenience functionality.
Jul 5, 2010
Jul 5, 2010
6
require_once 'localcfg.php';
Jul 1, 2010
Jul 1, 2010
7
Jul 5, 2010
Jul 5, 2010
8
9
$GDebugSQL = false;
$GDebugSQLErrors = true;
Jul 1, 2010
Jul 1, 2010
10
11
$dblink = NULL;
Jul 1, 2010
Jul 1, 2010
12
13
function write_error($err)
{
Jul 5, 2010
Jul 5, 2010
14
15
16
global $GDebugSQLErrors;
if ($GDebugSQLErrors)
print("\n\nERROR: $err\n\n");
Jul 1, 2010
Jul 1, 2010
17
18
19
20
} // write_error
function write_debug($err)
{
Jul 5, 2010
Jul 5, 2010
21
22
23
global $GDebugSQL;
if ($GDebugSQL)
print("\n\nDEBUG: $err\n\n");
Jul 1, 2010
Jul 1, 2010
24
25
} // write_debug
Jul 1, 2010
Jul 1, 2010
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
function get_dblink()
{
global $dblink;
if ($dblink == NULL)
{
global $dbhost, $dbuser, $dbpass, $dbname;
$dblink = mysql_connect($dbhost, $dbuser, $dbpass);
if (!$dblink)
{
$err = mysql_error();
write_error("Failed to open database link: ${err}.");
$dblink = NULL;
} // if
if (!mysql_select_db($dbname))
{
$err = mysql_error();
write_error("Failed to select database: ${err}.");
mysql_close($dblink);
$dblink = NULL;
} // if
} // if
return($dblink);
} // get_dblink
function close_dblink($link = NULL)
{
global $dblink;
$closeme = $link;
if ($closeme == NULL)
{
$closeme = $dblink;
$dblink = NULL;
} // if
if ($closeme != NULL)
mysql_close($closeme);
} // close_dblink
function db_escape_string($str)
{
return("'" . mysql_escape_string($str) . "'");
} // db_escape_string
function do_dbquery($sql, $link = NULL, $suppress_output = false)
{
if ($link == NULL)
$link = get_dblink();
if ($link == NULL)
return(false);
if (!$suppress_output)
write_debug("SQL query: [$sql;]");
$rc = mysql_query($sql, $link);
if ($rc == false)
{
if (!$suppress_output)
{
$err = mysql_error();
write_error("Problem in SELECT statement: {$err}");
} // if
return(false);
} // if
return($rc);
} // do_dbquery
function do_dbwrite($sql, $verb, $expected_rows = 1, $link = NULL)
{
if ($link == NULL)
$link = get_dblink();
if ($link == NULL)
return(false);
write_debug("SQL $verb: [$sql;]");
$rc = mysql_query($sql, $link);
if ($rc == false)
{
$err = mysql_error();
$upperverb = strtoupper($verb);
write_error("Problem in $upperverb statement: {$err}");
return(false);
} // if
$retval = mysql_affected_rows($link);
if (($expected_rows >= 0) and ($retval != $expected_rows))
{
$err = mysql_error();
write_error("Database $verb error: {$err}");
} // if
return($retval);
} // do_dbwrite
function do_dbinsert($sql, $expected_rows = 1, $link = NULL)
{
return(do_dbwrite($sql, 'insert', $expected_rows, $link));
} // do_dbinsert
function do_dbupdate($sql, $expected_rows = 1, $link = NULL)
{
return(do_dbwrite($sql, 'update', $expected_rows, $link));
} // do_dbupdate
function do_dbdelete($sql, $expected_rows = 1, $link = NULL)
{
return(do_dbwrite($sql, 'delete', $expected_rows, $link));
} // do_dbdelete
function db_num_rows($query)
{
return(mysql_num_rows($query));
} // db_num_rows
function db_reset_array($query)
{
return(mysql_data_seek($query, 0));
} // db_reset_array
function db_fetch_array($query)
{
return(mysql_fetch_assoc($query));
} // db_fetch_array
function db_free_result($query)
{
return(mysql_free_result($query));
} // db_free_result
?>