Project

General

Profile

1 4 ryan
<?php
2
3 19 stefan
// $Id$
4 4 ryan
5
/*
6
7
 Website Baker Project <http://www.websitebaker.org/>
8 915 Ruebenwurz
 Copyright (C) 2004-2009, Ryan Djurovich
9 4 ryan
10
 Website Baker is free software; you can redistribute it and/or modify
11
 it under the terms of the GNU General Public License as published by
12
 the Free Software Foundation; either version 2 of the License, or
13
 (at your option) any later version.
14
15
 Website Baker is distributed in the hope that it will be useful,
16
 but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 GNU General Public License for more details.
19
20
 You should have received a copy of the GNU General Public License
21
 along with Website Baker; if not, write to the Free Software
22
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23
24
*/
25
26
/*
27
28
Database class
29
30
This class will be used to interface between the database
31
and the Website Baker code
32
33
*/
34
35
// Stop this file from being accessed directly
36 19 stefan
if(!defined('WB_URL')) {
37
	header('Location: ../index.php');
38 286 stefan
	exit(0);
39 19 stefan
}
40 4 ryan
41
if(!defined('DB_URL')) {
42
	//define('DB_URL', DB_TYPE.'://'.DB_USERNAME.':'.DB_PASSWORD.'@'.DB_HOST.'/'.DB_NAME);
43
}
44
45
define('DATABASE_CLASS_LOADED', true);
46
47
class database {
48
49
	// Set DB_URL
50
	function database($url = '') {
51
		// Connect to database
52
		$this->connect();
53
		// Check for database connection error
54
		if($this->is_error()) {
55
			die($this->get_error());
56
		}
57
	}
58
59
	// Connect to the database
60
	function connect() {
61
		$status = $this->db_handle = mysql_connect(DB_HOST, DB_USERNAME, DB_PASSWORD);
62
		if(mysql_error()) {
63
			$this->connected = false;
64
			$this->error = mysql_error();
65
		} else {
66
			if(!mysql_select_db(DB_NAME)) {
67
				$this->connected = false;
68
				$this->error = mysql_error();
69
			} else {
70
				$this->connected = true;
71
			}
72
		}
73
		return $this->connected;
74
	}
75
76
	// Disconnect from the database
77
	function disconnect() {
78 95 stefan
		if($this->connected==true) {
79 4 ryan
			mysql_close();
80
			return true;
81
		} else {
82
			return false;
83
		}
84
	}
85
86
	// Run a query
87
	function query($statement) {
88
		$mysql = new mysql();
89
		$mysql->query($statement);
90 342 stefan
		$this->set_error($mysql->error());
91 4 ryan
		if($mysql->error()) {
92
			return null;
93
		} else {
94
			return $mysql;
95
		}
96
	}
97
98
	// Gets the first column of the first row
99
	function get_one($statement) {
100
		$fetch_row = mysql_fetch_row(mysql_query($statement));
101
		$result = $fetch_row[0];
102 342 stefan
		$this->set_error(mysql_error());
103 4 ryan
		if(mysql_error()) {
104
			return null;
105
		} else {
106
			return $result;
107
		}
108
	}
109
110
	// Set the DB error
111
	function set_error($message = null) {
112
		global $TABLE_DOES_NOT_EXIST, $TABLE_UNKNOWN;
113
		$this->error = $message;
114
		if(strpos($message, 'no such table')) {
115
			$this->error_type = $TABLE_DOES_NOT_EXIST;
116
		} else {
117
			$this->error_type = $TABLE_UNKNOWN;
118
		}
119
	}
120
121
	// Return true if there was an error
122
	function is_error() {
123
		return (!empty($this->error)) ? true : false;
124
	}
125
126
	// Return the error
127
	function get_error() {
128
		return $this->error;
129
	}
130
131
}
132
133
class mysql {
134
135
	// Run a query
136
	function query($statement) {
137
		$this->result = mysql_query($statement);
138
		$this->error = mysql_error();
139
		return $this->result;
140
	}
141
142
	// Fetch num rows
143
	function numRows() {
144
		return mysql_num_rows($this->result);
145
	}
146
147
	// Fetch row
148
	function fetchRow() {
149
		return mysql_fetch_array($this->result);
150
	}
151
152
	// Get error
153
	function error() {
154
		if(isset($this->error)) {
155
			return $this->error;
156
		} else {
157
			return null;
158
		}
159
	}
160
161
}
162
163
?>