1 |
<? |
2 |
|
3 |
/* |
4 |
* $Id: dbi_mysql.php,v 1.5 2001/04/03 14:48:31 cfsl Exp $ |
5 |
* |
6 |
* This class is based on the Perl DBI. |
7 |
* New functionality should be added accordingly. |
8 |
* Please refer to the DBI documentation (perldoc DBI). |
9 |
* |
10 |
* This program 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 |
* This program 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 this program; if not, write to the Free Software |
22 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
23 |
*/ |
24 |
|
25 |
class DBI { |
26 |
// Unauthorized |
27 |
var $Driver_name = "mySQL"; |
28 |
var $Name; |
29 |
|
30 |
var $session; |
31 |
var $debug = 0; |
32 |
|
33 |
var $err; |
34 |
var $errstr=""; |
35 |
var $RaiseError = 1; |
36 |
|
37 |
function DBI ($db, $user, $password) { |
38 |
$this->session = mysql_pconnect('localhost', $user, $password); |
39 |
mysql_select_db($db,$this->session); |
40 |
$this->Name = $db; |
41 |
} |
42 |
|
43 |
function prepare ($query) { |
44 |
return new STH($this, $this->session, $query, $this->debug); |
45 |
} |
46 |
|
47 |
function dbh_do($query) { |
48 |
// --OBS-- returns -1 on failure, 0 is a valid return from this function! |
49 |
$res = mysql_query($query, $this-> session); |
50 |
if ($res) { |
51 |
$this->err = 0; |
52 |
$this->errstr = ""; |
53 |
return mysql_affected_rows($this->session); |
54 |
} else { |
55 |
$this->err = mysql_errno($this->session); |
56 |
$this->errstr = mysql_error($this->session); |
57 |
return -1; |
58 |
} |
59 |
} |
60 |
|
61 |
function insert_id() { |
62 |
return mysql_insert_id($this->session); |
63 |
} |
64 |
|
65 |
function autocommit ($value) { |
66 |
echo "<br><b>Warning: Transactions not supported by this driver.</b><br>"; |
67 |
return 0; |
68 |
} |
69 |
|
70 |
function commit () { |
71 |
echo "<br><b>Warning: Transactions not supported by this driver.</b><br>"; |
72 |
return 0; |
73 |
} |
74 |
|
75 |
function rollback () { |
76 |
echo "<br><b>Warning: Transactions not supported by this driver.</b><br>"; |
77 |
return 0; |
78 |
} |
79 |
|
80 |
function quote ($str) { |
81 |
if (get_magic_quotes_gpc()) { |
82 |
$str = stripslashes($str); |
83 |
} |
84 |
return "'".AddSlashes($str)."'"; |
85 |
} |
86 |
} |
87 |
|
88 |
class STH { |
89 |
var $query; |
90 |
var $debug; |
91 |
var $session; |
92 |
var $res; |
93 |
var $row; |
94 |
var $placeholders; |
95 |
var $dbi; |
96 |
|
97 |
function STH (&$dbi, $session, $query, $debug) { |
98 |
$this->dbi = &$dbi; |
99 |
$this->session = $session; |
100 |
$this->query = $query; |
101 |
$this->debug = $debug; |
102 |
|
103 |
// Scan for placeholders |
104 |
|
105 |
$this->placeholders = array(); |
106 |
$quote = ''; |
107 |
for ($i = 0; $i < strlen($query); ++$i) { |
108 |
if ($query[$i] == "'") { |
109 |
if (empty($quote)) { |
110 |
$quote = "'"; |
111 |
} elseif ($quote == "'") { |
112 |
$quote = ''; |
113 |
} |
114 |
} elseif ($query[$i] == '"') { |
115 |
if (empty($quote)) { |
116 |
$quote = '"'; |
117 |
} elseif ($quote == '"') { |
118 |
$quote = ''; |
119 |
} |
120 |
} elseif ($query[$i] == '?') { |
121 |
if (empty($quote)) { |
122 |
array_push($this->placeholders, $i); |
123 |
} |
124 |
} |
125 |
} |
126 |
} |
127 |
|
128 |
function execute () { |
129 |
global $SERVER_NAME; |
130 |
|
131 |
$numargs = func_num_args(); |
132 |
$arg_list = func_get_args(); |
133 |
|
134 |
$parms = array(); |
135 |
|
136 |
for ($i = 0; $i < $numargs; $i++) { |
137 |
if (is_array($arg_list[$i])) { |
138 |
while (list($dummy,$parm) = each ($arg_list[$i])) { |
139 |
array_push($parms, $parm); |
140 |
} |
141 |
} else { |
142 |
array_push($parms,$arg_list[$i]); |
143 |
} |
144 |
} |
145 |
|
146 |
if (sizeof($parms) != sizeof($this->placeholders)) { |
147 |
print "<br><b>SQL Query contains ".sizeof($this->placeholders)." placeholders but ".sizeof($parms)." was passed</b><br>"; |
148 |
exit; |
149 |
} |
150 |
|
151 |
if (sizeof($parms) > 0) { |
152 |
$query = substr($this->query, 0, $this->placeholders[0]); |
153 |
for ($i = 0; $i < sizeof($parms) - 1; ++$i) { |
154 |
$query .= $this->dbi->quote($parms[$i]) . substr($this->query, $this->placeholders[$i] + 1, $this->placeholders[$i + 1] - $this->placeholders[$i] - 1); |
155 |
} |
156 |
$query .= $this->dbi->quote($parms[$i]) . substr($this->query, $this->placeholders[$i] + 1); |
157 |
} else { |
158 |
$query = $this->query; |
159 |
} |
160 |
|
161 |
if ($this->debug) { // Log the query |
162 |
$fd = fopen("/tmp/dbi.$SERVER_NAME.log", "a") or die ("Couldn't append to file"); |
163 |
fputs($fd, $this->query."\n==================\n"); |
164 |
fclose($fd); |
165 |
} |
166 |
|
167 |
$this->res = @mysql_query($query, $this->session); |
168 |
if (!$this->res) { |
169 |
if ($this->dbi->RaiseError) { |
170 |
print "<br><b>Could not execute SQL query: \" ". $query . "\"</b><br>"; |
171 |
print "<br>".mysql_errno($this->session) . " " .mysql_error($this->session)."<br>"; |
172 |
exit; |
173 |
} |
174 |
$this->dbi->errstr = mysql_error($this->session); |
175 |
$this->dbi->err = mysql_errno($this->session); |
176 |
} |
177 |
$this->row = 0; |
178 |
return $this->res; |
179 |
} |
180 |
|
181 |
function fetchrow_array () { |
182 |
return mysql_fetch_row($this->res); |
183 |
$this->row++; |
184 |
} |
185 |
|
186 |
function fetchrow_hash () { |
187 |
return mysql_fetch_array($this->res); |
188 |
$this->row++; |
189 |
} |
190 |
|
191 |
function finish () { |
192 |
mysql_free_result($this->res); |
193 |
} |
194 |
|
195 |
function rows () { |
196 |
return mysql_num_rows($this->res); |
197 |
} |
198 |
} |
199 |
?> |