source: includes/mysql.class.php

Last change on this file was 1, checked in by george, 17 years ago

Prvnotní import původních kódů z wowresource.

File size: 5.0 KB
Line 
1<?php
2class tMySql {
3 var $dbPassword,
4 $dbUser,
5 $dbName,
6 $dbServer,
7 $conn,
8 $result,
9 $err_handler = false,
10 $log_query = false,
11 $insert_addslashes = false;
12
13 function err($r="", $q="", $e="") {
14 if ($this->err_handler) {
15 $err_handler = $this->err_handler;
16 $err_handler($r, $q, $e);
17 } else {
18 echo "<code>MYSQL";
19 if ($r) echo "[$r]";
20 if ($q) echo "Dotaz:$q";
21 if ($e) echo "Chyba:$e";
22 echo "</code><br />";
23 };
24 }
25
26 function connect() {
27 $this->conn = mysql_pconnect($this->dbServer, $this->dbUser, $this->dbPassword) or die(mysql_error());
28 if (!$this->dbName=='')
29 {
30 ($db = mysql_select_db($this->dbName, $this->conn)) or $this->err("", "mysql_select_db", "no db");
31 }
32 return ($this->conn and $db);
33 }
34
35 function query($r, $q) {
36 $sr = mysql_query($q, $this->conn);
37 if ($sr)
38 {
39 if ($r) $this->result[$r] = $sr;
40 if ($this->log_query) err($r, $q);
41 }
42 else
43 {
44 $this->err($r, $q, mysql_error($this->conn));
45 }
46 return($sr);
47 }
48
49 function query_result($q, $line=0, $r=0) {
50 $sr = $this->query(0, $q); // proved sql dotaz
51 if ($sr) { // pokud je nejaky vysledek
52 if (mysql_num_rows($sr) > 0) {
53 if (!$r) $ret=mysql_result($sr, $line);// vrat hodnotu výsledku
54 else $ret=mysql_result($sr, $line, $r);
55 mysql_free_result($sr); // uvolni výsledek
56 return($ret);
57 } else {
58 mysql_free_result($sr); // uvolni výsledek
59 return(null);
60 };
61 } else return(null);
62 }
63
64 function query_result_all($q, $r=0) {
65 $sr = $this->query(0, $q);
66 if ($sr) {
67 $ret = array();
68 $c = mysql_num_rows($sr);
69 for($i = 0; $i < $c; $i++)
70 $ret[] = ($r ? mysql_result($sr, $i, $r) : mysql_result($sr, $i));
71 mysql_free_result($sr);
72 return($ret);
73 } else return(null);
74 }
75
76 function update ($tbl, $cols, $where = "") {
77 $s_set = "";
78 while(list($k, $v) = each($cols)) {
79 if ($s_set != "") $s_set .= ", ";
80 if (ereg('^'.$k.'(.*)$', $v))
81 {
82 $s_set .= "`$k`=".$v;
83 }
84 else
85 {
86 $s_set .= "`$k`='".($this->insert_addslashes ? addslashes($v) : $v)."'";
87 }
88 };
89 $this->query("", "UPDATE $tbl SET $s_set ".($where != "" ? "WHERE $where" : ""));
90 }
91
92 function insert ($tbl, $cols) {
93 $s_set = "";
94 while(list($k, $v) = each($cols)) {
95 if ($s_set != "") $s_set .= ", ";
96 if (ereg('^'.$k.'(.*)$', $v))
97 {
98 $s_set .= "`$k`=".$v;
99 }
100 else
101 {
102 $s_set .= "`$k`='".($this->insert_addslashes ? addslashes($v) : $v)."'";
103 }
104 };
105 $this->query(0, "INSERT INTO $tbl SET $s_set ");
106 }
107
108 function replace ($tbl, $cols) {
109 $s_set = "";
110 while(list($k, $v) = each($cols)) {
111 if ($s_set != "") $s_set .= ", ";
112 $s_set .= "$k='".($this->insert_addslashes ? addslashes($v) : $v)."'";
113 };
114 $this->query("", "REPLACE $tbl SET $s_set");
115 }
116
117 function fetch_assoc($r){
118 if ($this->result[$r]) return (mysql_fetch_assoc ($this->result[$r]));
119 else $this->err($r, "", "no result");
120 }
121
122 function query_fetch_assoc($q){
123 $sr = $this->query(0, $q);
124 if ($sr) {
125 $ret = mysql_fetch_assoc($sr);
126 mysql_free_result($sr);
127 return($ret);
128 } else return(null);
129 }
130
131 function query_fetch_assoc_all($q, $index=0){
132 $sr = $this->query(0, $q);
133 while($row = mysql_fetch_assoc ($sr)) {
134 if ($index) $ret[$row[$index]] = $row;
135 else $ret[] = $row;
136 };
137 return($ret);
138 }
139
140 function num_rows($r){
141 if ($this->result[$r]) return (mysql_num_rows ($this->result[$r]));
142 else $this->err($r, "->num_rows", "no '$r' result");
143 }
144
145 function result($r, $f, $row = 0){
146 if ($this->result[$r]) return mysql_result ($this->result[$r], $row, $f);
147 else $this->err($r, "->result", "no '$r' result");
148 }
149
150 function free_result($r){
151 if ($this->result[$r]) mysql_free_result ($this->result[$r]);
152 unset($this->result[$r]);
153 }
154
155 function close() {
156 mysql_close($this->conn);
157 }
158
159 function insert_id(){
160 return mysql_insert_id ($this->conn);
161 }
162
163 function conf($a) {
164 while(list($k, $v) = each($a)) {
165 $this->$k = $v;
166 };
167 }
168 function escape($thing)
169 {
170 if (is_array($thing))
171 {
172 $escaped = array();
173 foreach ($thing as $key => $value)
174 {
175 $escaped[$key] = $this->escape($value);
176 }
177 return $escaped;
178 }
179 return mysql_real_escape_string($thing);
180}
181
182 function strip($thing)
183 {
184 if (is_array($thing))
185 {
186 $escaped = array();
187 foreach ($thing as $key => $value)
188 {
189 $escaped[$key] = $this->strip($value);
190 }
191 return $escaped;
192 }
193 return stripslashes($thing);
194}
195
196};
197
198?>
Note: See TracBrowser for help on using the repository browser.