2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Interface to the improved MySQL extension (MySQLi)
6 * @package phpMyAdmin-DBI-MySQLi
8 if (! defined('PHPMYADMIN')) {
12 require_once './libraries/logging.lib.php';
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])));
24 * some PHP versions are reporting extra messages like "No index used in query"
27 mysqli_report(MYSQLI_REPORT_OFF
);
30 * some older mysql client libs are missing these constants ...
32 if (! defined('MYSQLI_BINARY_FLAG')) {
33 define('MYSQLI_BINARY_FLAG', 128);
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);
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
56 * @uses mysqli_options()
57 * @uses mysqli_real_connect()
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)
69 $server_port = (empty($server['port']))
71 : (int)$server['port'];
72 $server_socket = (empty($server['socket']))
75 $server['host'] = (empty($server['host']))
79 $server_port = (empty($GLOBALS['cfg']['Server']['port']))
81 : (int) $GLOBALS['cfg']['Server']['port'];
82 $server_socket = (empty($GLOBALS['cfg']['Server']['socket']))
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);
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
;
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);
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
);
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');
135 PMA_DBI_postConnect($link, $is_controluser);
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)
153 if (isset($GLOBALS['userlink'])) {
154 $link = $GLOBALS['userlink'];
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()
172 * @param string $query query to execute
173 * @param object mysqli $link mysqli object
174 * @param integer $options
175 * @param boolean $cache_affected_rows
176 * @return mixed true, false or result object
178 function PMA_DBI_try_query($query, $link = null, $options = 0, $cache_affected_rows = true)
180 if ($options == ($options | PMA_DBI_QUERY_STORE
)) {
181 $method = MYSQLI_STORE_RESULT
;
182 } elseif ($options == ($options | PMA_DBI_QUERY_UNBUFFERED
)) {
183 $method = MYSQLI_USE_RESULT
;
189 if (isset($GLOBALS['userlink'])) {
190 $link = $GLOBALS['userlink'];
196 if ($GLOBALS['cfg']['DBG']['sql']) {
197 $time = microtime(true);
199 $r = mysqli_query($link, $query, $method);
201 if ($cache_affected_rows) {
202 $GLOBALS['cached_affected_rows'] = PMA_DBI_affected_rows($link, $get_from_cache = false);
205 if ($GLOBALS['cfg']['DBG']['sql']) {
206 $time = microtime(true) - $time;
210 if (isset($_SESSION['debug']['queries'][$hash])) {
211 $_SESSION['debug']['queries'][$hash]['count']++
;
213 $_SESSION['debug']['queries'][$hash] = array();
214 $_SESSION['debug']['queries'][$hash]['count'] = 1;
215 $_SESSION['debug']['queries'][$hash]['query'] = $query;
216 $_SESSION['debug']['queries'][$hash]['time'] = $time;
220 foreach (debug_backtrace() as $trace_step) {
221 $trace[] = PMA_Error
::relPath($trace_step['file']) . '#'
222 . $trace_step['line'] . ': '
223 . (isset($trace_step['class']) ?
$trace_step['class'] : '')
224 //. (isset($trace_step['object']) ? get_class($trace_step['object']) : '')
225 . (isset($trace_step['type']) ?
$trace_step['type'] : '')
226 . (isset($trace_step['function']) ?
$trace_step['function'] : '')
228 . (isset($trace_step['params']) ?
implode(', ', $trace_step['params']) : '')
232 $_SESSION['debug']['queries'][$hash]['trace'][] = $trace;
235 if ($r != FALSE && PMA_Tracker
::isActive() == TRUE ) {
236 PMA_Tracker
::handleQuery($query);
241 // From the PHP manual:
242 // "note: returns true on success or false on failure. For SELECT,
243 // SHOW, DESCRIBE or EXPLAIN, mysqli_query() will return a result object"
244 // so, do not use the return value to feed mysqli_num_rows() if it's
249 * returns array of rows with associative and numeric keys from $result
251 * @uses mysqli_fetch_array()
253 * @param object mysqli result $result
254 * @return array result rows
256 function PMA_DBI_fetch_array($result)
258 return mysqli_fetch_array($result, MYSQLI_BOTH
);
262 * returns array of rows with associative keys from $result
264 * @uses mysqli_fetch_array()
266 * @param object mysqli result $result
267 * @return array result rows
269 function PMA_DBI_fetch_assoc($result)
271 return mysqli_fetch_array($result, MYSQLI_ASSOC
);
275 * returns array of rows with numeric keys from $result
277 * @uses mysqli_fetch_array()
279 * @param object mysqli result $result
280 * @return array result rows
282 function PMA_DBI_fetch_row($result)
284 return mysqli_fetch_array($result, MYSQLI_NUM
);
288 * Adjusts the result pointer to an arbitrary row in the result
290 * @uses mysqli_data_seek()
293 * @return boolean true on success, false on failure
295 function PMA_DBI_data_seek($result, $offset)
297 return mysqli_data_seek($result, $offset);
301 * Frees the memory associated with the results
303 * @uses mysqli_result
304 * @uses func_get_args()
305 * @uses mysqli_free_result()
306 * @param result $result,... one or more mysql result resources
308 function PMA_DBI_free_result()
310 foreach (func_get_args() as $result) {
311 if ($result instanceof mysqli_result
) {
312 mysqli_free_result($result);
318 * Returns a string representing the type of connection used
319 * @uses mysqli_get_host_info()
320 * @uses $GLOBALS['userlink'] as default for $link
321 * @param resource $link mysql link
322 * @return string type of connection used
324 function PMA_DBI_get_host_info($link = null)
326 if (null === $link) {
327 if (isset($GLOBALS['userlink'])) {
328 $link = $GLOBALS['userlink'];
333 return mysqli_get_host_info($link);
337 * Returns the version of the MySQL protocol used
338 * @uses mysqli_get_proto_info()
339 * @uses $GLOBALS['userlink'] as default for $link
340 * @param resource $link mysql link
341 * @return integer version of the MySQL protocol used
343 function PMA_DBI_get_proto_info($link = null)
345 if (null === $link) {
346 if (isset($GLOBALS['userlink'])) {
347 $link = $GLOBALS['userlink'];
352 return mysqli_get_proto_info($link);
356 * returns a string that represents the client library version
357 * @uses mysqli_get_client_info()
358 * @return string MySQL client library version
360 function PMA_DBI_get_client_info()
362 return mysqli_get_client_info();
366 * returns last error message or false if no errors occured
368 * @uses PMA_DBI_convert_message()
369 * @uses $GLOBALS['errno']
370 * @uses $GLOBALS['userlink']
371 * @uses mysqli_errno()
372 * @uses mysqli_error()
373 * @uses mysqli_connect_errno()
374 * @uses mysqli_connect_error()
376 * @param resource $link mysql link
377 * @return string|boolean $error or false
379 function PMA_DBI_getError($link = null)
381 $GLOBALS['errno'] = 0;
383 /* Treat false same as null because of controllink */
384 if ($link === false) {
388 if (null === $link && isset($GLOBALS['userlink'])) {
389 $link =& $GLOBALS['userlink'];
390 // Do not stop now. We still can get the error code
391 // with mysqli_connect_errno()
396 if (null !== $link) {
397 $error_number = mysqli_errno($link);
398 $error_message = mysqli_error($link);
400 $error_number = mysqli_connect_errno();
401 $error_message = mysqli_connect_error();
403 if (0 == $error_number) {
407 // keep the error number for further check after the call to PMA_DBI_getError()
408 $GLOBALS['errno'] = $error_number;
410 if (! empty($error_message)) {
411 $error_message = PMA_DBI_convert_message($error_message);
414 $error_message = htmlspecialchars($error_message);
416 if ($error_number == 2002) {
417 $error = '#' . ((string) $error_number) . ' - ' . __('The server is not responding') . ' ' . __('(or the local MySQL server\'s socket is not correctly configured)');
419 $error = '#' . ((string) $error_number) . ' - ' . $error_message;
426 * @param object mysqli result $result
428 function PMA_DBI_num_rows($result)
430 // see the note for PMA_DBI_try_query();
431 if (!is_bool($result)) {
432 return @mysqli_num_rows
($result);
439 * returns last inserted auto_increment id for given $link or $GLOBALS['userlink']
441 * @uses $GLOBALS['userlink']
442 * @uses mysqli_insert_id()
443 * @param object mysqli $link the mysqli object
444 * @return string ineteger
446 function PMA_DBI_insert_id($link = '')
449 if (isset($GLOBALS['userlink'])) {
450 $link = $GLOBALS['userlink'];
455 // When no controluser is defined, using mysqli_insert_id($link)
456 // does not always return the last insert id due to a mixup with
457 // the tracking mechanism, but this works:
458 return PMA_DBI_fetch_value('SELECT LAST_INSERT_ID();', 0, 0, $link);
459 // Curiously, this problem does not happen with the mysql extension but
460 // there is another problem with BIGINT primary keys so PMA_DBI_insert_id()
461 // in the mysql extension also uses this logic.
465 * returns the number of rows affected by last query
467 * @uses $GLOBALS['userlink']
468 * @uses mysqli_affected_rows()
469 * @param object mysqli $link the mysqli object
470 * @param boolean $get_from_cache
471 * @return string integer
473 function PMA_DBI_affected_rows($link = null, $get_from_cache = true)
476 if (isset($GLOBALS['userlink'])) {
477 $link = $GLOBALS['userlink'];
482 if ($get_from_cache) {
483 return $GLOBALS['cached_affected_rows'];
485 return mysqli_affected_rows($link);
490 * returns metainfo for fields in $result
492 * @todo preserve orignal flags value
493 * @uses PMA_DBI_field_flags()
494 * @uses MYSQLI_TYPE_*
495 * @uses MYSQLI_MULTIPLE_KEY_FLAG
496 * @uses MYSQLI_PRI_KEY_FLAG
497 * @uses MYSQLI_UNIQUE_KEY_FLAG
498 * @uses MYSQLI_NOT_NULL_FLAG
499 * @uses MYSQLI_UNSIGNED_FLAG
500 * @uses MYSQLI_ZEROFILL_FLAG
501 * @uses MYSQLI_NUM_FLAG
502 * @uses MYSQLI_TYPE_BLOB
503 * @uses MYSQLI_BLOB_FLAG
505 * @uses mysqli_fetch_fields()
507 * @param object mysqli result $result
508 * @return array meta info for fields in $result
510 function PMA_DBI_get_fields_meta($result)
512 // Build an associative array for a type look up
514 $typeAr[MYSQLI_TYPE_DECIMAL
] = 'real';
515 $typeAr[MYSQLI_TYPE_NEWDECIMAL
] = 'real';
516 $typeAr[MYSQLI_TYPE_BIT
] = 'int';
517 $typeAr[MYSQLI_TYPE_TINY
] = 'int';
518 $typeAr[MYSQLI_TYPE_SHORT
] = 'int';
519 $typeAr[MYSQLI_TYPE_LONG
] = 'int';
520 $typeAr[MYSQLI_TYPE_FLOAT
] = 'real';
521 $typeAr[MYSQLI_TYPE_DOUBLE
] = 'real';
522 $typeAr[MYSQLI_TYPE_NULL
] = 'null';
523 $typeAr[MYSQLI_TYPE_TIMESTAMP
] = 'timestamp';
524 $typeAr[MYSQLI_TYPE_LONGLONG
] = 'int';
525 $typeAr[MYSQLI_TYPE_INT24
] = 'int';
526 $typeAr[MYSQLI_TYPE_DATE
] = 'date';
527 $typeAr[MYSQLI_TYPE_TIME
] = 'time';
528 $typeAr[MYSQLI_TYPE_DATETIME
] = 'datetime';
529 $typeAr[MYSQLI_TYPE_YEAR
] = 'year';
530 $typeAr[MYSQLI_TYPE_NEWDATE
] = 'date';
531 $typeAr[MYSQLI_TYPE_ENUM
] = 'unknown';
532 $typeAr[MYSQLI_TYPE_SET
] = 'unknown';
533 $typeAr[MYSQLI_TYPE_TINY_BLOB
] = 'blob';
534 $typeAr[MYSQLI_TYPE_MEDIUM_BLOB
] = 'blob';
535 $typeAr[MYSQLI_TYPE_LONG_BLOB
] = 'blob';
536 $typeAr[MYSQLI_TYPE_BLOB
] = 'blob';
537 $typeAr[MYSQLI_TYPE_VAR_STRING
] = 'string';
538 $typeAr[MYSQLI_TYPE_STRING
] = 'string';
539 // MySQL returns MYSQLI_TYPE_STRING for CHAR
540 // and MYSQLI_TYPE_CHAR === MYSQLI_TYPE_TINY
541 // so this would override TINYINT and mark all TINYINT as string
542 // https://sf.net/tracker/?func=detail&aid=1532111&group_id=23067&atid=377408
543 //$typeAr[MYSQLI_TYPE_CHAR] = 'string';
544 $typeAr[MYSQLI_TYPE_GEOMETRY
] = 'geometry';
545 $typeAr[MYSQLI_TYPE_BIT
] = 'bit';
547 $fields = mysqli_fetch_fields($result);
549 // this happens sometimes (seen under MySQL 4.0.25)
550 if (!is_array($fields)) {
554 foreach ($fields as $k => $field) {
555 $fields[$k]->_type
= $field->type
;
556 $fields[$k]->type
= $typeAr[$field->type
];
557 $fields[$k]->_flags
= $field->flags
;
558 $fields[$k]->flags
= PMA_DBI_field_flags($result, $k);
560 // Enhance the field objects for mysql-extension compatibilty
561 //$flags = explode(' ', $fields[$k]->flags);
562 //array_unshift($flags, 'dummy');
563 $fields[$k]->multiple_key
564 = (int) (bool) ($fields[$k]->_flags
& MYSQLI_MULTIPLE_KEY_FLAG
);
565 $fields[$k]->primary_key
566 = (int) (bool) ($fields[$k]->_flags
& MYSQLI_PRI_KEY_FLAG
);
567 $fields[$k]->unique_key
568 = (int) (bool) ($fields[$k]->_flags
& MYSQLI_UNIQUE_KEY_FLAG
);
569 $fields[$k]->not_null
570 = (int) (bool) ($fields[$k]->_flags
& MYSQLI_NOT_NULL_FLAG
);
571 $fields[$k]->unsigned
572 = (int) (bool) ($fields[$k]->_flags
& MYSQLI_UNSIGNED_FLAG
);
573 $fields[$k]->zerofill
574 = (int) (bool) ($fields[$k]->_flags
& MYSQLI_ZEROFILL_FLAG
);
576 = (int) (bool) ($fields[$k]->_flags
& MYSQLI_NUM_FLAG
);
578 = (int) (bool) ($fields[$k]->_flags
& MYSQLI_BLOB_FLAG
);
584 * return number of fields in given $result
586 * @param object mysqli result $result
587 * @return integer field count
589 function PMA_DBI_num_fields($result)
591 return mysqli_num_fields($result);
595 * returns the length of the given field $i in $result
597 * @uses mysqli_fetch_field_direct()
598 * @param object mysqli result $result
599 * @param integer $i field
600 * @return integer length of field
602 function PMA_DBI_field_len($result, $i)
604 return mysqli_fetch_field_direct($result, $i)->length
;
608 * returns name of $i. field in $result
610 * @uses mysqli_fetch_field_direct()
611 * @param object mysqli result $result
612 * @param integer $i field
613 * @return string name of $i. field in $result
615 function PMA_DBI_field_name($result, $i)
617 return mysqli_fetch_field_direct($result, $i)->name
;
621 * returns concatenated string of human readable field flags
623 * @uses MYSQLI_UNIQUE_KEY_FLAG
624 * @uses MYSQLI_NUM_FLAG
625 * @uses MYSQLI_PART_KEY_FLAG
626 * @uses MYSQLI_TYPE_SET
627 * @uses MYSQLI_TIMESTAMP_FLAG
628 * @uses MYSQLI_AUTO_INCREMENT_FLAG
629 * @uses MYSQLI_TYPE_ENUM
630 * @uses MYSQLI_ZEROFILL_FLAG
631 * @uses MYSQLI_UNSIGNED_FLAG
632 * @uses MYSQLI_BLOB_FLAG
633 * @uses MYSQLI_MULTIPLE_KEY_FLAG
634 * @uses MYSQLI_UNIQUE_KEY_FLAG
635 * @uses MYSQLI_PRI_KEY_FLAG
636 * @uses MYSQLI_NOT_NULL_FLAG
637 * @uses MYSQLI_TYPE_BLOB
638 * @uses MYSQLI_TYPE_MEDIUM_BLOB
639 * @uses MYSQLI_TYPE_LONG_BLOB
640 * @uses MYSQLI_TYPE_VAR_STRING
641 * @uses MYSQLI_TYPE_STRING
642 * @uses mysqli_fetch_field_direct()
643 * @param object mysqli result $result
644 * @param integer $i field
645 * @return string field flags
647 function PMA_DBI_field_flags($result, $i)
649 // This is missing from PHP 5.2.5, see http://bugs.php.net/bug.php?id=44846
650 if (! defined('MYSQLI_ENUM_FLAG')) {
651 define('MYSQLI_ENUM_FLAG', 256); // see MySQL source include/mysql_com.h
653 $f = mysqli_fetch_field_direct($result, $i);
655 $charsetnr = $f->charsetnr
;
658 if ($f & MYSQLI_UNIQUE_KEY_FLAG
) { $flags .= 'unique ';}
659 if ($f & MYSQLI_NUM_FLAG
) { $flags .= 'num ';}
660 if ($f & MYSQLI_PART_KEY_FLAG
) { $flags .= 'part_key ';}
661 if ($f & MYSQLI_SET_FLAG
) { $flags .= 'set ';}
662 if ($f & MYSQLI_TIMESTAMP_FLAG
) { $flags .= 'timestamp ';}
663 if ($f & MYSQLI_AUTO_INCREMENT_FLAG
) { $flags .= 'auto_increment ';}
664 if ($f & MYSQLI_ENUM_FLAG
) { $flags .= 'enum ';}
665 // See http://dev.mysql.com/doc/refman/6.0/en/c-api-datatypes.html:
666 // to determine if a string is binary, we should not use MYSQLI_BINARY_FLAG
667 // but instead the charsetnr member of the MYSQL_FIELD
668 // structure. Watch out: some types like DATE returns 63 in charsetnr
669 // so we have to check also the type.
670 // Unfortunately there is no equivalent in the mysql extension.
671 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 ';}
672 if ($f & MYSQLI_ZEROFILL_FLAG
) { $flags .= 'zerofill ';}
673 if ($f & MYSQLI_UNSIGNED_FLAG
) { $flags .= 'unsigned ';}
674 if ($f & MYSQLI_BLOB_FLAG
) { $flags .= 'blob ';}
675 if ($f & MYSQLI_MULTIPLE_KEY_FLAG
) { $flags .= 'multiple_key ';}
676 if ($f & MYSQLI_UNIQUE_KEY_FLAG
) { $flags .= 'unique_key ';}
677 if ($f & MYSQLI_PRI_KEY_FLAG
) { $flags .= 'primary_key ';}
678 if ($f & MYSQLI_NOT_NULL_FLAG
) { $flags .= 'not_null ';}