2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Interface to the improved MySQL extension (MySQLi)
6 * @package PhpMyAdmin-DBI
9 if (! defined('PHPMYADMIN')) {
13 require_once './libraries/logging.lib.php';
18 if (!defined('PMA_MYSQL_CLIENT_API')) {
19 $client_api = explode('.', mysqli_get_client_info());
21 'PMA_MYSQL_CLIENT_API',
24 $client_api[0], $client_api[1], intval($client_api[2])
31 * some PHP versions are reporting extra messages like "No index used in query"
34 mysqli_report(MYSQLI_REPORT_OFF
);
37 * some older mysql client libs are missing these constants ...
39 if (! defined('MYSQLI_BINARY_FLAG')) {
40 define('MYSQLI_BINARY_FLAG', 128);
44 * @see http://bugs.php.net/36007
46 if (! defined('MYSQLI_TYPE_NEWDECIMAL')) {
47 define('MYSQLI_TYPE_NEWDECIMAL', 246);
49 if (! defined('MYSQLI_TYPE_BIT')) {
50 define('MYSQLI_TYPE_BIT', 16);
54 if (! defined('MYSQLI_TYPE_VARCHAR')) {
55 define('MYSQLI_TYPE_VARCHAR', 15);
59 * Helper function for connecting to the database server
61 * @param mysqli $link connection link
62 * @param string $host mysql hostname
63 * @param string $user mysql user name
64 * @param string $password mysql user password
65 * @param string $dbname database name
66 * @param int $server_port server port
67 * @param string $server_socket server socket
68 * @param int $client_flags client flags of connection
69 * @param bool $persistent whether to use peristent connection
73 function PMA_DBI_real_connect(
74 $link, $host, $user, $password, $dbname, $server_port,
75 $server_socket, $client_flags = null, $persistent = false
79 // mysqli persistent connections only on PHP 5.3+
80 if (PMA_PHP_INT_VERSION
>= 50300) {
81 if ($cfg['PersistentConnections'] ||
$persistent) {
85 if ($client_flags === null) {
86 return @mysqli_real_connect
(
96 return @mysqli_real_connect
(
110 * connects to the database server
112 * @param string $user mysql user name
113 * @param string $password mysql user password
114 * @param bool $is_controluser whether this is a control user connection
115 * @param array $server host/port/socket/persistent
116 * @param bool $auxiliary_connection (when true, don't go back to login if
119 * @return mixed false on error or a mysqli object on success
121 function PMA_DBI_connect(
122 $user, $password, $is_controluser = false, $server = null,
123 $auxiliary_connection = false
128 $server_port = (empty($server['port']))
130 : (int)$server['port'];
131 $server_socket = (empty($server['socket']))
134 $server['host'] = (empty($server['host']))
138 $server_port = (empty($cfg['Server']['port']))
140 : (int) $cfg['Server']['port'];
141 $server_socket = (empty($cfg['Server']['socket']))
143 : $cfg['Server']['socket'];
146 // NULL enables connection to the default socket
148 $link = mysqli_init();
150 mysqli_options($link, MYSQLI_OPT_LOCAL_INFILE
, true);
154 /* Optionally compress connection */
155 if ($cfg['Server']['compress'] && defined('MYSQLI_CLIENT_COMPRESS')) {
156 $client_flags |
= MYSQLI_CLIENT_COMPRESS
;
159 /* Optionally enable SSL */
160 if ($cfg['Server']['ssl'] && defined('MYSQLI_CLIENT_SSL')) {
161 $client_flags |
= MYSQLI_CLIENT_SSL
;
165 $return_value = @PMA_DBI_real_connect
(
167 $cfg['Server']['host'],
175 // Retry with empty password if we're allowed to
176 if ($return_value == false
177 && isset($cfg['Server']['nopassword']) && $cfg['Server']['nopassword']
180 $return_value = @PMA_DBI_real_connect
(
182 $cfg['Server']['host'],
192 $return_value = @PMA_DBI_real_connect
(
203 if ($return_value == false) {
204 if ($is_controluser) {
206 __('Connection for controluser as defined in your configuration failed.'),
211 // we could be calling PMA_DBI_connect() to connect to another
212 // server, for example in the Synchronize feature, so do not
213 // go back to main login if it fails
214 if (! $auxiliary_connection) {
215 PMA_log_user($user, 'mysql-denied');
217 $auth_plugin->authFails();
222 PMA_DBI_postConnect($link, $is_controluser);
229 * selects given database
231 * @param string $dbname database name to select
232 * @param mysqli $link the mysqli object
236 function PMA_DBI_select_db($dbname, $link = null)
239 if (isset($GLOBALS['userlink'])) {
240 $link = $GLOBALS['userlink'];
245 return mysqli_select_db($link, $dbname);
249 * runs a query and returns the result
251 * @param string $query query to execute
252 * @param mysqli $link mysqli object
253 * @param int $options query options
255 * @return mysqli_result|bool
257 function PMA_DBI_real_query($query, $link, $options)
259 if ($options == ($options | PMA_DBI_QUERY_STORE
)) {
260 $method = MYSQLI_STORE_RESULT
;
261 } elseif ($options == ($options | PMA_DBI_QUERY_UNBUFFERED
)) {
262 $method = MYSQLI_USE_RESULT
;
267 return mysqli_query($link, $query, $method);
271 * Run the multi query and output the results
273 * @param mysqli $link mysqli object
274 * @param string $query multi query statement to execute
276 * @return mysqli_result collection | boolean(false)
278 function PMA_DBI_real_multi_query($link, $query)
280 return mysqli_multi_query($link, $query);
284 * returns array of rows with associative and numeric keys from $result
286 * @param mysqli_result $result result set identifier
290 function PMA_DBI_fetch_array($result)
292 return mysqli_fetch_array($result, MYSQLI_BOTH
);
296 * returns array of rows with associative keys from $result
298 * @param mysqli_result $result result set identifier
302 function PMA_DBI_fetch_assoc($result)
304 return mysqli_fetch_array($result, MYSQLI_ASSOC
);
308 * returns array of rows with numeric keys from $result
310 * @param mysqli_result $result result set identifier
314 function PMA_DBI_fetch_row($result)
316 return mysqli_fetch_array($result, MYSQLI_NUM
);
320 * Adjusts the result pointer to an arbitrary row in the result
322 * @param resource $result database result
323 * @param integer $offset offset to seek
325 * @return bool true on success, false on failure
327 function PMA_DBI_data_seek($result, $offset)
329 return mysqli_data_seek($result, $offset);
333 * Frees memory associated with the result
335 * @param mysqli_result $result database result
339 function PMA_DBI_free_result($result)
341 if ($result instanceof mysqli_result
) {
342 mysqli_free_result($result);
347 * Check if there are any more query results from a multi query
349 * @param mysqli $link the mysqli object
351 * @return bool true or false
353 function PMA_DBI_more_results($link = null)
356 if (isset($GLOBALS['userlink'])) {
357 $link = $GLOBALS['userlink'];
362 return mysqli_more_results($link);
366 * Prepare next result from multi_query
368 * @param mysqli $link the mysqli object
370 * @return bool true or false
372 function PMA_DBI_next_result($link = null)
375 if (isset($GLOBALS['userlink'])) {
376 $link = $GLOBALS['userlink'];
381 return mysqli_next_result($link);
385 * Store the result returned from multi query
387 * @return mixed false when empty results / result set when not empty
389 function PMA_DBI_store_result()
391 if (isset($GLOBALS['userlink'])) {
392 $link = $GLOBALS['userlink'];
396 return mysqli_store_result($link);
400 * Returns a string representing the type of connection used
402 * @param resource $link mysql link
404 * @return string type of connection used
406 function PMA_DBI_get_host_info($link = null)
408 if (null === $link) {
409 if (isset($GLOBALS['userlink'])) {
410 $link = $GLOBALS['userlink'];
415 return mysqli_get_host_info($link);
419 * Returns the version of the MySQL protocol used
421 * @param resource $link mysql link
423 * @return integer version of the MySQL protocol used
425 function PMA_DBI_get_proto_info($link = null)
427 if (null === $link) {
428 if (isset($GLOBALS['userlink'])) {
429 $link = $GLOBALS['userlink'];
434 return mysqli_get_proto_info($link);
438 * returns a string that represents the client library version
440 * @return string MySQL client library version
442 function PMA_DBI_get_client_info()
444 return mysqli_get_client_info();
448 * returns last error message or false if no errors occured
450 * @param resource $link mysql link
452 * @return string|bool $error or false
454 function PMA_DBI_getError($link = null)
456 $GLOBALS['errno'] = 0;
458 /* Treat false same as null because of controllink */
459 if ($link === false) {
463 if (null === $link && isset($GLOBALS['userlink'])) {
464 $link =& $GLOBALS['userlink'];
465 // Do not stop now. We still can get the error code
466 // with mysqli_connect_errno()
469 if (null !== $link) {
470 $error_number = mysqli_errno($link);
471 $error_message = mysqli_error($link);
473 $error_number = mysqli_connect_errno();
474 $error_message = mysqli_connect_error();
476 if (0 == $error_number) {
480 // keep the error number for further check after the call to PMA_DBI_getError()
481 $GLOBALS['errno'] = $error_number;
483 return PMA_DBI_formatError($error_number, $error_message);
487 * returns the number of rows returned by last query
489 * @param mysqli_result $result result set identifier
493 function PMA_DBI_num_rows($result)
495 // see the note for PMA_DBI_try_query();
496 if (!is_bool($result)) {
497 return @mysqli_num_rows
($result);
504 * returns last inserted auto_increment id for given $link or $GLOBALS['userlink']
506 * @param mysqli $link the mysqli object
510 function PMA_DBI_insert_id($link = null)
513 if (isset($GLOBALS['userlink'])) {
514 $link = $GLOBALS['userlink'];
519 // When no controluser is defined, using mysqli_insert_id($link)
520 // does not always return the last insert id due to a mixup with
521 // the tracking mechanism, but this works:
522 return PMA_DBI_fetch_value('SELECT LAST_INSERT_ID();', 0, 0, $link);
523 // Curiously, this problem does not happen with the mysql extension but
524 // there is another problem with BIGINT primary keys so PMA_DBI_insert_id()
525 // in the mysql extension also uses this logic.
529 * returns the number of rows affected by last query
531 * @param mysqli $link the mysqli object
532 * @param bool $get_from_cache whether to retrieve from cache
536 function PMA_DBI_affected_rows($link = null, $get_from_cache = true)
539 if (isset($GLOBALS['userlink'])) {
540 $link = $GLOBALS['userlink'];
545 if ($get_from_cache) {
546 return $GLOBALS['cached_affected_rows'];
548 return mysqli_affected_rows($link);
553 * returns metainfo for fields in $result
555 * @param mysqli_result $result result set identifier
557 * @return array meta info for fields in $result
559 function PMA_DBI_get_fields_meta($result)
561 // Build an associative array for a type look up
563 $typeAr[MYSQLI_TYPE_DECIMAL
] = 'real';
564 $typeAr[MYSQLI_TYPE_NEWDECIMAL
] = 'real';
565 $typeAr[MYSQLI_TYPE_BIT
] = 'int';
566 $typeAr[MYSQLI_TYPE_TINY
] = 'int';
567 $typeAr[MYSQLI_TYPE_SHORT
] = 'int';
568 $typeAr[MYSQLI_TYPE_LONG
] = 'int';
569 $typeAr[MYSQLI_TYPE_FLOAT
] = 'real';
570 $typeAr[MYSQLI_TYPE_DOUBLE
] = 'real';
571 $typeAr[MYSQLI_TYPE_NULL
] = 'null';
572 $typeAr[MYSQLI_TYPE_TIMESTAMP
] = 'timestamp';
573 $typeAr[MYSQLI_TYPE_LONGLONG
] = 'int';
574 $typeAr[MYSQLI_TYPE_INT24
] = 'int';
575 $typeAr[MYSQLI_TYPE_DATE
] = 'date';
576 $typeAr[MYSQLI_TYPE_TIME
] = 'time';
577 $typeAr[MYSQLI_TYPE_DATETIME
] = 'datetime';
578 $typeAr[MYSQLI_TYPE_YEAR
] = 'year';
579 $typeAr[MYSQLI_TYPE_NEWDATE
] = 'date';
580 $typeAr[MYSQLI_TYPE_ENUM
] = 'unknown';
581 $typeAr[MYSQLI_TYPE_SET
] = 'unknown';
582 $typeAr[MYSQLI_TYPE_TINY_BLOB
] = 'blob';
583 $typeAr[MYSQLI_TYPE_MEDIUM_BLOB
] = 'blob';
584 $typeAr[MYSQLI_TYPE_LONG_BLOB
] = 'blob';
585 $typeAr[MYSQLI_TYPE_BLOB
] = 'blob';
586 $typeAr[MYSQLI_TYPE_VAR_STRING
] = 'string';
587 $typeAr[MYSQLI_TYPE_STRING
] = 'string';
588 $typeAr[MYSQLI_TYPE_VARCHAR
] = 'string'; // for Drizzle
589 // MySQL returns MYSQLI_TYPE_STRING for CHAR
590 // and MYSQLI_TYPE_CHAR === MYSQLI_TYPE_TINY
591 // so this would override TINYINT and mark all TINYINT as string
592 // https://sourceforge.net/p/phpmyadmin/bugs/2205/
593 //$typeAr[MYSQLI_TYPE_CHAR] = 'string';
594 $typeAr[MYSQLI_TYPE_GEOMETRY
] = 'geometry';
595 $typeAr[MYSQLI_TYPE_BIT
] = 'bit';
597 $fields = mysqli_fetch_fields($result);
599 // this happens sometimes (seen under MySQL 4.0.25)
600 if (!is_array($fields)) {
604 foreach ($fields as $k => $field) {
605 $fields[$k]->_type
= $field->type
;
606 $fields[$k]->type
= $typeAr[$field->type
];
607 $fields[$k]->_flags
= $field->flags
;
608 $fields[$k]->flags
= PMA_DBI_field_flags($result, $k);
610 // Enhance the field objects for mysql-extension compatibilty
611 //$flags = explode(' ', $fields[$k]->flags);
612 //array_unshift($flags, 'dummy');
613 $fields[$k]->multiple_key
614 = (int) (bool) ($fields[$k]->_flags
& MYSQLI_MULTIPLE_KEY_FLAG
);
615 $fields[$k]->primary_key
616 = (int) (bool) ($fields[$k]->_flags
& MYSQLI_PRI_KEY_FLAG
);
617 $fields[$k]->unique_key
618 = (int) (bool) ($fields[$k]->_flags
& MYSQLI_UNIQUE_KEY_FLAG
);
619 $fields[$k]->not_null
620 = (int) (bool) ($fields[$k]->_flags
& MYSQLI_NOT_NULL_FLAG
);
621 $fields[$k]->unsigned
622 = (int) (bool) ($fields[$k]->_flags
& MYSQLI_UNSIGNED_FLAG
);
623 $fields[$k]->zerofill
624 = (int) (bool) ($fields[$k]->_flags
& MYSQLI_ZEROFILL_FLAG
);
626 = (int) (bool) ($fields[$k]->_flags
& MYSQLI_NUM_FLAG
);
628 = (int) (bool) ($fields[$k]->_flags
& MYSQLI_BLOB_FLAG
);
634 * return number of fields in given $result
636 * @param mysqli_result $result result set identifier
638 * @return int field count
640 function PMA_DBI_num_fields($result)
642 return mysqli_num_fields($result);
646 * returns the length of the given field $i in $result
648 * @param mysqli_result $result result set identifier
649 * @param int $i field
651 * @return int length of field
653 function PMA_DBI_field_len($result, $i)
655 return mysqli_fetch_field_direct($result, $i)->length
;
659 * returns name of $i. field in $result
661 * @param mysqli_result $result result set identifier
662 * @param int $i field
664 * @return string name of $i. field in $result
666 function PMA_DBI_field_name($result, $i)
668 return mysqli_fetch_field_direct($result, $i)->name
;
672 * returns concatenated string of human readable field flags
674 * @param mysqli_result $result result set identifier
675 * @param int $i field
677 * @return string field flags
679 function PMA_DBI_field_flags($result, $i)
681 // This is missing from PHP 5.2.5, see http://bugs.php.net/bug.php?id=44846
682 if (! defined('MYSQLI_ENUM_FLAG')) {
683 define('MYSQLI_ENUM_FLAG', 256); // see MySQL source include/mysql_com.h
685 $f = mysqli_fetch_field_direct($result, $i);
687 $charsetnr = $f->charsetnr
;
690 if ($f & MYSQLI_UNIQUE_KEY_FLAG
) {
693 if ($f & MYSQLI_NUM_FLAG
) {
696 if ($f & MYSQLI_PART_KEY_FLAG
) {
697 $flags .= 'part_key ';
699 if ($f & MYSQLI_SET_FLAG
) {
702 if ($f & MYSQLI_TIMESTAMP_FLAG
) {
703 $flags .= 'timestamp ';
705 if ($f & MYSQLI_AUTO_INCREMENT_FLAG
) {
706 $flags .= 'auto_increment ';
708 if ($f & MYSQLI_ENUM_FLAG
) {
711 // See http://dev.mysql.com/doc/refman/6.0/en/c-api-datatypes.html:
712 // to determine if a string is binary, we should not use MYSQLI_BINARY_FLAG
713 // but instead the charsetnr member of the MYSQL_FIELD
714 // structure. Watch out: some types like DATE returns 63 in charsetnr
715 // so we have to check also the type.
716 // Unfortunately there is no equivalent in the mysql extension.
717 if (($type == MYSQLI_TYPE_TINY_BLOB ||
$type == MYSQLI_TYPE_BLOB
718 ||
$type == MYSQLI_TYPE_MEDIUM_BLOB ||
$type == MYSQLI_TYPE_LONG_BLOB
719 ||
$type == MYSQLI_TYPE_VAR_STRING ||
$type == MYSQLI_TYPE_STRING
)
724 if ($f & MYSQLI_ZEROFILL_FLAG
) {
725 $flags .= 'zerofill ';
727 if ($f & MYSQLI_UNSIGNED_FLAG
) {
728 $flags .= 'unsigned ';
730 if ($f & MYSQLI_BLOB_FLAG
) {
733 if ($f & MYSQLI_MULTIPLE_KEY_FLAG
) {
734 $flags .= 'multiple_key ';
736 if ($f & MYSQLI_UNIQUE_KEY_FLAG
) {
737 $flags .= 'unique_key ';
739 if ($f & MYSQLI_PRI_KEY_FLAG
) {
740 $flags .= 'primary_key ';
742 if ($f & MYSQLI_NOT_NULL_FLAG
) {
743 $flags .= 'not_null ';