1 | <?php
|
---|
2 | /*
|
---|
3 | $Id: mysql_database.php 2040 2008-11-23 05:46:25Z andrewsimpson $
|
---|
4 |
|
---|
5 | (c) 2002 - 2008 Andrew Simpson <andrew.simpson at paradise.net.nz>
|
---|
6 |
|
---|
7 | WebCollab
|
---|
8 | ---------------------------------------
|
---|
9 |
|
---|
10 | This program is free software; you can redistribute it and/or modify it under the
|
---|
11 | terms of the GNU General Public License as published by the Free Software Foundation;
|
---|
12 | either version 2 of the License, or (at your option) any later version.
|
---|
13 |
|
---|
14 | This program is distributed in the hope that it will be useful, but WITHOUT ANY
|
---|
15 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
---|
16 | PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
---|
17 |
|
---|
18 | You should have received a copy of the GNU General Public License along with this
|
---|
19 | program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave,
|
---|
20 | Cambridge, MA 02139, USA.
|
---|
21 |
|
---|
22 | Function:
|
---|
23 | ---------
|
---|
24 |
|
---|
25 | Creates a singular interface for mysql database access.
|
---|
26 |
|
---|
27 | */
|
---|
28 |
|
---|
29 | require_once('path.php' );
|
---|
30 |
|
---|
31 | //set some base variables
|
---|
32 | $database_connection = '';
|
---|
33 | $delim = '';
|
---|
34 | $epoch = 'UNIX_TIMESTAMP( ';
|
---|
35 | $day_part = 'DAYOFMONTH( ';
|
---|
36 | $date_type = '';
|
---|
37 |
|
---|
38 | //
|
---|
39 | // connect to database
|
---|
40 | //
|
---|
41 | function db_connection() {
|
---|
42 |
|
---|
43 | global $database_connection, $db_error_message;
|
---|
44 |
|
---|
45 | //make connection
|
---|
46 | if( ! ($database_connection = @mysql_connect(DATABASE_HOST, DATABASE_USER, DATABASE_PASSWORD ) ) ) {
|
---|
47 | error('No database connection', 'Sorry but there seems to be a problem in connecting to the database server');
|
---|
48 | }
|
---|
49 |
|
---|
50 | //select database
|
---|
51 | if( ! @mysql_select_db(DATABASE_NAME, $database_connection ) ) {
|
---|
52 | $db_error_message = mysql_error($database_connection );
|
---|
53 | error('Database error', 'Not able to select database tables' );
|
---|
54 | }
|
---|
55 |
|
---|
56 | //set transaction mode for innodb
|
---|
57 | if(DATABASE_TYPE == 'mysql_innodb' ) {
|
---|
58 | db_query('SET AUTOCOMMIT = 1' );
|
---|
59 | }
|
---|
60 |
|
---|
61 | //set timezone
|
---|
62 | if(! mysql_query("SET time_zone='".sprintf('%+d:%02d', (int)TZ, (TZ - floor(TZ) )*60 )."'", $database_connection ) ) {
|
---|
63 | $db_error_message = mysql_error($database_connection );
|
---|
64 | error("Database error", "Not able to set timezone. <br />Check that your version of MySQL is 4.1.3, or higher" );
|
---|
65 | }
|
---|
66 |
|
---|
67 | return;
|
---|
68 | }
|
---|
69 |
|
---|
70 | //
|
---|
71 | // Provides a safe way to do a query
|
---|
72 | //
|
---|
73 | function db_query($query, $die_on_error=1 ) {
|
---|
74 |
|
---|
75 | global $database_connection, $db_error_message;
|
---|
76 |
|
---|
77 | if(! $database_connection ) db_connection();
|
---|
78 |
|
---|
79 | //do it
|
---|
80 | if( ! ($result = @mysql_query($query, $database_connection ) ) ) {
|
---|
81 | $db_error_message = mysql_error($database_connection );
|
---|
82 | if($die_on_error) {
|
---|
83 | error('Database query error', 'The following query :<br /><br /><b>'.$query.'</b><br /><br />Had the following error:<br /><b>'.mysql_error($database_connection).'</b>' );
|
---|
84 | }
|
---|
85 | }
|
---|
86 |
|
---|
87 | //all was okay return resultset
|
---|
88 | return $result;
|
---|
89 | }
|
---|
90 |
|
---|
91 | //
|
---|
92 | // escapes special characters in a string for use in a SQL statement
|
---|
93 | //
|
---|
94 | function db_escape_string($string ) {
|
---|
95 |
|
---|
96 | global $database_connection;
|
---|
97 |
|
---|
98 | if(! $database_connection ) db_connection();
|
---|
99 |
|
---|
100 | return mysql_real_escape_string($string, $database_connection );
|
---|
101 | }
|
---|
102 |
|
---|
103 | //
|
---|
104 | // get single result set
|
---|
105 | //
|
---|
106 | function db_result($q, $row=0, $field=0 ) {
|
---|
107 |
|
---|
108 | return mysql_result($q, $row, $field );
|
---|
109 | }
|
---|
110 |
|
---|
111 | //
|
---|
112 | // fetch array result set
|
---|
113 | //
|
---|
114 | function db_fetch_array($q, $row=0 ) {
|
---|
115 |
|
---|
116 | return mysql_fetch_array($q, MYSQL_ASSOC );
|
---|
117 | }
|
---|
118 |
|
---|
119 | //
|
---|
120 | // fetch enumerated array result set
|
---|
121 | //
|
---|
122 | function db_fetch_num($q, $row=0 ) {
|
---|
123 |
|
---|
124 | return mysql_fetch_row($q );
|
---|
125 | }
|
---|
126 |
|
---|
127 | //
|
---|
128 | // last oid
|
---|
129 | //
|
---|
130 | function db_lastoid($seq ) {
|
---|
131 |
|
---|
132 | global $database_connection;
|
---|
133 |
|
---|
134 | return mysql_insert_id($database_connection );
|
---|
135 | }
|
---|
136 |
|
---|
137 | //
|
---|
138 | //free memory
|
---|
139 | //
|
---|
140 | function db_free_result($q ) {
|
---|
141 |
|
---|
142 | return mysql_free_result($q );
|
---|
143 | }
|
---|
144 |
|
---|
145 | //
|
---|
146 | //begin transaction
|
---|
147 | //
|
---|
148 | function db_begin() {
|
---|
149 |
|
---|
150 | global $database_connection;
|
---|
151 |
|
---|
152 | //not used for ISAM tables
|
---|
153 | if(DATABASE_TYPE == 'mysql' ) return true;
|
---|
154 |
|
---|
155 | return mysql_query('BEGIN' );
|
---|
156 | }
|
---|
157 |
|
---|
158 | //
|
---|
159 | //rollback transaction
|
---|
160 | //
|
---|
161 | function db_rollback() {
|
---|
162 |
|
---|
163 | global $database_connection;
|
---|
164 |
|
---|
165 | //not used for ISAM tables
|
---|
166 | if(DATABASE_TYPE == 'mysql' ) return true;
|
---|
167 |
|
---|
168 | return mysql_query('ROLLBACK' );
|
---|
169 | }
|
---|
170 |
|
---|
171 | //
|
---|
172 | //commit transaction
|
---|
173 | //
|
---|
174 | function db_commit() {
|
---|
175 |
|
---|
176 | global $database_connection;
|
---|
177 |
|
---|
178 | //not used for ISAM tables
|
---|
179 | if(DATABASE_TYPE == 'mysql' ) return true;
|
---|
180 |
|
---|
181 | return mysql_query('COMMIT' );
|
---|
182 | }
|
---|
183 |
|
---|
184 | //
|
---|
185 | //sets the required session client encoding
|
---|
186 | //
|
---|
187 | function db_user_locale($encoding ) {
|
---|
188 |
|
---|
189 | global $database_connection, $db_error_message;
|
---|
190 |
|
---|
191 | if(! $database_connection ) db_connection();
|
---|
192 |
|
---|
193 | switch(strtoupper($encoding ) ) {
|
---|
194 |
|
---|
195 | case 'ISO-8859-1':
|
---|
196 | $my_encoding = 'latin1';
|
---|
197 | break;
|
---|
198 |
|
---|
199 | case 'UTF-8':
|
---|
200 | $my_encoding = 'utf8';
|
---|
201 | break;
|
---|
202 |
|
---|
203 | case 'ISO-8859-2':
|
---|
204 | $my_encoding = 'latin2';
|
---|
205 | break;
|
---|
206 |
|
---|
207 | case 'ISO-8859-7':
|
---|
208 | $my_encoding = 'greek';
|
---|
209 | break;
|
---|
210 |
|
---|
211 | case 'ISO-8859-9':
|
---|
212 | //ISO-8859-9 === latin5 in MySQL!!
|
---|
213 | $my_encoding = 'latin5';
|
---|
214 | break;
|
---|
215 |
|
---|
216 | case 'KOI8-R':
|
---|
217 | $my_encoding = 'koi8r';
|
---|
218 | break;
|
---|
219 |
|
---|
220 | case 'WINDOWS-1251':
|
---|
221 | $my_encoding = 'cp1251';
|
---|
222 | break;
|
---|
223 |
|
---|
224 | default:
|
---|
225 | $my_encoding = 'latin1';
|
---|
226 | break;
|
---|
227 | }
|
---|
228 |
|
---|
229 | //set character set -- 1
|
---|
230 | if(! @mysql_query("SET CHARACTER SET ".$my_encoding, $database_connection ) ) {
|
---|
231 | $db_error_message = mysql_error($database_connection );
|
---|
232 | error("Database error", "Not able to set CHARACTER SET : ".$my_encoding );
|
---|
233 | }
|
---|
234 |
|
---|
235 | //set character set -- 2
|
---|
236 | if(! @mysql_query("SET NAMES '".$my_encoding."'", $database_connection ) ) {
|
---|
237 | $db_error_message = mysql_error($database_connection );
|
---|
238 | error("Database error", "Not able to set ".$my_encoding." client encoding" );
|
---|
239 | }
|
---|
240 | return true;
|
---|
241 | }
|
---|
242 |
|
---|
243 | ?>
|
---|