1 |
ravilov |
1.1 |
<?PHP // -*-Mode: C++;-*- |
2 |
|
|
/* |
3 |
|
|
Bill Adams (bill<at>evil<dot>inetarena<dot>com) |
4 |
|
|
DBD::mysql -- The MySQL Driver. |
5 |
|
|
See: http://evil.inetarena.com/php/DBI.php for more info. |
6 |
|
|
|
7 |
|
|
Licence: LGPL (http://www.gnu.org/). |
8 |
|
|
|
9 |
|
|
This software is provided "AS IS" with no warrenty either implicit, |
10 |
|
|
explicit or otherwise. Use at your own risk. |
11 |
|
|
|
12 |
|
|
17 Mar 2000 baa Moved the DBD stuff into their own class files. |
13 |
|
|
07 May 2000 baa Added rows( ) function. |
14 |
|
|
|
15 |
|
|
*/ |
16 |
|
|
|
17 |
|
|
class DBD_mysql { |
18 |
|
|
|
19 |
|
|
var $is_persisent; |
20 |
|
|
var $DBI; |
21 |
|
|
var $exit_on_fail = 0; |
22 |
|
|
|
23 |
|
|
function DBD_connect( $db_name, $db_host = '', |
24 |
|
|
$db_user = '', $db_passwd = '', |
25 |
|
|
$is_persistent = 0){ |
26 |
|
|
|
27 |
|
|
//Track the $is_persistant just for fun... |
28 |
|
|
$is_persistent = $is_persistent ? $is_persistent : 0; |
29 |
|
|
$this->is_persistent = $is_persistent; |
30 |
|
|
|
31 |
|
|
//DEBUG... |
32 |
|
|
//echo "DBD::mysql: [$db_name][$db_host][$db_user][$is_persistent]<br>\n"; |
33 |
|
|
|
34 |
|
|
|
35 |
|
|
if( $is_persistent ){ |
36 |
|
|
$conn = mysql_pconnect( $db_host, $db_user, $db_passwd ); |
37 |
|
|
} else { |
38 |
|
|
$conn = mysql_connect( $db_host, $db_user, $db_passwd ); |
39 |
|
|
} |
40 |
|
|
|
41 |
|
|
if( !$conn ){ |
42 |
|
|
$this->errstr = "DBD::mysql Error: Could not connect to the database."; |
43 |
|
|
return; |
44 |
|
|
} |
45 |
|
|
|
46 |
|
|
if( ! mysql_select_db( $db_name )){ |
47 |
|
|
$this->errstr = "DBD::mysql Error: Could not select the database.<br>\n"; |
48 |
|
|
return; |
49 |
|
|
} |
50 |
|
|
$this->dbh = $conn; |
51 |
|
|
return( $this ); |
52 |
|
|
}//end connect function |
53 |
|
|
|
54 |
|
|
|
55 |
|
|
|
56 |
|
|
function disconnect( ){ |
57 |
|
|
mysql_close( $this->dbh ); |
58 |
|
|
$this->dbh = undef; |
59 |
|
|
return( 1 ); |
60 |
|
|
}//end disconnect function |
61 |
|
|
|
62 |
|
|
|
63 |
|
|
function prepare( $query ){ |
64 |
|
|
if( ! $this->dbh ){ return; } |
65 |
|
|
//Does nothing but save the query... |
66 |
|
|
$sth = new STH_mysql; |
67 |
|
|
$sth->DBH( $this ); |
68 |
|
|
$sth->prepare( $query ); |
69 |
|
|
return( $sth ); |
70 |
|
|
}//end dbh prepare fn |
71 |
|
|
|
72 |
|
|
|
73 |
|
|
//do is a reserved word so I have to name this something else. |
74 |
|
|
function dbh_do( $query ){ |
75 |
|
|
//run the query and return the number of affected rows. |
76 |
|
|
$result = mysql_query( $query, $this->dbh ); |
77 |
|
|
if( ! $result ){ |
78 |
|
|
$this->errstr = "DBI Error: 'dbh_do' failed: ". |
79 |
|
|
mysql_error( $this->dbh ); |
80 |
|
|
|
81 |
|
|
if( $this->exit_on_fail ){ |
82 |
|
|
echo $this->errstr."<br>\n"; |
83 |
|
|
$this->disconnect( ); |
84 |
|
|
exit( ); } |
85 |
|
|
|
86 |
|
|
return; |
87 |
|
|
} |
88 |
|
|
$rows = mysql_affected_rows( $this->dbh ); |
89 |
|
|
if( $rows == 0 ){ $rows = '0E0'; } |
90 |
|
|
return( $rows ); |
91 |
|
|
}//end fn do |
92 |
|
|
|
93 |
|
|
|
94 |
|
|
function insert_id( ){ |
95 |
|
|
//Get the last serial number from an insert. |
96 |
|
|
return( mysql_insert_id( $this->dbh )); |
97 |
|
|
}//end fn do |
98 |
|
|
|
99 |
|
|
|
100 |
|
|
function quote( $string ){ |
101 |
|
|
if( $string ){ |
102 |
|
|
return( "'".addslashes( $string )."'" ); |
103 |
|
|
} else { |
104 |
|
|
return( 'NULL' ); |
105 |
|
|
} |
106 |
|
|
}//end fn quote |
107 |
|
|
|
108 |
|
|
|
109 |
|
|
}//end DBI class |
110 |
|
|
|
111 |
|
|
|
112 |
|
|
//=========================================================== |
113 |
|
|
|
114 |
|
|
class STH_mysql { |
115 |
|
|
|
116 |
|
|
var $query; |
117 |
|
|
var $DBH; |
118 |
|
|
var $dbh; |
119 |
|
|
var $result; |
120 |
|
|
|
121 |
|
|
function STH( $dbh = '' ){ |
122 |
|
|
if( $dbh ){ |
123 |
|
|
$this->dbh = $dbh; |
124 |
|
|
} |
125 |
|
|
return( $this ); |
126 |
|
|
} |
127 |
|
|
|
128 |
|
|
|
129 |
|
|
function DBH( $dbh ){ |
130 |
|
|
$this->DBH = $dbh; //watch out, this copies the object not the reference. |
131 |
|
|
$this->dbh = $dbh->dbh; |
132 |
|
|
return( $dbh ); |
133 |
|
|
} |
134 |
|
|
|
135 |
|
|
|
136 |
|
|
function prepare( $query ){ |
137 |
|
|
if( ! $this->dbh ){ return; } |
138 |
|
|
$this->query = $query; |
139 |
|
|
return( $query ); |
140 |
|
|
} |
141 |
|
|
|
142 |
|
|
|
143 |
|
|
function execute( ){ |
144 |
|
|
if( ! $this->dbh ){ return; } |
145 |
|
|
//echo "Running query $this->query<br>\n"; |
146 |
|
|
$this->result = mysql_query( $this->query, $this->dbh ); |
147 |
|
|
|
148 |
|
|
if( ! $this->result ){ |
149 |
|
|
$this->errstr = "STH Execute failed: ".mysql_error( $this->dbh ); |
150 |
|
|
return; |
151 |
|
|
} |
152 |
|
|
|
153 |
|
|
return( 1 ); |
154 |
|
|
}//end execute fn |
155 |
|
|
|
156 |
|
|
|
157 |
|
|
function finish( ){ |
158 |
|
|
if( ! $this->result ){ return; } |
159 |
|
|
mysql_free_result( $this->result ); |
160 |
|
|
$this->result = undef; |
161 |
|
|
return( 1 ); |
162 |
|
|
}//end execute fn |
163 |
|
|
|
164 |
|
|
|
165 |
|
|
function rows( ){ |
166 |
|
|
if( ! $this->dbh ){ return; } |
167 |
|
|
return mysql_num_rows( $this->result ); |
168 |
|
|
} |
169 |
|
|
|
170 |
|
|
|
171 |
|
|
function fetchrow_array( ){ |
172 |
|
|
if( ! $this->result ){ |
173 |
|
|
//echo "STH Error: Calling fetchrow on null result.<br>\n"; |
174 |
|
|
return; |
175 |
|
|
} |
176 |
|
|
return( mysql_fetch_row( $this->result )); |
177 |
|
|
} |
178 |
|
|
|
179 |
|
|
|
180 |
|
|
function fetchrow_hash( ){ |
181 |
|
|
if( ! $this->result ){ |
182 |
|
|
//echo "STH Error: Calling fetchrow on null result.<br>\n"; |
183 |
|
|
return; |
184 |
|
|
} |
185 |
|
|
|
186 |
|
|
return( mysql_fetch_array( $this->result )); |
187 |
|
|
} |
188 |
|
|
} |
189 |
|
|
|
190 |
|
|
|
191 |
|
|
$GLOBALS[classDBDmysql_read] = 1; |