Project

General

Profile

1
<?php
2
class _database {
3
	private $data		= false;
4
	private $result		= false;
5
	private $row		= false;
6

    
7
	public $settings	= array(
8
			"servername"=> "localhost",
9
			"serverport"=> "3306",
10
			"username"	=> false,
11
			"password"	=> false,
12
			"database"	=> false,
13
			"persist"	=> false,
14
			"dieonerror"=> false,
15
			"showerror"	=> false,
16
			"error_file"=> true
17
		);
18

    
19
	function __construct() {
20
		global $db_config;
21
		$this->settings = array_merge($this->settings, $db_config);
22
		if($this->settings["error_file"] === true) $this->settings["error_file"] = dirname(__FILE__)."/__mysql_errors.log";
23
	}
24

    
25
	function connect() {
26
		$this->data = new mysqli(
27
			$this->settings["servername"], 
28
			$this->settings["username"], 
29
			$this->settings["password"], 
30
			$this->settings["database"],
31
			$this->settings["serverport"]
32
		);
33

    
34
		if(mysqli_connect_errno()) { 
35
			$this->error("Connection error: ".mysqli_connect_error() ); 
36
			return false; 
37
		}
38
		if(!$this->data->set_charset("utf8")) {
39
			$this->error("Error loading character set utf8");
40
			return false;
41
		}
42
		return true;
43
	}
44

    
45
	function query($sql) {
46
		if(!$this->data && !$this->connect()) {
47
			$this->error("Could node connect for query: ".$sql);
48
			return false;
49
		}
50
		//echo $sql."<br />:";
51
		if(!($this->result = $this->data->query($sql))) $this->error($sql);
52
		return ($this->result) ? true : false;
53
	}
54
	
55
	function nextr(){
56
		if(!$this->result) {
57
			$this->error("No query pending");
58
			return false;
59
		}
60
		unset($this->row);
61
		$this->row = $this->result->fetch_array(MYSQL_BOTH);
62
		return ($this->row) ? true : false ;
63
	}
64

    
65
	function get_row($mode = "both") {
66
		if(!$this->row) return false;
67

    
68
		$return = array();
69
		switch($mode) {
70
			case "assoc":
71
				foreach($this->row as $k => $v) {
72
					if(!is_int($k)) $return[$k] = $v;
73
				}
74
				break;
75
			case "num":
76
				foreach($this->row as $k => $v) {
77
					if(is_int($k)) $return[$k] = $v;
78
				}
79
				break;
80
			default:
81
				$return = $this->row;
82
				break;
83
		}
84
		return array_map("stripslashes",$return);
85
	}
86

    
87
	function get_all($mode = "both", $key = false) {
88
		if(!$this->result) {
89
			$this->error("No query pending");
90
			return false;
91
		}
92
		$return = array();
93
		while($this->nextr()) {
94
			if($key !== false) $return[$this->f($key)] = $this->get_row($mode);
95
			else $return[] = $this->get_row($mode);
96
		}
97
		return $return;
98
	}
99

    
100
	function f($index) {
101
		return stripslashes($this->row[$index]);
102
	}
103

    
104
	function go_to($row) {
105
		if(!$this->result) {
106
			$this->error("No query pending");
107
			return false;
108
		}
109
		if(!$this->data->data_seek($row)) $this->error();
110
	}
111

    
112
	function nf() {
113
		if (!$this->result) {
114
			$this->error("nf: no result set");
115
			return false;
116
		}
117
		return $this->result->num_rows;
118
	}
119
	function af() {
120
		return $this->data->affected_rows;
121
	}
122
	function error($string = "") {
123
		$error = $this->data->error;
124
		if($this->settings["show_error"]) echo $error;
125
		if($this->settings["error_file"] !== false) {
126
			$handle = @fopen($this->settings["error_file"], "a+");
127
			if($handle) {
128
				@fwrite($handle, "[".date("Y-m-d H:i:s")."] ".$string." <".$error.">\n");
129
				@fclose($handle);
130
			}
131
		}
132
		if($this->settings["dieonerror"]) {
133
			if(isset($this->result)) $this->result->free();
134
			@$this->data->close();
135
			die();
136
		}
137
	}
138
	function insert_id() {
139
		return $this->data->insert_id;
140
	}
141
	function escape($string) {
142
		if(!$this->data) return addslashes($string);
143
		return $this->data->escape_string($string);
144
	}
145

    
146
	function destroy() {
147
		if(isset($this->result)) $this->result->free();
148
		if($this->data) $this->data->close();
149
	}
150

    
151

    
152
}
(2-2/3)