Project

General

Profile

1
<?php
2

    
3
// $Id: class.database.php 95 2005-09-12 23:08:46Z stefan $
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_URL')) {
37
	header('Location: ../index.php');
38
}
39

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

    
44
define('DATABASE_CLASS_LOADED', true);
45

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

    
132
class mysql {
133

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

    
160
}
161

    
162
?>
(2-2/11)