1
|
<?php
|
2
|
/**
|
3
|
*
|
4
|
* @category framework
|
5
|
* @package database
|
6
|
* @author WebsiteBaker Project
|
7
|
* @copyright 2004-2009, Ryan Djurovich
|
8
|
* @copyright 2009-2011, Website Baker Org. e.V.
|
9
|
* @link http://www.websitebaker2.org/
|
10
|
* @license http://www.gnu.org/licenses/gpl.html
|
11
|
* @platform WebsiteBaker 2.8.x
|
12
|
* @requirements PHP 5.2.2 and higher
|
13
|
* @version $Id: class.database.php 1362 2010-12-29 11:24:22Z Luisehahne $
|
14
|
* @filesource $HeadURL: $
|
15
|
* @lastmodified $Date: $
|
16
|
*
|
17
|
*/
|
18
|
|
19
|
/*
|
20
|
|
21
|
Database class
|
22
|
|
23
|
This class will be used to interface between the database
|
24
|
and the Website Baker code
|
25
|
|
26
|
*/
|
27
|
|
28
|
// Stop this file from being accessed directly
|
29
|
if(!defined('WB_URL')) {
|
30
|
header('Location: ../index.php');
|
31
|
exit(0);
|
32
|
}
|
33
|
|
34
|
if(!defined('DB_URL')) {
|
35
|
//define('DB_URL', DB_TYPE.'://'.DB_USERNAME.':'.DB_PASSWORD.'@'.DB_HOST.'/'.DB_NAME);
|
36
|
}
|
37
|
|
38
|
define('DATABASE_CLASS_LOADED', true);
|
39
|
|
40
|
class database {
|
41
|
|
42
|
private $db_handle = null; // readonly from outside
|
43
|
|
44
|
private $connected = false;
|
45
|
|
46
|
private $error = '';
|
47
|
private $error_type = '';
|
48
|
private $message = array();
|
49
|
|
50
|
|
51
|
// Set DB_URL
|
52
|
function database($url = '') {
|
53
|
// Connect to database
|
54
|
$this->connect();
|
55
|
// Check for database connection error
|
56
|
if($this->is_error()) {
|
57
|
die($this->get_error());
|
58
|
}
|
59
|
}
|
60
|
|
61
|
// Connect to the database
|
62
|
function connect() {
|
63
|
$status = $this->db_handle = mysql_connect(DB_HOST, DB_USERNAME, DB_PASSWORD);
|
64
|
if(mysql_error()) {
|
65
|
$this->connected = false;
|
66
|
$this->error = mysql_error();
|
67
|
} else {
|
68
|
if(!mysql_select_db(DB_NAME)) {
|
69
|
$this->connected = false;
|
70
|
$this->error = mysql_error();
|
71
|
} else {
|
72
|
$this->connected = true;
|
73
|
}
|
74
|
}
|
75
|
return $this->connected;
|
76
|
}
|
77
|
|
78
|
// Disconnect from the database
|
79
|
function disconnect() {
|
80
|
if($this->connected==true) {
|
81
|
mysql_close();
|
82
|
return true;
|
83
|
} else {
|
84
|
return false;
|
85
|
}
|
86
|
}
|
87
|
|
88
|
// Run a query
|
89
|
function query($statement) {
|
90
|
$mysql = new mysql();
|
91
|
$mysql->query($statement);
|
92
|
$this->set_error($mysql->error());
|
93
|
if($mysql->error()) {
|
94
|
return null;
|
95
|
} else {
|
96
|
return $mysql;
|
97
|
}
|
98
|
}
|
99
|
|
100
|
// Gets the first column of the first row
|
101
|
function get_one( $statement )
|
102
|
{
|
103
|
$fetch_row = mysql_fetch_array(mysql_query($statement) );
|
104
|
$result = $fetch_row[0];
|
105
|
$this->set_error(mysql_error());
|
106
|
if(mysql_error()) {
|
107
|
return null;
|
108
|
} else {
|
109
|
return $result;
|
110
|
}
|
111
|
}
|
112
|
|
113
|
// Set the DB error
|
114
|
function set_error($message = null) {
|
115
|
global $TABLE_DOES_NOT_EXIST, $TABLE_UNKNOWN;
|
116
|
$this->error = $message;
|
117
|
if(strpos($message, 'no such table')) {
|
118
|
$this->error_type = $TABLE_DOES_NOT_EXIST;
|
119
|
} else {
|
120
|
$this->error_type = $TABLE_UNKNOWN;
|
121
|
}
|
122
|
}
|
123
|
|
124
|
// Return true if there was an error
|
125
|
function is_error() {
|
126
|
return (!empty($this->error)) ? true : false;
|
127
|
}
|
128
|
|
129
|
// Return the error
|
130
|
function get_error() {
|
131
|
return $this->error;
|
132
|
}
|
133
|
|
134
|
/*
|
135
|
* default Getter
|
136
|
*/
|
137
|
public function __get($var_name)
|
138
|
{
|
139
|
if($var_name == 'db_handle')
|
140
|
{
|
141
|
return $this->db_handle;
|
142
|
}
|
143
|
return null;
|
144
|
}
|
145
|
|
146
|
/*
|
147
|
* @param string $table_name: full name of the table (incl. TABLE_PREFIX)
|
148
|
* @param string $field_name: name of the field to seek for
|
149
|
* @return bool: true if field exists
|
150
|
*/
|
151
|
public function field_exists($table_name, $field_name)
|
152
|
{
|
153
|
$sql = 'DESCRIBE `'.$table_name.'` `'.$field_name.'` ';
|
154
|
$query = $this->query($sql);
|
155
|
return ($query->numRows() != 0);
|
156
|
}
|
157
|
|
158
|
/*
|
159
|
* @param string $table_name: full name of the table (incl. TABLE_PREFIX)
|
160
|
* @param string $index_name: name of the index to seek for
|
161
|
* @return bool: true if field exists
|
162
|
*/
|
163
|
public function index_exists($table_name, $index_name, $number_fields = 0)
|
164
|
{
|
165
|
$number_fields = intval($number_fields);
|
166
|
$keys = 0;
|
167
|
$sql = 'SHOW INDEX FROM `'.$table_name.'`';
|
168
|
if( ($res_keys = $this->query($sql)) )
|
169
|
{
|
170
|
while(($rec_key = $res_keys->fetchRow()))
|
171
|
{
|
172
|
if( $rec_key['Key_name'] == $index_name )
|
173
|
{
|
174
|
$keys++;
|
175
|
}
|
176
|
}
|
177
|
|
178
|
}
|
179
|
if( $number_fields == 0 )
|
180
|
{
|
181
|
return ($keys != $number_fields);
|
182
|
}else
|
183
|
{
|
184
|
return ($keys == $number_fields);
|
185
|
}
|
186
|
}
|
187
|
/*
|
188
|
* @param string $table_name: full name of the table (incl. TABLE_PREFIX)
|
189
|
* @param string $field_name: name of the field to add
|
190
|
* @param string $description: describes the new field like ( INT NOT NULL DEFAULT '0')
|
191
|
* @return bool: true if successful, otherwise false and error will be set
|
192
|
*/
|
193
|
public function field_add($table_name, $field_name, $description)
|
194
|
{
|
195
|
if( !$this->field_exists($field_name, $table_name) )
|
196
|
{ // add new field into a table
|
197
|
$sql = 'ALTER TABLE `'.$table_name.'` ADD '.$field_name.' '.$description.' ';
|
198
|
$query = $this->query($sql);
|
199
|
$this->set_error(mysql_error());
|
200
|
if( !$this->is_error() )
|
201
|
{
|
202
|
return ( $this->field_exists($field_name, $table_name) ) ? true : false;
|
203
|
}
|
204
|
}else
|
205
|
{
|
206
|
$this->set_error('field \''.$field_name.'\' already exists');
|
207
|
}
|
208
|
return false;
|
209
|
}
|
210
|
|
211
|
/*
|
212
|
* @param string $table_name: full name of the table (incl. TABLE_PREFIX)
|
213
|
* @param string $field_name: name of the field to add
|
214
|
* @param string $description: describes the new field like ( INT NOT NULL DEFAULT '0')
|
215
|
* @return bool: true if successful, otherwise false and error will be set
|
216
|
*/
|
217
|
public function field_modify($table_name, $field_name, $description)
|
218
|
{
|
219
|
$retval = false;
|
220
|
if( $this->field_exists($field_name, $table_name) )
|
221
|
{ // modify a existing field in a table
|
222
|
$sql = 'ALTER TABLE `'.$table_name.'` DROP `'.$field_name.'`';
|
223
|
}
|
224
|
}
|
225
|
|
226
|
/*
|
227
|
* @param string $table_name: full name of the table (incl. TABLE_PREFIX)
|
228
|
* @param string $field_name: name of the field to remove
|
229
|
* @return bool: true if successful, otherwise false and error will be set
|
230
|
*/
|
231
|
public function field_remove($table_name, $field_name)
|
232
|
{
|
233
|
$retval = false;
|
234
|
if( $this->field_exists($field_name, $table_name) )
|
235
|
{ // modify a existing field in a table
|
236
|
$sql = 'ALTER TABLE `'.$table_name.'` DROP `'.$field_name.'`';
|
237
|
$retval = ( $this->query($sql) ? true : false );
|
238
|
}
|
239
|
return $retval;
|
240
|
}
|
241
|
|
242
|
/*
|
243
|
* @param string $table_name: full name of the table (incl. TABLE_PREFIX)
|
244
|
* @param string $index_name: name of the new index
|
245
|
* @param string $field_list: comma seperated list of fields for this index
|
246
|
* @param string $index_type: kind of index (UNIQUE, PRIMARY, '')
|
247
|
* @return bool: true if successful, otherwise false and error will be set
|
248
|
*/
|
249
|
public function index_add($table_name, $index_name, $field_list, $index_type = '')
|
250
|
{
|
251
|
$retval = false;
|
252
|
$field_list = str_replace(' ', '', $field_list);
|
253
|
$field_list = explode(',', $field_list);
|
254
|
$number_fields = sizeof($field_list);
|
255
|
$field_list = '`'.implode('`,`', $field_list).'`';
|
256
|
if( $this->index_exists($table_name, $index_name, $number_fields) ||
|
257
|
$this->index_exists($table_name, $index_name))
|
258
|
{
|
259
|
$sql = 'ALTER TABLE `'.$table_name.'` ';
|
260
|
$sql .= 'DROP INDEX `'.$index_name.'`';
|
261
|
if( $this->query($sql))
|
262
|
{
|
263
|
$sql = 'ALTER TABLE `'.$table_name.'` ';
|
264
|
$sql .= 'ADD '.$index_type.' `'.$index_name.'` ( '.$field_list.' ); ';
|
265
|
if( $this->query($sql)) { $retval = true; }
|
266
|
}
|
267
|
}
|
268
|
return $retval;
|
269
|
}
|
270
|
|
271
|
/*
|
272
|
* @param string $table_name: full name of the table (incl. TABLE_PREFIX)
|
273
|
* @param string $field_name: name of the field to remove
|
274
|
* @return bool: true if successful, otherwise false and error will be set
|
275
|
*/
|
276
|
public function index_remove($table_name, $index_name)
|
277
|
{
|
278
|
$retval = false;
|
279
|
if( $this->index_exists($table_name, $index_name) )
|
280
|
{ // modify a existing field in a table
|
281
|
$sql = 'ALTER TABLE `'.$table_name.'` DROP INDEX `'.$index_name.'`';
|
282
|
$retval = ( $this->query($sql) ? true : false );
|
283
|
}
|
284
|
return $retval;
|
285
|
}
|
286
|
|
287
|
} /// end of class database
|
288
|
|
289
|
class mysql {
|
290
|
|
291
|
// Run a query
|
292
|
function query($statement) {
|
293
|
$this->result = mysql_query($statement);
|
294
|
$this->error = mysql_error();
|
295
|
return $this->result;
|
296
|
}
|
297
|
|
298
|
// Fetch num rows
|
299
|
function numRows() {
|
300
|
return mysql_num_rows($this->result);
|
301
|
}
|
302
|
|
303
|
// Fetch row $typ = MYSQL_ASSOC, MYSQL_NUM, MYSQL_BOTH
|
304
|
function fetchRow($typ = MYSQL_BOTH) {
|
305
|
return mysql_fetch_array($this->result, $typ);
|
306
|
}
|
307
|
|
308
|
// Get error
|
309
|
function error() {
|
310
|
if(isset($this->error)) {
|
311
|
return $this->error;
|
312
|
} else {
|
313
|
return null;
|
314
|
}
|
315
|
}
|
316
|
|
317
|
}
|
318
|
|
319
|
?>
|