Rev Author Line No. Line
250 kaklik 1 <?php
2 /* $Id: mysql.dbi.lib.php,v 2.43 2006/01/17 17:03:02 cybot_tm Exp $ */
3 // vim: expandtab sw=4 ts=4 sts=4:
4  
5 /**
6 * Interface to the classic MySQL extension
7 */
8  
9 // MySQL client API
10 if (!defined('PMA_MYSQL_CLIENT_API')) {
11 if (function_exists('mysql_get_client_info')) {
12 $client_api = explode('.', mysql_get_client_info());
13 define('PMA_MYSQL_CLIENT_API', (int)sprintf('%d%02d%02d', $client_api[0], $client_api[1], intval($client_api[2])));
14 unset($client_api);
15 } else {
16 define('PMA_MYSQL_CLIENT_API', 32332); // always expect the worst...
17 }
18 }
19  
20 function PMA_DBI_connect($user, $password, $is_controluser = FALSE) {
21 global $cfg, $php_errormsg;
22  
23 $server_port = (empty($cfg['Server']['port']))
24 ? ''
25 : ':' . $cfg['Server']['port'];
26  
27 if (strtolower($cfg['Server']['connect_type']) == 'tcp') {
28 $cfg['Server']['socket'] = '';
29 }
30  
31 $server_socket = (empty($cfg['Server']['socket']))
32 ? ''
33 : ':' . $cfg['Server']['socket'];
34  
35 if (PMA_PHP_INT_VERSION >= 40300 && PMA_MYSQL_CLIENT_API >= 32349) {
36 $client_flags = $cfg['Server']['compress'] && defined('MYSQL_CLIENT_COMPRESS') ? MYSQL_CLIENT_COMPRESS : 0;
37 // always use CLIENT_LOCAL_FILES as defined in mysql_com.h
38 // for the case where the client library was not compiled
39 // with --enable-local-infile
40 $client_flags |= 128;
41 }
42  
43 if (empty($client_flags)) {
44 $connect_func = 'mysql_' . ($cfg['PersistentConnections'] ? 'p' : '') . 'connect';
45 $link = @$connect_func($cfg['Server']['host'] . $server_port . $server_socket, $user, $password);
46 } else {
47 if ($cfg['PersistentConnections']) {
48 $link = @mysql_pconnect($cfg['Server']['host'] . $server_port . $server_socket, $user, $password, $client_flags);
49 } else {
50 $link = @mysql_connect($cfg['Server']['host'] . $server_port . $server_socket, $user, $password, FALSE, $client_flags);
51 }
52 }
53  
54 if (empty($link)) {
55 PMA_auth_fails();
56 } // end if
57  
58 PMA_DBI_postConnect($link, $is_controluser);
59  
60 return $link;
61 }
62  
63 function PMA_DBI_select_db($dbname, $link = null) {
64 if (empty($link)) {
65 if (isset($GLOBALS['userlink'])) {
66 $link = $GLOBALS['userlink'];
67 } else {
68 return FALSE;
69 }
70 }
71 if (PMA_MYSQL_INT_VERSION < 40100) {
72 $dbname = PMA_convert_charset($dbname);
73 }
74 return mysql_select_db($dbname, $link);
75 }
76  
77 function PMA_DBI_try_query($query, $link = null, $options = 0) {
78 if (empty($link)) {
79 if (isset($GLOBALS['userlink'])) {
80 $link = $GLOBALS['userlink'];
81 } else {
82 return FALSE;
83 }
84 }
85 if (defined('PMA_MYSQL_INT_VERSION') && PMA_MYSQL_INT_VERSION < 40100) {
86 $query = PMA_convert_charset($query);
87 }
88 if ($options == ($options | PMA_DBI_QUERY_STORE)) {
89 return @mysql_query($query, $link);
90 } elseif ($options == ($options | PMA_DBI_QUERY_UNBUFFERED)) {
91 return @mysql_unbuffered_query($query, $link);
92 } else {
93 return @mysql_query($query, $link);
94 }
95 }
96  
97 // The following function is meant for internal use only.
98 // Do not call it from outside this library!
99 function PMA_mysql_fetch_array($result, $type = FALSE) {
100 global $cfg, $allow_recoding, $charset, $convcharset;
101  
102 if ($type != FALSE) {
103 $data = mysql_fetch_array($result, $type);
104 } else {
105 $data = mysql_fetch_array($result);
106 }
107  
108 /* No data returned => do not touch it */
109 if (! $data) {
110 return $data;
111 }
112  
113 if (!defined('PMA_MYSQL_INT_VERSION') || PMA_MYSQL_INT_VERSION >= 40100
114 || !(isset($cfg['AllowAnywhereRecoding']) && $cfg['AllowAnywhereRecoding'] && $allow_recoding)) {
115 /* No recoding -> return data as we got them */
116 return $data;
117 } else {
118 $ret = array();
119 $num = mysql_num_fields($result);
120 $i = 0;
121 for ($i = 0; $i < $num; $i++) {
122 $name = mysql_field_name($result, $i);
123 $flags = mysql_field_flags($result, $i);
124 /* Field is BINARY (either marked manually, or it is BLOB) => do not convert it */
125 if (stristr($flags, 'BINARY')) {
126 if (isset($data[$i])) {
127 $ret[$i] = $data[$i];
128 }
129 if (isset($data[$name])) {
130 $ret[PMA_convert_display_charset($name)] = $data[$name];
131 }
132 } else {
133 if (isset($data[$i])) {
134 $ret[$i] = PMA_convert_display_charset($data[$i]);
135 }
136 if (isset($data[$name])) {
137 $ret[PMA_convert_display_charset($name)] = PMA_convert_display_charset($data[$name]);
138 }
139 }
140 }
141 return $ret;
142 }
143 }
144  
145 function PMA_DBI_fetch_array($result) {
146 return PMA_mysql_fetch_array($result);
147 }
148  
149 function PMA_DBI_fetch_assoc($result) {
150 return PMA_mysql_fetch_array($result, MYSQL_ASSOC);
151 }
152  
153 function PMA_DBI_fetch_row($result) {
154 return PMA_mysql_fetch_array($result, MYSQL_NUM);
155 }
156  
157 /**
158 * Frees the memory associated with the results
159 *
160 * @param result $result,... one or more mysql result resources
161 */
162 function PMA_DBI_free_result() {
163 foreach ( func_get_args() as $result ) {
164 if ( is_resource($result)
165 && get_resource_type($result) === 'mysql result' ) {
166 mysql_free_result($result);
167 }
168 }
169 }
170  
171 /**
172 * Returns a string representing the type of connection used
173 * @uses mysql_get_host_info()
174 * @uses $GLOBALS['userlink'] as default for $link
175 * @param resource $link mysql link
176 * @return string type of connection used
177 */
178 function PMA_DBI_get_host_info($link = null)
179 {
180 if (null === $link) {
181 if (isset($GLOBALS['userlink'])) {
182 $link = $GLOBALS['userlink'];
183 } else {
184 return false;
185 }
186 }
187 return mysql_get_host_info($link);
188 }
189  
190 /**
191 * Returns the version of the MySQL protocol used
192 * @uses mysql_get_proto_info()
193 * @uses $GLOBALS['userlink'] as default for $link
194 * @param resource $link mysql link
195 * @return integer version of the MySQL protocol used
196 */
197 function PMA_DBI_get_proto_info($link = null)
198 {
199 if (null === $link) {
200 if (isset($GLOBALS['userlink'])) {
201 $link = $GLOBALS['userlink'];
202 } else {
203 return false;
204 }
205 }
206 return mysql_get_proto_info($link);
207 }
208  
209 /**
210 * returns a string that represents the client library version
211 * @uses mysql_get_client_info()
212 * @return string MySQL client library version
213 */
214 function PMA_DBI_get_client_info() {
215 return mysql_get_client_info();
216 }
217  
218 /**
219 * returns last error message or false if no errors occured
220 *
221 * @uses PMA_MYSQL_INT_VERSION
222 * @uses PMA_convert_display_charset()
223 * @uses PMA_DBI_convert_message()
224 * @uses $GLOBALS['errno']
225 * @uses $GLOBALS['userlink']
226 * @uses $GLOBALS['strServerNotResponding']
227 * @uses $GLOBALS['strSocketProblem']
228 * @uses mysql_errno()
229 * @uses mysql_error()
230 * @uses defined()
231 * @param resource $link mysql link
232 * @return string|boolean $error or false
233 */
234 function PMA_DBI_getError($link = null)
235 {
236 unset($GLOBALS['errno']);
237 if (null === $link && isset($GLOBALS['userlink'])) {
238 $link =& $GLOBALS['userlink'];
239  
240 // Do not stop now. On the initial connection, we don't have a $link,
241 // we don't have a $GLOBALS['userlink'], but we can catch the error code
242 // } else {
243 // return FALSE;
244 }
245  
246 if (null !== $link) {
247 $error_number = mysql_errno($link);
248 $error_message = mysql_error($link);
249 } else {
250 $error_number = mysql_errno();
251 $error_message = mysql_error();
252 }
253 if (0 == $error_number) {
254 return false;
255 }
256  
257 // keep the error number for further check after the call to PMA_DBI_getError()
258 $GLOBALS['errno'] = $error_number;
259  
260 if (! empty($error_message)) {
261 $error_message = PMA_DBI_convert_message($error_message);
262 }
263  
264 // Some errors messages cannot be obtained by mysql_error()
265 if ($error_number == 2002) {
266 $error = '#' . ((string) $error_number) . ' - ' . $GLOBALS['strServerNotResponding'] . ' ' . $GLOBALS['strSocketProblem'];
267 } elseif ($error_number == 2003 ) {
268 $error = '#' . ((string) $error_number) . ' - ' . $GLOBALS['strServerNotResponding'];
269 } elseif (defined('PMA_MYSQL_INT_VERSION') && PMA_MYSQL_INT_VERSION >= 40100) {
270 $error = '#' . ((string) $error_number) . ' - ' . $error_message;
271 } else {
272 $error = '#' . ((string) $error_number) . ' - ' . PMA_convert_display_charset($error_message);
273 }
274 return $error;
275 }
276  
277 function PMA_DBI_close($link = null)
278 {
279 if (empty($link)) {
280 if (isset($GLOBALS['userlink'])) {
281 $link = $GLOBALS['userlink'];
282 } else {
283 return FALSE;
284 }
285 }
286 return @mysql_close($link);
287 }
288  
289 function PMA_DBI_num_rows($result) {
290 if (!is_bool($result)) {
291 return mysql_num_rows($result);
292 } else {
293 return 0;
294 }
295 }
296  
297 function PMA_DBI_insert_id($link = null)
298 {
299 if (empty($link)) {
300 if (isset($GLOBALS['userlink'])) {
301 $link = $GLOBALS['userlink'];
302 } else {
303 return FALSE;
304 }
305 }
306 return mysql_insert_id($link);
307 }
308  
309 function PMA_DBI_affected_rows($link = null)
310 {
311 if (empty($link)) {
312 if (isset($GLOBALS['userlink'])) {
313 $link = $GLOBALS['userlink'];
314 } else {
315 return FALSE;
316 }
317 }
318 return mysql_affected_rows($link);
319 }
320  
321 function PMA_DBI_get_fields_meta($result) {
322 $fields = array();
323 $num_fields = mysql_num_fields($result);
324 for ($i = 0; $i < $num_fields; $i++) {
325 $fields[] = PMA_convert_display_charset(mysql_fetch_field($result, $i));
326 }
327 return $fields;
328 }
329  
330 function PMA_DBI_num_fields($result) {
331 return mysql_num_fields($result);
332 }
333  
334 function PMA_DBI_field_len($result, $i) {
335 return mysql_field_len($result, $i);
336 }
337  
338 function PMA_DBI_field_name($result, $i) {
339 return mysql_field_name($result, $i);
340 }
341  
342 function PMA_DBI_field_flags($result, $i) {
343 return PMA_convert_display_charset(mysql_field_flags($result, $i));
344 }
345  
346 ?>