Rev Author Line No. Line
250 kaklik 1 <?php
2 /* $Id: mysqli.dbi.lib.php,v 2.43.2.1 2006/02/22 15:30:38 cybot_tm Exp $ */
3 // vim: expandtab sw=4 ts=4 sts=4:
4  
5 /**
6 * Interface to the improved MySQL extension (MySQLi)
7 */
8  
9 // MySQL client API
10 if (!defined('PMA_MYSQL_CLIENT_API')) {
11 $client_api = explode('.', mysqli_get_client_info());
12 define('PMA_MYSQL_CLIENT_API', (int)sprintf('%d%02d%02d', $client_api[0], $client_api[1], intval($client_api[2])));
13 unset($client_api);
14 }
15  
16 // Constants from mysql_com.h of MySQL 4.1.3
17  
18 define('NOT_NULL_FLAG', 1);
19 define('PRI_KEY_FLAG', 2);
20 define('UNIQUE_KEY_FLAG', 4);
21 define('MULTIPLE_KEY_FLAG', 8);
22 define('BLOB_FLAG', 16);
23 define('UNSIGNED_FLAG', 32);
24 define('ZEROFILL_FLAG', 64);
25 define('BINARY_FLAG', 128);
26 define('ENUM_FLAG', 256);
27 define('AUTO_INCREMENT_FLAG', 512);
28 define('TIMESTAMP_FLAG', 1024);
29 define('SET_FLAG', 2048);
30 define('NUM_FLAG', 32768);
31 define('PART_KEY_FLAG', 16384);
32 define('UNIQUE_FLAG', 65536);
33  
34 /**
35 * @see http://bugs.php.net/36007
36 */
37 if (! defined('MYSQLI_TYPE_NEWDECIMAL')) {
38 define('MYSQLI_TYPE_NEWDECIMAL', 246);
39 }
40 if (! defined('MYSQLI_TYPE_BIT')) {
41 define('MYSQLI_TYPE_BIT', 16);
42 }
43  
44 function PMA_DBI_connect($user, $password, $is_controluser = FALSE)
45 {
46 global $cfg, $php_errormsg;
47  
48 $server_port = (empty($cfg['Server']['port']))
49 ? FALSE
50 : (int) $cfg['Server']['port'];
51  
52 if (strtolower($cfg['Server']['connect_type']) == 'tcp') {
53 $cfg['Server']['socket'] = '';
54 }
55  
56 // NULL enables connection to the default socket
57 $server_socket = (empty($cfg['Server']['socket']))
58 ? null
59 : $cfg['Server']['socket'];
60  
61 $link = mysqli_init();
62  
63 mysqli_options($link, MYSQLI_OPT_LOCAL_INFILE, TRUE);
64  
65 $client_flags = $cfg['Server']['compress'] && defined('MYSQLI_CLIENT_COMPRESS') ? MYSQLI_CLIENT_COMPRESS : 0;
66  
67 $return_value = @mysqli_real_connect($link, $cfg['Server']['host'], $user, $password, FALSE, $server_port, $server_socket, $client_flags);
68  
69 if ($return_value == FALSE) {
70 PMA_auth_fails();
71 } // end if
72  
73 PMA_DBI_postConnect($link, $is_controluser);
74  
75 return $link;
76 }
77  
78 function PMA_DBI_select_db($dbname, $link = null)
79 {
80 if (empty($link)) {
81 if (isset($GLOBALS['userlink'])) {
82 $link = $GLOBALS['userlink'];
83 } else {
84 return FALSE;
85 }
86 }
87 if (PMA_MYSQL_INT_VERSION < 40100) {
88 $dbname = PMA_convert_charset($dbname);
89 }
90 return mysqli_select_db($link, $dbname);
91 }
92  
93 function PMA_DBI_try_query($query, $link = null, $options = 0)
94 {
95 if ($options == ($options | PMA_DBI_QUERY_STORE)) {
96 $method = MYSQLI_STORE_RESULT;
97 } elseif ($options == ($options | PMA_DBI_QUERY_UNBUFFERED)) {
98 $method = MYSQLI_USE_RESULT;
99 } else {
100 $method = MYSQLI_USE_RESULT;
101 }
102  
103 if (empty($link)) {
104 if (isset($GLOBALS['userlink'])) {
105 $link = $GLOBALS['userlink'];
106 } else {
107 return FALSE;
108 }
109 }
110 if (defined('PMA_MYSQL_INT_VERSION') && PMA_MYSQL_INT_VERSION < 40100) {
111 $query = PMA_convert_charset($query);
112 }
113 return mysqli_query($link, $query, $method);
114 // From the PHP manual:
115 // "note: returns TRUE on success or FALSE on failure. For SELECT,
116 // SHOW, DESCRIBE or EXPLAIN, mysqli_query() will return a result object"
117 // so, do not use the return value to feed mysqli_num_rows() if it's
118 // a boolean
119 }
120  
121 // The following function is meant for internal use only.
122 // Do not call it from outside this library!
123 function PMA_mysqli_fetch_array($result, $type = FALSE)
124 {
125 global $cfg, $allow_recoding, $charset, $convcharset;
126  
127 if ($type != FALSE) {
128 $data = @mysqli_fetch_array($result, $type);
129 } else {
130 $data = @mysqli_fetch_array($result);
131 }
132  
133 /* No data returned => do not touch it */
134 if (! $data) {
135 return $data;
136 }
137  
138 if (!defined('PMA_MYSQL_INT_VERSION') || PMA_MYSQL_INT_VERSION >= 40100
139 || !(isset($cfg['AllowAnywhereRecoding']) && $cfg['AllowAnywhereRecoding'] && $allow_recoding)) {
140 /* No recoding -> return data as we got them */
141 return $data;
142 } else {
143 $ret = array();
144 $num = mysqli_num_fields($result);
145 if ($num > 0) {
146 $fields = PMA_DBI_get_fields_meta($result);
147 }
148 // sometimes, mysqli_fetch_fields() does not return results
149 // (as seen in PHP 5.1.0-dev), so for now, return $data unchanged
150 if (!$fields) {
151 return $data;
152 }
153 $i = 0;
154 for ($i = 0; $i < $num; $i++) {
155 if (!isset($fields[$i]->type)) {
156 /* No meta information available -> we guess that it should be converted */
157 if (isset($data[$i])) {
158 $ret[$i] = PMA_convert_display_charset($data[$i]);
159 }
160 if (isset($fields[$i]->name) && isset($data[$fields[$i]->name])) {
161 $ret[PMA_convert_display_charset($fields[$i]->name)] = PMA_convert_display_charset($data[$fields[$i]->name]);
162 }
163 } else {
164 /* Meta information available -> check type of field and convert it according to the type */
165 if (stristr($fields[$i]->type, 'BLOB') || stristr($fields[$i]->type, 'BINARY')) {
166 if (isset($data[$i])) {
167 $ret[$i] = $data[$i];
168 }
169 if (isset($data[$fields[$i]->name])) {
170 $ret[PMA_convert_display_charset($fields[$i]->name)] = $data[$fields[$i]->name];
171 }
172 } else {
173 if (isset($data[$i])) {
174 $ret[$i] = PMA_convert_display_charset($data[$i]);
175 }
176 if (isset($data[$fields[$i]->name])) {
177 $ret[PMA_convert_display_charset($fields[$i]->name)] = PMA_convert_display_charset($data[$fields[$i]->name]);
178 }
179 }
180 }
181 }
182 return $ret;
183 }
184 }
185  
186 function PMA_DBI_fetch_array($result)
187 {
188 return PMA_mysqli_fetch_array($result, MYSQLI_BOTH);
189 }
190  
191 function PMA_DBI_fetch_assoc($result)
192 {
193 return PMA_mysqli_fetch_array($result, MYSQLI_ASSOC);
194 }
195  
196 function PMA_DBI_fetch_row($result)
197 {
198 return PMA_mysqli_fetch_array($result, MYSQLI_NUM);
199 }
200  
201 /**
202 * Frees the memory associated with the results
203 *
204 * @param result $result,... one or more mysql result resources
205 */
206 function PMA_DBI_free_result()
207 {
208 foreach (func_get_args() as $result) {
209 if (is_object($result)
210 && is_a($result, 'mysqli_result')) {
211 mysqli_free_result($result);
212 }
213 }
214 }
215  
216 /**
217 * Returns a string representing the type of connection used
218 * @uses mysqli_get_host_info()
219 * @uses $GLOBALS['userlink'] as default for $link
220 * @param resource $link mysql link
221 * @return string type of connection used
222 */
223 function PMA_DBI_get_host_info($link = null)
224 {
225 if (null === $link) {
226 if (isset($GLOBALS['userlink'])) {
227 $link = $GLOBALS['userlink'];
228 } else {
229 return false;
230 }
231 }
232 return mysqli_get_host_info($link);
233 }
234  
235 /**
236 * Returns the version of the MySQL protocol used
237 * @uses mysqli_get_proto_info()
238 * @uses $GLOBALS['userlink'] as default for $link
239 * @param resource $link mysql link
240 * @return integer version of the MySQL protocol used
241 */
242 function PMA_DBI_get_proto_info( $link = null )
243 {
244 if (null === $link) {
245 if (isset($GLOBALS['userlink'])) {
246 $link = $GLOBALS['userlink'];
247 } else {
248 return false;
249 }
250 }
251 return mysqli_get_proto_info($link);
252 }
253  
254 /**
255 * returns a string that represents the client library version
256 * @uses mysqli_get_client_info()
257 * @return string MySQL client library version
258 */
259 function PMA_DBI_get_client_info() {
260 return mysqli_get_client_info();
261 }
262  
263 /**
264 * returns last error message or false if no errors occured
265 *
266 * @uses PMA_MYSQL_INT_VERSION
267 * @uses PMA_convert_display_charset()
268 * @uses PMA_DBI_convert_message()
269 * @uses $GLOBALS['errno']
270 * @uses $GLOBALS['userlink']
271 * @uses $GLOBALS['strServerNotResponding']
272 * @uses $GLOBALS['strSocketProblem']
273 * @uses mysqli_errno()
274 * @uses mysqli_error()
275 * @uses mysqli_connect_errno()
276 * @uses mysqli_connect_error()
277 * @uses defined()
278 * @param resource $link mysql link
279 * @return string|boolean $error or false
280 */
281 function PMA_DBI_getError($link = null)
282 {
283 unset($GLOBALS['errno']);
284  
285 if (null === $link && isset($GLOBALS['userlink'])) {
286 $link =& $GLOBALS['userlink'];
287 // Do not stop now. We still can get the error code
288 // with mysqli_connect_errno()
289 // } else {
290 // return false;
291 }
292  
293 if (null !== $link) {
294 $error_number = mysqli_errno($link);
295 $error_message = mysqli_error($link);
296 } else {
297 $error_number = mysqli_connect_errno();
298 $error_message = mysqli_connect_error();
299 }
300 if (0 == $error_number) {
301 return false;
302 }
303  
304 // keep the error number for further check after the call to PMA_DBI_getError()
305 $GLOBALS['errno'] = $error_number;
306  
307 if (! empty($error_message)) {
308 $error_message = PMA_DBI_convert_message($error_message);
309 }
310  
311 if ($error_number == 2002) {
312 $error = '#' . ((string) $error_number) . ' - ' . $GLOBALS['strServerNotResponding'] . ' ' . $GLOBALS['strSocketProblem'];
313 } elseif (defined('PMA_MYSQL_INT_VERSION') && PMA_MYSQL_INT_VERSION >= 40100) {
314 $error = '#' . ((string) $error_number) . ' - ' . $error_message;
315 } else {
316 $error = '#' . ((string) $error_number) . ' - ' . PMA_convert_display_charset($error_message);
317 }
318 return $error;
319 }
320  
321 function PMA_DBI_close($link = null)
322 {
323 if (empty($link)) {
324 if (isset($GLOBALS['userlink'])) {
325 $link = $GLOBALS['userlink'];
326 } else {
327 return FALSE;
328 }
329 }
330 return @mysqli_close($link);
331 }
332  
333 function PMA_DBI_num_rows($result)
334 {
335 // see the note for PMA_DBI_try_query();
336 if (!is_bool($result)) {
337 return @mysqli_num_rows($result);
338 } else {
339 return 0;
340 }
341 }
342  
343 function PMA_DBI_insert_id($link = '')
344 {
345 if (empty($link)) {
346 if (isset($GLOBALS['userlink'])) {
347 $link = $GLOBALS['userlink'];
348 } else {
349 return FALSE;
350 }
351 }
352 return mysqli_insert_id($link);
353 }
354  
355 function PMA_DBI_affected_rows($link = null)
356 {
357 if (empty($link)) {
358 if (isset($GLOBALS['userlink'])) {
359 $link = $GLOBALS['userlink'];
360 } else {
361 return FALSE;
362 }
363 }
364 return mysqli_affected_rows($link);
365 }
366  
367 function PMA_DBI_get_fields_meta($result)
368 {
369 // Build an associative array for a type look up
370 $typeAr = Array();
371 $typeAr[MYSQLI_TYPE_DECIMAL] = 'real';
372 $typeAr[MYSQLI_TYPE_NEWDECIMAL] = 'real';
373 $typeAr[MYSQLI_TYPE_BIT] = 'bool';
374 $typeAr[MYSQLI_TYPE_TINY] = 'int';
375 $typeAr[MYSQLI_TYPE_SHORT] = 'int';
376 $typeAr[MYSQLI_TYPE_LONG] = 'int';
377 $typeAr[MYSQLI_TYPE_FLOAT] = 'real';
378 $typeAr[MYSQLI_TYPE_DOUBLE] = 'real';
379 $typeAr[MYSQLI_TYPE_NULL] = 'null';
380 $typeAr[MYSQLI_TYPE_TIMESTAMP] = 'timestamp';
381 $typeAr[MYSQLI_TYPE_LONGLONG] = 'int';
382 $typeAr[MYSQLI_TYPE_INT24] = 'int';
383 $typeAr[MYSQLI_TYPE_DATE] = 'date';
384 $typeAr[MYSQLI_TYPE_TIME] = 'time';
385 $typeAr[MYSQLI_TYPE_DATETIME] = 'datetime';
386 $typeAr[MYSQLI_TYPE_YEAR] = 'year';
387 $typeAr[MYSQLI_TYPE_NEWDATE] = 'date';
388 $typeAr[MYSQLI_TYPE_ENUM] = 'unknown';
389 $typeAr[MYSQLI_TYPE_SET] = 'unknown';
390 $typeAr[MYSQLI_TYPE_TINY_BLOB] = 'blob';
391 $typeAr[MYSQLI_TYPE_MEDIUM_BLOB] = 'blob';
392 $typeAr[MYSQLI_TYPE_LONG_BLOB] = 'blob';
393 $typeAr[MYSQLI_TYPE_BLOB] = 'blob';
394 $typeAr[MYSQLI_TYPE_VAR_STRING] = 'string';
395 $typeAr[MYSQLI_TYPE_STRING] = 'string';
396 $typeAr[MYSQLI_TYPE_CHAR] = 'string';
397 $typeAr[MYSQLI_TYPE_GEOMETRY] = 'unknown';
398  
399 $fields = mysqli_fetch_fields($result);
400  
401 // this happens sometimes (seen under MySQL 4.0.25)
402 if (!is_array($fields)) {
403 return FALSE;
404 }
405  
406 foreach ($fields as $k => $field) {
407 $fields[$k]->type = $typeAr[$fields[$k]->type];
408 $fields[$k]->flags = PMA_DBI_field_flags($result, $k);
409  
410 // Enhance the field objects for mysql-extension compatibilty
411 $flags = explode(' ', $fields[$k]->flags);
412 array_unshift($flags, 'dummy');
413 $fields[$k]->multiple_key = (int)(array_search('multiple_key', $flags, true) > 0);
414 $fields[$k]->primary_key = (int)(array_search('primary_key', $flags, true) > 0);
415 $fields[$k]->unique_key = (int)(array_search('unique_key', $flags, true) > 0);
416 $fields[$k]->not_null = (int)(array_search('not_null', $flags, true) > 0);
417 $fields[$k]->unsigned = (int)(array_search('unsigned', $flags, true) > 0);
418 $fields[$k]->zerofill = (int)(array_search('zerofill', $flags, true) > 0);
419 $fields[$k]->numeric = (int)(array_search('num', $flags, true) > 0);
420 $fields[$k]->blob = (int)(array_search('blob', $flags, true) > 0);
421 }
422 return $fields;
423 }
424  
425 function PMA_DBI_num_fields($result)
426 {
427 return mysqli_num_fields($result);
428 }
429  
430 function PMA_DBI_field_len($result, $i)
431 {
432 $info = mysqli_fetch_field_direct($result, $i);
433 // stdClass::$length will be integrated in
434 // mysqli-ext when mysql4.1 has been released.
435 return @$info->length;
436 }
437  
438 function PMA_DBI_field_name($result, $i)
439 {
440 $info = mysqli_fetch_field_direct($result, $i);
441 return $info->name;
442 }
443  
444 function PMA_DBI_field_flags($result, $i)
445 {
446 $f = mysqli_fetch_field_direct($result, $i);
447 $f = $f->flags;
448 $flags = '';
449 if ($f & UNIQUE_FLAG) { $flags .= 'unique ';}
450 if ($f & NUM_FLAG) { $flags .= 'num ';}
451 if ($f & PART_KEY_FLAG) { $flags .= 'part_key ';}
452 if ($f & SET_FLAG) { $flags .= 'set ';}
453 if ($f & TIMESTAMP_FLAG) { $flags .= 'timestamp ';}
454 if ($f & AUTO_INCREMENT_FLAG) { $flags .= 'auto_increment ';}
455 if ($f & ENUM_FLAG) { $flags .= 'enum ';}
456 if ($f & BINARY_FLAG) { $flags .= 'binary ';}
457 if ($f & ZEROFILL_FLAG) { $flags .= 'zerofill ';}
458 if ($f & UNSIGNED_FLAG) { $flags .= 'unsigned ';}
459 if ($f & BLOB_FLAG) { $flags .= 'blob ';}
460 if ($f & MULTIPLE_KEY_FLAG) { $flags .= 'multiple_key ';}
461 if ($f & UNIQUE_KEY_FLAG) { $flags .= 'unique_key ';}
462 if ($f & PRI_KEY_FLAG) { $flags .= 'primary_key ';}
463 if ($f & NOT_NULL_FLAG) { $flags .= 'not_null ';}
464 return PMA_convert_display_charset(trim($flags));
465 }
466  
467 ?>