Project

General

Profile

1
<?php
2

    
3
// $Id: class.database.php 10 2005-09-04 08:59:31Z ryan $
4

    
5
/*
6

    
7
 Website Baker Project <http://www.websitebaker.org/>
8
 Copyright (C) 2004-2005, Ryan Djurovich
9

    
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
if(!defined('WB_PATH')) { exit('Direct access to this file is not allowed'); }
37

    
38
if(!defined('DB_URL')) {
39
	//define('DB_URL', DB_TYPE.'://'.DB_USERNAME.':'.DB_PASSWORD.'@'.DB_HOST.'/'.DB_NAME);
40
}
41

    
42
define('DATABASE_CLASS_LOADED', true);
43

    
44
class database {
45
	
46
	// Set DB_URL
47
	function database($url = '') {
48
		// Connect to database
49
		$this->connect();
50
		// Check for database connection error
51
		if($this->is_error()) {
52
			die($this->get_error());
53
		}
54
	}
55
	
56
	// Connect to the database
57
	function connect() {
58
		$status = $this->db_handle = mysql_connect(DB_HOST, DB_USERNAME, DB_PASSWORD);
59
		if(mysql_error()) {
60
			$this->connected = false;
61
			$this->error = mysql_error();
62
		} else {
63
			if(!mysql_select_db(DB_NAME)) {
64
				$this->connected = false;
65
				$this->error = mysql_error();
66
			} else {
67
				$this->connected = true;
68
			}
69
		}
70
		return $this->connected;
71
	}
72
	
73
	// Disconnect from the database
74
	function disconnect() {
75
		if(isset($this->Database)) {
76
			mysql_close();
77
			return true;
78
		} else {
79
			return false;
80
		}
81
	}
82
	
83
	// Run a query
84
	function query($statement) {
85
		$mysql = new mysql();
86
		$mysql->query($statement);
87
		if($mysql->error()) {
88
			$this->set_error($mysql->error());
89
			return null;
90
		} else {
91
			return $mysql;
92
		}
93
	}
94
	
95
	// Gets the first column of the first row
96
	function get_one($statement) {
97
		$fetch_row = mysql_fetch_row(mysql_query($statement));
98
		$result = $fetch_row[0];
99
		if(mysql_error()) {
100
			$this->set_error(mysql_error());
101
			return null;
102
		} else {
103
			return $result;
104
		}
105
	}
106
	
107
	// Set the DB error
108
	function set_error($message = null) {
109
		global $TABLE_DOES_NOT_EXIST, $TABLE_UNKNOWN;
110
		$this->error = $message;
111
		if(strpos($message, 'no such table')) {
112
			$this->error_type = $TABLE_DOES_NOT_EXIST;
113
		} else {
114
			$this->error_type = $TABLE_UNKNOWN;
115
		}
116
	}
117
	
118
	// Return true if there was an error
119
	function is_error() {
120
		return (!empty($this->error)) ? true : false;
121
	}
122
	
123
	// Return the error
124
	function get_error() {
125
		return $this->error;
126
	}
127
	
128
}
129

    
130
class mysql {
131

    
132
	// Run a query
133
	function query($statement) {
134
		$this->result = mysql_query($statement);
135
		$this->error = mysql_error();
136
		return $this->result;
137
	}
138
	
139
	// Fetch num rows
140
	function numRows() {
141
		return mysql_num_rows($this->result);
142
	}
143
	
144
	// Fetch row
145
	function fetchRow() {
146
		return mysql_fetch_array($this->result);
147
	}
148
	
149
	// Get error
150
	function error() {
151
		if(isset($this->error)) {
152
			return $this->error;
153
		} else {
154
			return null;
155
		}
156
	}
157

    
158
}
159

    
160
?>
(2-2/11)