jquery components should be under js/jquery
[phpmyadmin/ninadsp.git] / libraries / dbi / mysqli.dbi.lib.php
blob086b47adb9f20b87e958731e6920b7425dbc7c5a
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 */
8 if (! defined('PHPMYADMIN')) {
9 exit;
12 require_once './libraries/logging.lib.php';
14 /**
15 * MySQL client API
17 if (!defined('PMA_MYSQL_CLIENT_API')) {
18 $client_api = explode('.', mysqli_get_client_info());
19 define('PMA_MYSQL_CLIENT_API', (int)sprintf('%d%02d%02d', $client_api[0], $client_api[1], intval($client_api[2])));
20 unset($client_api);
23 /**
24 * some PHP versions are reporting extra messages like "No index used in query"
27 mysqli_report(MYSQLI_REPORT_OFF);
29 /**
30 * some older mysql client libs are missing these constants ...
32 if (! defined('MYSQLI_BINARY_FLAG')) {
33 define('MYSQLI_BINARY_FLAG', 128);
36 /**
37 * @see http://bugs.php.net/36007
39 if (! defined('MYSQLI_TYPE_NEWDECIMAL')) {
40 define('MYSQLI_TYPE_NEWDECIMAL', 246);
42 if (! defined('MYSQLI_TYPE_BIT')) {
43 define('MYSQLI_TYPE_BIT', 16);
46 /**
47 * connects to the database server
49 * @uses $GLOBALS['cfg']['Server']
50 * @uses PMA_auth_fails()
51 * @uses PMA_DBI_postConnect()
52 * @uses MYSQLI_CLIENT_COMPRESS
53 * @uses MYSQLI_OPT_LOCAL_INFILE
54 * @uses strtolower()
55 * @uses mysqli_init()
56 * @uses mysqli_options()
57 * @uses mysqli_real_connect()
58 * @uses defined()
59 * @param string $user mysql user name
60 * @param string $password mysql user password
61 * @param boolean $is_controluser
62 * @param array $server host/port/socket
63 * @param boolean $auxiliary_connection (when true, don't go back to login if connection fails)
64 * @return mixed false on error or a mysqli object on success
66 function PMA_DBI_connect($user, $password, $is_controluser = false, $server = null, $auxiliary_connection = false)
68 if ($server) {
69 $server_port = (empty($server['port']))
70 ? false
71 : (int)$server['port'];
72 $server_socket = (empty($server['socket']))
73 ? ''
74 : $server['socket'];
75 $server['host'] = (empty($server['host']))
76 ? 'localhost'
77 : $server['host'];
78 } else {
79 $server_port = (empty($GLOBALS['cfg']['Server']['port']))
80 ? false
81 : (int) $GLOBALS['cfg']['Server']['port'];
82 $server_socket = (empty($GLOBALS['cfg']['Server']['socket']))
83 ? null
84 : $GLOBALS['cfg']['Server']['socket'];
88 if (strtolower($GLOBALS['cfg']['Server']['connect_type']) == 'tcp') {
89 $GLOBALS['cfg']['Server']['socket'] = '';
92 // NULL enables connection to the default socket
94 $link = mysqli_init();
96 mysqli_options($link, MYSQLI_OPT_LOCAL_INFILE, true);
98 $client_flags = 0;
100 /* Optionally compress connection */
101 if ($GLOBALS['cfg']['Server']['compress'] && defined('MYSQLI_CLIENT_COMPRESS')) {
102 $client_flags |= MYSQLI_CLIENT_COMPRESS;
105 /* Optionally enable SSL */
106 if ($GLOBALS['cfg']['Server']['ssl'] && defined('MYSQLI_CLIENT_SSL')) {
107 $client_flags |= MYSQLI_CLIENT_SSL;
110 if (!$server) {
111 $return_value = @mysqli_real_connect($link, $GLOBALS['cfg']['Server']['host'], $user, $password, false, $server_port, $server_socket, $client_flags);
112 // Retry with empty password if we're allowed to
113 if ($return_value == false && isset($GLOBALS['cfg']['Server']['nopassword']) && $GLOBALS['cfg']['Server']['nopassword'] && !$is_controluser) {
114 $return_value = @mysqli_real_connect($link, $GLOBALS['cfg']['Server']['host'], $user, '', false, $server_port, $server_socket, $client_flags);
116 } else {
117 $return_value = @mysqli_real_connect($link, $server['host'], $user, $password, false, $server_port, $server_socket);
120 if ($return_value == false) {
121 if ($is_controluser) {
122 trigger_error(__('Connection for controluser as defined in your configuration failed.'), E_USER_WARNING);
123 return false;
125 // we could be calling PMA_DBI_connect() to connect to another
126 // server, for example in the Synchronize feature, so do not
127 // go back to main login if it fails
128 if (! $auxiliary_connection) {
129 PMA_log_user($user, 'mysql-denied');
130 PMA_auth_fails();
131 } else {
132 return false;
134 } else {
135 PMA_DBI_postConnect($link, $is_controluser);
138 return $link;
142 * selects given database
144 * @uses $GLOBALS['userlink']
145 * @uses mysqli_select_db()
146 * @param string $dbname database name to select
147 * @param object mysqli $link the mysqli object
148 * @return boolean true or false
150 function PMA_DBI_select_db($dbname, $link = null)
152 if (empty($link)) {
153 if (isset($GLOBALS['userlink'])) {
154 $link = $GLOBALS['userlink'];
155 } else {
156 return false;
159 return mysqli_select_db($link, $dbname);
163 * runs a query and returns the result
165 * @uses PMA_DBI_QUERY_STORE
166 * @uses PMA_DBI_QUERY_UNBUFFERED
167 * @uses $GLOBALS['userlink']
168 * @uses MYSQLI_STORE_RESULT
169 * @uses MYSQLI_USE_RESULT
170 * @uses mysqli_query()
171 * @uses defined()
172 * @param string $query query to execute
173 * @param object mysqli $link mysqli object
174 * @param integer $options
175 * @return mixed true, false or result object
177 function PMA_DBI_try_query($query, $link = null, $options = 0)
179 if ($options == ($options | PMA_DBI_QUERY_STORE)) {
180 $method = MYSQLI_STORE_RESULT;
181 } elseif ($options == ($options | PMA_DBI_QUERY_UNBUFFERED)) {
182 $method = MYSQLI_USE_RESULT;
183 } else {
184 $method = 0;
187 if (empty($link)) {
188 if (isset($GLOBALS['userlink'])) {
189 $link = $GLOBALS['userlink'];
190 } else {
191 return false;
195 if ($GLOBALS['cfg']['DBG']['sql']) {
196 $time = microtime(true);
198 $r = mysqli_query($link, $query, $method);
199 if ($GLOBALS['cfg']['DBG']['sql']) {
200 $time = microtime(true) - $time;
202 $hash = md5($query);
204 if (isset($_SESSION['debug']['queries'][$hash])) {
205 $_SESSION['debug']['queries'][$hash]['count']++;
206 } else {
207 $_SESSION['debug']['queries'][$hash] = array();
208 $_SESSION['debug']['queries'][$hash]['count'] = 1;
209 $_SESSION['debug']['queries'][$hash]['query'] = $query;
210 $_SESSION['debug']['queries'][$hash]['time'] = $time;
213 $trace = array();
214 foreach (debug_backtrace() as $trace_step) {
215 $trace[] = PMA_Error::relPath($trace_step['file']) . '#'
216 . $trace_step['line'] . ': '
217 . (isset($trace_step['class']) ? $trace_step['class'] : '')
218 //. (isset($trace_step['object']) ? get_class($trace_step['object']) : '')
219 . (isset($trace_step['type']) ? $trace_step['type'] : '')
220 . (isset($trace_step['function']) ? $trace_step['function'] : '')
221 . '('
222 . (isset($trace_step['params']) ? implode(', ', $trace_step['params']) : '')
223 . ')'
226 $_SESSION['debug']['queries'][$hash]['trace'][] = $trace;
229 if ($r != FALSE && PMA_Tracker::isActive() == TRUE ) {
230 PMA_Tracker::handleQuery($query);
233 return $r;
235 // From the PHP manual:
236 // "note: returns true on success or false on failure. For SELECT,
237 // SHOW, DESCRIBE or EXPLAIN, mysqli_query() will return a result object"
238 // so, do not use the return value to feed mysqli_num_rows() if it's
239 // a boolean
243 * returns array of rows with associative and numeric keys from $result
245 * @uses mysqli_fetch_array()
246 * @uses MYSQLI_BOTH
247 * @param object mysqli result $result
248 * @return array result rows
250 function PMA_DBI_fetch_array($result)
252 return mysqli_fetch_array($result, MYSQLI_BOTH);
256 * returns array of rows with associative keys from $result
258 * @uses mysqli_fetch_array()
259 * @uses MYSQLI_ASSOC
260 * @param object mysqli result $result
261 * @return array result rows
263 function PMA_DBI_fetch_assoc($result)
265 return mysqli_fetch_array($result, MYSQLI_ASSOC);
269 * returns array of rows with numeric keys from $result
271 * @uses mysqli_fetch_array()
272 * @uses MYSQLI_NUM
273 * @param object mysqli result $result
274 * @return array result rows
276 function PMA_DBI_fetch_row($result)
278 return mysqli_fetch_array($result, MYSQLI_NUM);
282 * Adjusts the result pointer to an arbitrary row in the result
284 * @uses mysqli_data_seek()
285 * @param $result
286 * @param $offset
287 * @return boolean true on success, false on failure
289 function PMA_DBI_data_seek($result, $offset)
291 return mysqli_data_seek($result, $offset);
295 * Frees the memory associated with the results
297 * @uses mysqli_result
298 * @uses func_get_args()
299 * @uses mysqli_free_result()
300 * @param result $result,... one or more mysql result resources
302 function PMA_DBI_free_result()
304 foreach (func_get_args() as $result) {
305 if ($result instanceof mysqli_result) {
306 mysqli_free_result($result);
312 * Returns a string representing the type of connection used
313 * @uses mysqli_get_host_info()
314 * @uses $GLOBALS['userlink'] as default for $link
315 * @param resource $link mysql link
316 * @return string type of connection used
318 function PMA_DBI_get_host_info($link = null)
320 if (null === $link) {
321 if (isset($GLOBALS['userlink'])) {
322 $link = $GLOBALS['userlink'];
323 } else {
324 return false;
327 return mysqli_get_host_info($link);
331 * Returns the version of the MySQL protocol used
332 * @uses mysqli_get_proto_info()
333 * @uses $GLOBALS['userlink'] as default for $link
334 * @param resource $link mysql link
335 * @return integer version of the MySQL protocol used
337 function PMA_DBI_get_proto_info($link = null)
339 if (null === $link) {
340 if (isset($GLOBALS['userlink'])) {
341 $link = $GLOBALS['userlink'];
342 } else {
343 return false;
346 return mysqli_get_proto_info($link);
350 * returns a string that represents the client library version
351 * @uses mysqli_get_client_info()
352 * @return string MySQL client library version
354 function PMA_DBI_get_client_info()
356 return mysqli_get_client_info();
360 * returns last error message or false if no errors occured
362 * @uses PMA_DBI_convert_message()
363 * @uses $GLOBALS['errno']
364 * @uses $GLOBALS['userlink']
365 * @uses mysqli_errno()
366 * @uses mysqli_error()
367 * @uses mysqli_connect_errno()
368 * @uses mysqli_connect_error()
369 * @uses defined()
370 * @param resource $link mysql link
371 * @return string|boolean $error or false
373 function PMA_DBI_getError($link = null)
375 $GLOBALS['errno'] = 0;
377 /* Treat false same as null because of controllink */
378 if ($link === false) {
379 $link = null;
382 if (null === $link && isset($GLOBALS['userlink'])) {
383 $link =& $GLOBALS['userlink'];
384 // Do not stop now. We still can get the error code
385 // with mysqli_connect_errno()
386 // } else {
387 // return false;
390 if (null !== $link) {
391 $error_number = mysqli_errno($link);
392 $error_message = mysqli_error($link);
393 } else {
394 $error_number = mysqli_connect_errno();
395 $error_message = mysqli_connect_error();
397 if (0 == $error_number) {
398 return false;
401 // keep the error number for further check after the call to PMA_DBI_getError()
402 $GLOBALS['errno'] = $error_number;
404 if (! empty($error_message)) {
405 $error_message = PMA_DBI_convert_message($error_message);
408 $error_message = htmlspecialchars($error_message);
410 if ($error_number == 2002) {
411 $error = '#' . ((string) $error_number) . ' - ' . __('The server is not responding') . ' ' . __('(or the local MySQL server\'s socket is not correctly configured)');
412 } else {
413 $error = '#' . ((string) $error_number) . ' - ' . $error_message;
415 return $error;
420 * @param object mysqli result $result
422 function PMA_DBI_num_rows($result)
424 // see the note for PMA_DBI_try_query();
425 if (!is_bool($result)) {
426 return @mysqli_num_rows($result);
427 } else {
428 return 0;
433 * returns last inserted auto_increment id for given $link or $GLOBALS['userlink']
435 * @uses $GLOBALS['userlink']
436 * @uses mysqli_insert_id()
437 * @param object mysqli $link the mysqli object
438 * @return string ineteger
440 function PMA_DBI_insert_id($link = '')
442 if (empty($link)) {
443 if (isset($GLOBALS['userlink'])) {
444 $link = $GLOBALS['userlink'];
445 } else {
446 return false;
449 return mysqli_insert_id($link);
453 * returns the number of rows affected by last query
455 * @uses $GLOBALS['userlink']
456 * @uses mysqli_affected_rows()
457 * @param object mysqli $link the mysqli object
458 * @return string integer
460 function PMA_DBI_affected_rows($link = null)
462 if (empty($link)) {
463 if (isset($GLOBALS['userlink'])) {
464 $link = $GLOBALS['userlink'];
465 } else {
466 return false;
469 return mysqli_affected_rows($link);
473 * returns metainfo for fields in $result
475 * @todo preserve orignal flags value
476 * @uses PMA_DBI_field_flags()
477 * @uses MYSQLI_TYPE_*
478 * @uses MYSQLI_MULTIPLE_KEY_FLAG
479 * @uses MYSQLI_PRI_KEY_FLAG
480 * @uses MYSQLI_UNIQUE_KEY_FLAG
481 * @uses MYSQLI_NOT_NULL_FLAG
482 * @uses MYSQLI_UNSIGNED_FLAG
483 * @uses MYSQLI_ZEROFILL_FLAG
484 * @uses MYSQLI_NUM_FLAG
485 * @uses MYSQLI_TYPE_BLOB
486 * @uses MYSQLI_BLOB_FLAG
487 * @uses defined()
488 * @uses mysqli_fetch_fields()
489 * @uses is_array()
490 * @param object mysqli result $result
491 * @return array meta info for fields in $result
493 function PMA_DBI_get_fields_meta($result)
495 // Build an associative array for a type look up
496 $typeAr = array();
497 $typeAr[MYSQLI_TYPE_DECIMAL] = 'real';
498 $typeAr[MYSQLI_TYPE_NEWDECIMAL] = 'real';
499 $typeAr[MYSQLI_TYPE_BIT] = 'int';
500 $typeAr[MYSQLI_TYPE_TINY] = 'int';
501 $typeAr[MYSQLI_TYPE_SHORT] = 'int';
502 $typeAr[MYSQLI_TYPE_LONG] = 'int';
503 $typeAr[MYSQLI_TYPE_FLOAT] = 'real';
504 $typeAr[MYSQLI_TYPE_DOUBLE] = 'real';
505 $typeAr[MYSQLI_TYPE_NULL] = 'null';
506 $typeAr[MYSQLI_TYPE_TIMESTAMP] = 'timestamp';
507 $typeAr[MYSQLI_TYPE_LONGLONG] = 'int';
508 $typeAr[MYSQLI_TYPE_INT24] = 'int';
509 $typeAr[MYSQLI_TYPE_DATE] = 'date';
510 $typeAr[MYSQLI_TYPE_TIME] = 'time';
511 $typeAr[MYSQLI_TYPE_DATETIME] = 'datetime';
512 $typeAr[MYSQLI_TYPE_YEAR] = 'year';
513 $typeAr[MYSQLI_TYPE_NEWDATE] = 'date';
514 $typeAr[MYSQLI_TYPE_ENUM] = 'unknown';
515 $typeAr[MYSQLI_TYPE_SET] = 'unknown';
516 $typeAr[MYSQLI_TYPE_TINY_BLOB] = 'blob';
517 $typeAr[MYSQLI_TYPE_MEDIUM_BLOB] = 'blob';
518 $typeAr[MYSQLI_TYPE_LONG_BLOB] = 'blob';
519 $typeAr[MYSQLI_TYPE_BLOB] = 'blob';
520 $typeAr[MYSQLI_TYPE_VAR_STRING] = 'string';
521 $typeAr[MYSQLI_TYPE_STRING] = 'string';
522 // MySQL returns MYSQLI_TYPE_STRING for CHAR
523 // and MYSQLI_TYPE_CHAR === MYSQLI_TYPE_TINY
524 // so this would override TINYINT and mark all TINYINT as string
525 // https://sf.net/tracker/?func=detail&aid=1532111&group_id=23067&atid=377408
526 //$typeAr[MYSQLI_TYPE_CHAR] = 'string';
527 $typeAr[MYSQLI_TYPE_GEOMETRY] = 'unknown';
528 $typeAr[MYSQLI_TYPE_BIT] = 'bit';
530 $fields = mysqli_fetch_fields($result);
532 // this happens sometimes (seen under MySQL 4.0.25)
533 if (!is_array($fields)) {
534 return false;
537 foreach ($fields as $k => $field) {
538 $fields[$k]->_type = $field->type;
539 $fields[$k]->type = $typeAr[$field->type];
540 $fields[$k]->_flags = $field->flags;
541 $fields[$k]->flags = PMA_DBI_field_flags($result, $k);
543 // Enhance the field objects for mysql-extension compatibilty
544 //$flags = explode(' ', $fields[$k]->flags);
545 //array_unshift($flags, 'dummy');
546 $fields[$k]->multiple_key
547 = (int) (bool) ($fields[$k]->_flags & MYSQLI_MULTIPLE_KEY_FLAG);
548 $fields[$k]->primary_key
549 = (int) (bool) ($fields[$k]->_flags & MYSQLI_PRI_KEY_FLAG);
550 $fields[$k]->unique_key
551 = (int) (bool) ($fields[$k]->_flags & MYSQLI_UNIQUE_KEY_FLAG);
552 $fields[$k]->not_null
553 = (int) (bool) ($fields[$k]->_flags & MYSQLI_NOT_NULL_FLAG);
554 $fields[$k]->unsigned
555 = (int) (bool) ($fields[$k]->_flags & MYSQLI_UNSIGNED_FLAG);
556 $fields[$k]->zerofill
557 = (int) (bool) ($fields[$k]->_flags & MYSQLI_ZEROFILL_FLAG);
558 $fields[$k]->numeric
559 = (int) (bool) ($fields[$k]->_flags & MYSQLI_NUM_FLAG);
560 $fields[$k]->blob
561 = (int) (bool) ($fields[$k]->_flags & MYSQLI_BLOB_FLAG);
563 return $fields;
567 * return number of fields in given $result
569 * @param object mysqli result $result
570 * @return integer field count
572 function PMA_DBI_num_fields($result)
574 return mysqli_num_fields($result);
578 * returns the length of the given field $i in $result
580 * @uses mysqli_fetch_field_direct()
581 * @param object mysqli result $result
582 * @param integer $i field
583 * @return integer length of field
585 function PMA_DBI_field_len($result, $i)
587 return mysqli_fetch_field_direct($result, $i)->length;
591 * returns name of $i. field in $result
593 * @uses mysqli_fetch_field_direct()
594 * @param object mysqli result $result
595 * @param integer $i field
596 * @return string name of $i. field in $result
598 function PMA_DBI_field_name($result, $i)
600 return mysqli_fetch_field_direct($result, $i)->name;
604 * returns concatenated string of human readable field flags
606 * @uses MYSQLI_UNIQUE_KEY_FLAG
607 * @uses MYSQLI_NUM_FLAG
608 * @uses MYSQLI_PART_KEY_FLAG
609 * @uses MYSQLI_TYPE_SET
610 * @uses MYSQLI_TIMESTAMP_FLAG
611 * @uses MYSQLI_AUTO_INCREMENT_FLAG
612 * @uses MYSQLI_TYPE_ENUM
613 * @uses MYSQLI_ZEROFILL_FLAG
614 * @uses MYSQLI_UNSIGNED_FLAG
615 * @uses MYSQLI_BLOB_FLAG
616 * @uses MYSQLI_MULTIPLE_KEY_FLAG
617 * @uses MYSQLI_UNIQUE_KEY_FLAG
618 * @uses MYSQLI_PRI_KEY_FLAG
619 * @uses MYSQLI_NOT_NULL_FLAG
620 * @uses MYSQLI_TYPE_BLOB
621 * @uses MYSQLI_TYPE_MEDIUM_BLOB
622 * @uses MYSQLI_TYPE_LONG_BLOB
623 * @uses MYSQLI_TYPE_VAR_STRING
624 * @uses MYSQLI_TYPE_STRING
625 * @uses mysqli_fetch_field_direct()
626 * @param object mysqli result $result
627 * @param integer $i field
628 * @return string field flags
630 function PMA_DBI_field_flags($result, $i)
632 // This is missing from PHP 5.2.5, see http://bugs.php.net/bug.php?id=44846
633 if (! defined('MYSQLI_ENUM_FLAG')) {
634 define('MYSQLI_ENUM_FLAG', 256); // see MySQL source include/mysql_com.h
636 $f = mysqli_fetch_field_direct($result, $i);
637 $type = $f->type;
638 $charsetnr = $f->charsetnr;
639 $f = $f->flags;
640 $flags = '';
641 if ($f & MYSQLI_UNIQUE_KEY_FLAG) { $flags .= 'unique ';}
642 if ($f & MYSQLI_NUM_FLAG) { $flags .= 'num ';}
643 if ($f & MYSQLI_PART_KEY_FLAG) { $flags .= 'part_key ';}
644 if ($f & MYSQLI_SET_FLAG) { $flags .= 'set ';}
645 if ($f & MYSQLI_TIMESTAMP_FLAG) { $flags .= 'timestamp ';}
646 if ($f & MYSQLI_AUTO_INCREMENT_FLAG) { $flags .= 'auto_increment ';}
647 if ($f & MYSQLI_ENUM_FLAG) { $flags .= 'enum ';}
648 // See http://dev.mysql.com/doc/refman/6.0/en/c-api-datatypes.html:
649 // to determine if a string is binary, we should not use MYSQLI_BINARY_FLAG
650 // but instead the charsetnr member of the MYSQL_FIELD
651 // structure. Watch out: some types like DATE returns 63 in charsetnr
652 // so we have to check also the type.
653 // Unfortunately there is no equivalent in the mysql extension.
654 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 ';}
655 if ($f & MYSQLI_ZEROFILL_FLAG) { $flags .= 'zerofill ';}
656 if ($f & MYSQLI_UNSIGNED_FLAG) { $flags .= 'unsigned ';}
657 if ($f & MYSQLI_BLOB_FLAG) { $flags .= 'blob ';}
658 if ($f & MYSQLI_MULTIPLE_KEY_FLAG) { $flags .= 'multiple_key ';}
659 if ($f & MYSQLI_UNIQUE_KEY_FLAG) { $flags .= 'unique_key ';}
660 if ($f & MYSQLI_PRI_KEY_FLAG) { $flags .= 'primary_key ';}
661 if ($f & MYSQLI_NOT_NULL_FLAG) { $flags .= 'not_null ';}
662 return trim($flags);