Fix some phpdoc errors.
[phpmyadmin/crack.git] / libraries / dbi / mysqli.dbi.lib.php
blobb43d6dbae5d7cb02540c66fcc3248972ad35954f
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Interface to the improved MySQL extension (MySQLi)
6 * @package phpMyAdmin-DBI-MySQLi
7 * @version $Id$
8 */
9 if (! defined('PHPMYADMIN')) {
10 exit;
13 /**
14 * MySQL client API
16 if (!defined('PMA_MYSQL_CLIENT_API')) {
17 $client_api = explode('.', mysqli_get_client_info());
18 define('PMA_MYSQL_CLIENT_API', (int)sprintf('%d%02d%02d', $client_api[0], $client_api[1], intval($client_api[2])));
19 unset($client_api);
22 /**
23 * some older mysql client libs are missing this constants ...
25 if (! defined('MYSQLI_BINARY_FLAG')) {
26 define('MYSQLI_BINARY_FLAG', 128);
29 /**
30 * @see http://bugs.php.net/36007
32 if (! defined('MYSQLI_TYPE_NEWDECIMAL')) {
33 define('MYSQLI_TYPE_NEWDECIMAL', 246);
35 if (! defined('MYSQLI_TYPE_BIT')) {
36 define('MYSQLI_TYPE_BIT', 16);
39 /**
40 * connects to the database server
42 * @uses $GLOBALS['cfg']['Server']
43 * @uses PMA_auth_fails()
44 * @uses PMA_DBI_postConnect()
45 * @uses MYSQLI_CLIENT_COMPRESS
46 * @uses MYSQLI_OPT_LOCAL_INFILE
47 * @uses strtolower()
48 * @uses mysqli_init()
49 * @uses mysqli_options()
50 * @uses mysqli_real_connect()
51 * @uses defined()
52 * @param string $user mysql user name
53 * @param string $password mysql user password
54 * @param boolean $is_controluser
55 * @return mixed false on error or a mysqli object on success
57 function PMA_DBI_connect($user, $password, $is_controluser = false)
59 $server_port = (empty($GLOBALS['cfg']['Server']['port']))
60 ? false
61 : (int) $GLOBALS['cfg']['Server']['port'];
63 if (strtolower($GLOBALS['cfg']['Server']['connect_type']) == 'tcp') {
64 $GLOBALS['cfg']['Server']['socket'] = '';
67 // NULL enables connection to the default socket
68 $server_socket = (empty($GLOBALS['cfg']['Server']['socket']))
69 ? null
70 : $GLOBALS['cfg']['Server']['socket'];
72 $link = mysqli_init();
74 mysqli_options($link, MYSQLI_OPT_LOCAL_INFILE, true);
76 $client_flags = 0;
78 /* Optionally compress connection */
79 if ($GLOBALS['cfg']['Server']['compress'] && defined('MYSQLI_CLIENT_COMPRESS')) {
80 $client_flags |= MYSQLI_CLIENT_COMPRESS;
83 /* Optionally enable SSL */
84 if ($GLOBALS['cfg']['Server']['ssl'] && defined('MYSQLI_CLIENT_SSL')) {
85 $client_flags |= MYSQLI_CLIENT_SSL;
88 $return_value = @mysqli_real_connect($link, $GLOBALS['cfg']['Server']['host'], $user, $password, false, $server_port, $server_socket, $client_flags);
90 // Retry with empty password if we're allowed to
91 if ($return_value == false && isset($cfg['Server']['nopassword']) && $cfg['Server']['nopassword'] && !$is_controluser) {
92 $return_value = @mysqli_real_connect($link, $GLOBALS['cfg']['Server']['host'], $user, '', false, $server_port, $server_socket, $client_flags);
95 if ($return_value == false) {
96 if ($is_controluser) {
97 trigger_error($GLOBALS['strControluserFailed'], E_USER_WARNING);
98 return false;
100 PMA_auth_fails();
101 } // end if
103 PMA_DBI_postConnect($link, $is_controluser);
105 return $link;
109 * selects given database
111 * @uses $GLOBALS['userlink']
112 * @uses PMA_convert_charset()
113 * @uses mysqli_select_db()
114 * @param string $dbname database name to select
115 * @param object mysqli $link the mysqli object
116 * @return boolean true or false
118 function PMA_DBI_select_db($dbname, $link = null)
120 if (empty($link)) {
121 if (isset($GLOBALS['userlink'])) {
122 $link = $GLOBALS['userlink'];
123 } else {
124 return false;
127 return mysqli_select_db($link, $dbname);
131 * runs a query and returns the result
133 * @uses PMA_DBI_QUERY_STORE
134 * @uses PMA_DBI_QUERY_UNBUFFERED
135 * @uses $GLOBALS['userlink']
136 * @uses PMA_convert_charset()
137 * @uses MYSQLI_STORE_RESULT
138 * @uses MYSQLI_USE_RESULT
139 * @uses mysqli_query()
140 * @uses defined()
141 * @param string $query query to execute
142 * @param object mysqli $link mysqli object
143 * @param integer $options
144 * @return mixed true, false or result object
146 function PMA_DBI_try_query($query, $link = null, $options = 0)
148 if ($options == ($options | PMA_DBI_QUERY_STORE)) {
149 $method = MYSQLI_STORE_RESULT;
150 } elseif ($options == ($options | PMA_DBI_QUERY_UNBUFFERED)) {
151 $method = MYSQLI_USE_RESULT;
152 } else {
153 $method = 0;
156 if (empty($link)) {
157 if (isset($GLOBALS['userlink'])) {
158 $link = $GLOBALS['userlink'];
159 } else {
160 return false;
164 if ($GLOBALS['cfg']['DBG']['sql']) {
165 $time = microtime(true);
167 $r = mysqli_query($link, $query, $method);
168 if ($GLOBALS['cfg']['DBG']['sql']) {
169 $time = microtime(true) - $time;
171 $hash = md5($query);
173 if (isset($_SESSION['debug']['queries'][$hash])) {
174 $_SESSION['debug']['queries'][$hash]['count']++;
175 } else {
176 $_SESSION['debug']['queries'][$hash] = array();
177 $_SESSION['debug']['queries'][$hash]['count'] = 1;
178 $_SESSION['debug']['queries'][$hash]['query'] = $query;
179 $_SESSION['debug']['queries'][$hash]['time'] = $time;
182 $trace = array();
183 foreach (debug_backtrace() as $trace_step) {
184 $trace[] = PMA_Error::relPath($trace_step['file']) . '#'
185 . $trace_step['line'] . ': '
186 . (isset($trace_step['class']) ? $trace_step['class'] : '')
187 //. (isset($trace_step['object']) ? get_class($trace_step['object']) : '')
188 . (isset($trace_step['type']) ? $trace_step['type'] : '')
189 . (isset($trace_step['function']) ? $trace_step['function'] : '')
190 . '('
191 . (isset($trace_step['params']) ? implode(', ', $trace_step['params']) : '')
192 . ')'
195 $_SESSION['debug']['queries'][$hash]['trace'][] = $trace;
198 return $r;
200 // From the PHP manual:
201 // "note: returns true on success or false on failure. For SELECT,
202 // SHOW, DESCRIBE or EXPLAIN, mysqli_query() will return a result object"
203 // so, do not use the return value to feed mysqli_num_rows() if it's
204 // a boolean
208 * returns array of rows with associative and numeric keys from $result
210 * @uses mysqli_fetch_array()
211 * @uses MYSQLI_BOTH
212 * @param object mysqli result $result
213 * @return array result rows
215 function PMA_DBI_fetch_array($result)
217 return mysqli_fetch_array($result, MYSQLI_BOTH);
221 * returns array of rows with associative keys from $result
223 * @uses mysqli_fetch_array()
224 * @uses MYSQLI_ASSOC
225 * @param object mysqli result $result
226 * @return array result rows
228 function PMA_DBI_fetch_assoc($result)
230 return mysqli_fetch_array($result, MYSQLI_ASSOC);
234 * returns array of rows with numeric keys from $result
236 * @uses mysqli_fetch_array()
237 * @uses MYSQLI_NUM
238 * @param object mysqli result $result
239 * @return array result rows
241 function PMA_DBI_fetch_row($result)
243 return mysqli_fetch_array($result, MYSQLI_NUM);
247 * Adjusts the result pointer to an arbitrary row in the result
249 * @uses mysqli_data_seek()
250 * @param $result
251 * @param $offset
252 * @return boolean true on success, false on failure
254 function PMA_DBI_data_seek($result, $offset)
256 return mysqli_data_seek($result, $offset);
260 * Frees the memory associated with the results
262 * @uses mysqli_result
263 * @uses func_get_args()
264 * @uses mysqli_free_result()
265 * @param result $result,... one or more mysql result resources
267 function PMA_DBI_free_result()
269 foreach (func_get_args() as $result) {
270 if ($result instanceof mysqli_result) {
271 mysqli_free_result($result);
277 * Returns a string representing the type of connection used
278 * @uses mysqli_get_host_info()
279 * @uses $GLOBALS['userlink'] as default for $link
280 * @param resource $link mysql link
281 * @return string type of connection used
283 function PMA_DBI_get_host_info($link = null)
285 if (null === $link) {
286 if (isset($GLOBALS['userlink'])) {
287 $link = $GLOBALS['userlink'];
288 } else {
289 return false;
292 return mysqli_get_host_info($link);
296 * Returns the version of the MySQL protocol used
297 * @uses mysqli_get_proto_info()
298 * @uses $GLOBALS['userlink'] as default for $link
299 * @param resource $link mysql link
300 * @return integer version of the MySQL protocol used
302 function PMA_DBI_get_proto_info($link = null)
304 if (null === $link) {
305 if (isset($GLOBALS['userlink'])) {
306 $link = $GLOBALS['userlink'];
307 } else {
308 return false;
311 return mysqli_get_proto_info($link);
315 * returns a string that represents the client library version
316 * @uses mysqli_get_client_info()
317 * @return string MySQL client library version
319 function PMA_DBI_get_client_info()
321 return mysqli_get_client_info();
325 * returns last error message or false if no errors occured
327 * @uses PMA_DBI_convert_message()
328 * @uses $GLOBALS['errno']
329 * @uses $GLOBALS['userlink']
330 * @uses $GLOBALS['strServerNotResponding']
331 * @uses $GLOBALS['strSocketProblem']
332 * @uses mysqli_errno()
333 * @uses mysqli_error()
334 * @uses mysqli_connect_errno()
335 * @uses mysqli_connect_error()
336 * @uses defined()
337 * @param resource $link mysql link
338 * @return string|boolean $error or false
340 function PMA_DBI_getError($link = null)
342 $GLOBALS['errno'] = 0;
344 if (null === $link && isset($GLOBALS['userlink'])) {
345 $link =& $GLOBALS['userlink'];
346 // Do not stop now. We still can get the error code
347 // with mysqli_connect_errno()
348 // } else {
349 // return false;
352 if (null !== $link) {
353 $error_number = mysqli_errno($link);
354 $error_message = mysqli_error($link);
355 } else {
356 $error_number = mysqli_connect_errno();
357 $error_message = mysqli_connect_error();
359 if (0 == $error_number) {
360 return false;
363 // keep the error number for further check after the call to PMA_DBI_getError()
364 $GLOBALS['errno'] = $error_number;
366 if (! empty($error_message)) {
367 $error_message = PMA_DBI_convert_message($error_message);
370 if ($error_number == 2002) {
371 $error = '#' . ((string) $error_number) . ' - ' . $GLOBALS['strServerNotResponding'] . ' ' . $GLOBALS['strSocketProblem'];
372 } else {
373 $error = '#' . ((string) $error_number) . ' - ' . $error_message;
375 return $error;
380 * @param object mysqli result $result
382 function PMA_DBI_num_rows($result)
384 // see the note for PMA_DBI_try_query();
385 if (!is_bool($result)) {
386 return @mysqli_num_rows($result);
387 } else {
388 return 0;
393 * returns last inserted auto_increment id for given $link or $GLOBALS['userlink']
395 * @uses $GLOBALS['userlink']
396 * @uses mysqli_insert_id()
397 * @param object mysqli $link the mysqli object
398 * @return string ineteger
400 function PMA_DBI_insert_id($link = '')
402 if (empty($link)) {
403 if (isset($GLOBALS['userlink'])) {
404 $link = $GLOBALS['userlink'];
405 } else {
406 return false;
409 return mysqli_insert_id($link);
413 * returns the number of rows affected by last query
415 * @uses $GLOBALS['userlink']
416 * @uses mysqli_affected_rows()
417 * @param object mysqli $link the mysqli object
418 * @return string integer
420 function PMA_DBI_affected_rows($link = null)
422 if (empty($link)) {
423 if (isset($GLOBALS['userlink'])) {
424 $link = $GLOBALS['userlink'];
425 } else {
426 return false;
429 return mysqli_affected_rows($link);
433 * returns metainfo for fields in $result
435 * @todo preserve orignal flags value
436 * @uses PMA_DBI_field_flags()
437 * @uses MYSQLI_TYPE_*
438 * @uses MYSQLI_MULTIPLE_KEY_FLAG
439 * @uses MYSQLI_PRI_KEY_FLAG
440 * @uses MYSQLI_UNIQUE_KEY_FLAG
441 * @uses MYSQLI_NOT_NULL_FLAG
442 * @uses MYSQLI_UNSIGNED_FLAG
443 * @uses MYSQLI_ZEROFILL_FLAG
444 * @uses MYSQLI_NUM_FLAG
445 * @uses MYSQLI_TYPE_BLOB
446 * @uses MYSQLI_BLOB_FLAG
447 * @uses defined()
448 * @uses mysqli_fetch_fields()
449 * @uses is_array()
450 * @param object mysqli result $result
451 * @return array meta info for fields in $result
453 function PMA_DBI_get_fields_meta($result)
455 // Build an associative array for a type look up
456 $typeAr = array();
457 $typeAr[MYSQLI_TYPE_DECIMAL] = 'real';
458 $typeAr[MYSQLI_TYPE_NEWDECIMAL] = 'real';
459 $typeAr[MYSQLI_TYPE_BIT] = 'int';
460 $typeAr[MYSQLI_TYPE_TINY] = 'int';
461 $typeAr[MYSQLI_TYPE_SHORT] = 'int';
462 $typeAr[MYSQLI_TYPE_LONG] = 'int';
463 $typeAr[MYSQLI_TYPE_FLOAT] = 'real';
464 $typeAr[MYSQLI_TYPE_DOUBLE] = 'real';
465 $typeAr[MYSQLI_TYPE_NULL] = 'null';
466 $typeAr[MYSQLI_TYPE_TIMESTAMP] = 'timestamp';
467 $typeAr[MYSQLI_TYPE_LONGLONG] = 'int';
468 $typeAr[MYSQLI_TYPE_INT24] = 'int';
469 $typeAr[MYSQLI_TYPE_DATE] = 'date';
470 $typeAr[MYSQLI_TYPE_TIME] = 'time';
471 $typeAr[MYSQLI_TYPE_DATETIME] = 'datetime';
472 $typeAr[MYSQLI_TYPE_YEAR] = 'year';
473 $typeAr[MYSQLI_TYPE_NEWDATE] = 'date';
474 $typeAr[MYSQLI_TYPE_ENUM] = 'unknown';
475 $typeAr[MYSQLI_TYPE_SET] = 'unknown';
476 $typeAr[MYSQLI_TYPE_TINY_BLOB] = 'blob';
477 $typeAr[MYSQLI_TYPE_MEDIUM_BLOB] = 'blob';
478 $typeAr[MYSQLI_TYPE_LONG_BLOB] = 'blob';
479 $typeAr[MYSQLI_TYPE_BLOB] = 'blob';
480 $typeAr[MYSQLI_TYPE_VAR_STRING] = 'string';
481 $typeAr[MYSQLI_TYPE_STRING] = 'string';
482 // MySQL returns MYSQLI_TYPE_STRING for CHAR
483 // and MYSQLI_TYPE_CHAR === MYSQLI_TYPE_TINY
484 // so this would override TINYINT and mark all TINYINT as string
485 // https://sf.net/tracker/?func=detail&aid=1532111&group_id=23067&atid=377408
486 //$typeAr[MYSQLI_TYPE_CHAR] = 'string';
487 $typeAr[MYSQLI_TYPE_GEOMETRY] = 'unknown';
488 $typeAr[MYSQLI_TYPE_BIT] = 'bit';
490 $fields = mysqli_fetch_fields($result);
492 // this happens sometimes (seen under MySQL 4.0.25)
493 if (!is_array($fields)) {
494 return false;
497 foreach ($fields as $k => $field) {
498 $fields[$k]->_type = $field->type;
499 $fields[$k]->type = $typeAr[$field->type];
500 $fields[$k]->_flags = $field->flags;
501 $fields[$k]->flags = PMA_DBI_field_flags($result, $k);
503 // Enhance the field objects for mysql-extension compatibilty
504 //$flags = explode(' ', $fields[$k]->flags);
505 //array_unshift($flags, 'dummy');
506 $fields[$k]->multiple_key
507 = (int) (bool) ($fields[$k]->_flags & MYSQLI_MULTIPLE_KEY_FLAG);
508 $fields[$k]->primary_key
509 = (int) (bool) ($fields[$k]->_flags & MYSQLI_PRI_KEY_FLAG);
510 $fields[$k]->unique_key
511 = (int) (bool) ($fields[$k]->_flags & MYSQLI_UNIQUE_KEY_FLAG);
512 $fields[$k]->not_null
513 = (int) (bool) ($fields[$k]->_flags & MYSQLI_NOT_NULL_FLAG);
514 $fields[$k]->unsigned
515 = (int) (bool) ($fields[$k]->_flags & MYSQLI_UNSIGNED_FLAG);
516 $fields[$k]->zerofill
517 = (int) (bool) ($fields[$k]->_flags & MYSQLI_ZEROFILL_FLAG);
518 $fields[$k]->numeric
519 = (int) (bool) ($fields[$k]->_flags & MYSQLI_NUM_FLAG);
520 $fields[$k]->blob
521 = (int) (bool) ($fields[$k]->_flags & MYSQLI_BLOB_FLAG);
523 return $fields;
527 * return number of fields in given $result
529 * @param object mysqli result $result
530 * @return integer field count
532 function PMA_DBI_num_fields($result)
534 return mysqli_num_fields($result);
538 * returns the length of the given field $i in $result
540 * @uses mysqli_fetch_field_direct()
541 * @param object mysqli result $result
542 * @param integer $i field
543 * @return integer length of field
545 function PMA_DBI_field_len($result, $i)
547 return mysqli_fetch_field_direct($result, $i)->length;
551 * returns name of $i. field in $result
553 * @uses mysqli_fetch_field_direct()
554 * @param object mysqli result $result
555 * @param integer $i field
556 * @return string name of $i. field in $result
558 function PMA_DBI_field_name($result, $i)
560 return mysqli_fetch_field_direct($result, $i)->name;
564 * returns concatenated string of human readable field flags
566 * @uses MYSQLI_UNIQUE_KEY_FLAG
567 * @uses MYSQLI_NUM_FLAG
568 * @uses MYSQLI_PART_KEY_FLAG
569 * @uses MYSQLI_TYPE_SET
570 * @uses MYSQLI_TIMESTAMP_FLAG
571 * @uses MYSQLI_AUTO_INCREMENT_FLAG
572 * @uses MYSQLI_TYPE_ENUM
573 * @uses MYSQLI_ZEROFILL_FLAG
574 * @uses MYSQLI_UNSIGNED_FLAG
575 * @uses MYSQLI_BLOB_FLAG
576 * @uses MYSQLI_MULTIPLE_KEY_FLAG
577 * @uses MYSQLI_UNIQUE_KEY_FLAG
578 * @uses MYSQLI_PRI_KEY_FLAG
579 * @uses MYSQLI_NOT_NULL_FLAG
580 * @uses MYSQLI_TYPE_BLOB
581 * @uses MYSQLI_TYPE_MEDIUM_BLOB
582 * @uses MYSQLI_TYPE_LONG_BLOB
583 * @uses MYSQLI_TYPE_VAR_STRING
584 * @uses MYSQLI_TYPE_STRING
585 * @uses mysqli_fetch_field_direct()
586 * @param object mysqli result $result
587 * @param integer $i field
588 * @return string field flags
590 function PMA_DBI_field_flags($result, $i)
592 // This is missing from PHP 5.2.5, see http://bugs.php.net/bug.php?id=44846
593 if (! defined('MYSQLI_ENUM_FLAG')) {
594 define('MYSQLI_ENUM_FLAG', 256); // see MySQL source include/mysql_com.h
596 $f = mysqli_fetch_field_direct($result, $i);
597 $type = $f->type;
598 $charsetnr = $f->charsetnr;
599 $f = $f->flags;
600 $flags = '';
601 if ($f & MYSQLI_UNIQUE_KEY_FLAG) { $flags .= 'unique ';}
602 if ($f & MYSQLI_NUM_FLAG) { $flags .= 'num ';}
603 if ($f & MYSQLI_PART_KEY_FLAG) { $flags .= 'part_key ';}
604 if ($f & MYSQLI_SET_FLAG) { $flags .= 'set ';}
605 if ($f & MYSQLI_TIMESTAMP_FLAG) { $flags .= 'timestamp ';}
606 if ($f & MYSQLI_AUTO_INCREMENT_FLAG) { $flags .= 'auto_increment ';}
607 if ($f & MYSQLI_ENUM_FLAG) { $flags .= 'enum ';}
608 // See http://dev.mysql.com/doc/refman/6.0/en/c-api-datatypes.html:
609 // to determine if a string is binary, we should not use MYSQLI_BINARY_FLAG
610 // but instead the charsetnr member of the MYSQL_FIELD
611 // structure. Watch out: some types like DATE returns 63 in charsetnr
612 // so we have to check also the type.
613 // Unfortunately there is no equivalent in the mysql extension.
614 if (($type == MYSQLI_TYPE_TINY_BLOB || $type == MYSQLI_TYPE_BLOB || $type == MYSQLI_TYPE_MEDIUM_BLOB || $type == MYSQLI_TYPE_LONG_BLOB || $type == MYSQLI_TYPE_VAR_STRING || $type == MYSQLI_TYPE_STRING) && 63 == $charsetnr) { $flags .= 'binary ';}
615 if ($f & MYSQLI_ZEROFILL_FLAG) { $flags .= 'zerofill ';}
616 if ($f & MYSQLI_UNSIGNED_FLAG) { $flags .= 'unsigned ';}
617 if ($f & MYSQLI_BLOB_FLAG) { $flags .= 'blob ';}
618 if ($f & MYSQLI_MULTIPLE_KEY_FLAG) { $flags .= 'multiple_key ';}
619 if ($f & MYSQLI_UNIQUE_KEY_FLAG) { $flags .= 'unique_key ';}
620 if ($f & MYSQLI_PRI_KEY_FLAG) { $flags .= 'primary_key ';}
621 if ($f & MYSQLI_NOT_NULL_FLAG) { $flags .= 'not_null ';}
622 return trim($flags);