Advisor: mark that 'Rate of reading fixed position' may be wrong, requires further...
[phpmyadmin/thilanka.git] / libraries / dbi / mysqli.dbi.lib.php
blobd3829588c7cb8f9f9117ec4ecc94abbef1dfe593
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 * Helper function for connecting to the database server
49 * @param mysqli $link
50 * @param string $host
51 * @param string $user
52 * @param string $password
53 * @param string $dbname
54 * @param int $server_port
55 * @param string $server_socket
56 * @param int $client_flags
57 * @param bool $persistent
58 * @return bool
60 function PMA_DBI_real_connect($link, $host, $user, $password, $dbname, $server_port, $server_socket, $client_flags = null, $persistent = false)
62 global $cfg;
64 if ($cfg['PersistentConnections'] || $persistent) {
65 $host = 'p:' . $host;
67 if ($client_flags === null) {
68 return @mysqli_real_connect($link, $host, $user, $password, $dbname, $server_port, $server_socket);
69 } else {
70 return @mysqli_real_connect($link, $host, $user, $password, $dbname, $server_port, $server_socket, $client_flags);
74 /**
75 * connects to the database server
77 * @param string $user mysql user name
78 * @param string $password mysql user password
79 * @param bool $is_controluser
80 * @param array $server host/port/socket
81 * @param bool $auxiliary_connection (when true, don't go back to login if connection fails)
82 * @return mixed false on error or a mysqli object on success
84 function PMA_DBI_connect($user, $password, $is_controluser = false, $server = null, $auxiliary_connection = false)
86 global $cfg;
88 if ($server) {
89 $server_port = (empty($server['port']))
90 ? false
91 : (int)$server['port'];
92 $server_socket = (empty($server['socket']))
93 ? ''
94 : $server['socket'];
95 $server['host'] = (empty($server['host']))
96 ? 'localhost'
97 : $server['host'];
98 } else {
99 $server_port = (empty($cfg['Server']['port']))
100 ? false
101 : (int) $cfg['Server']['port'];
102 $server_socket = (empty($cfg['Server']['socket']))
103 ? null
104 : $cfg['Server']['socket'];
107 // NULL enables connection to the default socket
109 $link = mysqli_init();
111 mysqli_options($link, MYSQLI_OPT_LOCAL_INFILE, true);
113 $client_flags = 0;
115 /* Optionally compress connection */
116 if ($cfg['Server']['compress'] && defined('MYSQLI_CLIENT_COMPRESS')) {
117 $client_flags |= MYSQLI_CLIENT_COMPRESS;
120 /* Optionally enable SSL */
121 if ($cfg['Server']['ssl'] && defined('MYSQLI_CLIENT_SSL')) {
122 $client_flags |= MYSQLI_CLIENT_SSL;
125 if (!$server) {
126 $return_value = @PMA_DBI_real_connect($link, $cfg['Server']['host'], $user, $password, false, $server_port, $server_socket, $client_flags);
127 // Retry with empty password if we're allowed to
128 if ($return_value == false && isset($cfg['Server']['nopassword']) && $cfg['Server']['nopassword'] && !$is_controluser) {
129 $return_value = @PMA_DBI_real_connect($link, $cfg['Server']['host'], $user, '', false, $server_port, $server_socket, $client_flags);
131 } else {
132 $return_value = @PMA_DBI_real_connect($link, $server['host'], $user, $password, false, $server_port, $server_socket);
135 if ($return_value == false) {
136 if ($is_controluser) {
137 trigger_error(__('Connection for controluser as defined in your configuration failed.'), E_USER_WARNING);
138 return false;
140 // we could be calling PMA_DBI_connect() to connect to another
141 // server, for example in the Synchronize feature, so do not
142 // go back to main login if it fails
143 if (! $auxiliary_connection) {
144 PMA_log_user($user, 'mysql-denied');
145 PMA_auth_fails();
146 } else {
147 return false;
149 } else {
150 PMA_DBI_postConnect($link, $is_controluser);
153 return $link;
157 * selects given database
159 * @param string $dbname database name to select
160 * @param mysqli $link the mysqli object
161 * @return boolean
163 function PMA_DBI_select_db($dbname, $link = null)
165 if (empty($link)) {
166 if (isset($GLOBALS['userlink'])) {
167 $link = $GLOBALS['userlink'];
168 } else {
169 return false;
172 return mysqli_select_db($link, $dbname);
176 * runs a query and returns the result
178 * @param string $query query to execute
179 * @param mysqli $link mysqli object
180 * @param int $options
181 * @return mysqli_result|bool
183 function PMA_DBI_real_query($query, $link, $options)
185 if ($options == ($options | PMA_DBI_QUERY_STORE)) {
186 $method = MYSQLI_STORE_RESULT;
187 } elseif ($options == ($options | PMA_DBI_QUERY_UNBUFFERED)) {
188 $method = MYSQLI_USE_RESULT;
189 } else {
190 $method = 0;
193 return mysqli_query($link, $query, $method);
197 * returns array of rows with associative and numeric keys from $result
199 * @param mysqli_result $result
200 * @return array
202 function PMA_DBI_fetch_array($result)
204 return mysqli_fetch_array($result, MYSQLI_BOTH);
208 * returns array of rows with associative keys from $result
210 * @param mysqli_result $result
211 * @return array
213 function PMA_DBI_fetch_assoc($result)
215 return mysqli_fetch_array($result, MYSQLI_ASSOC);
219 * returns array of rows with numeric keys from $result
221 * @param mysqli_result $result
222 * @return array
224 function PMA_DBI_fetch_row($result)
226 return mysqli_fetch_array($result, MYSQLI_NUM);
230 * Adjusts the result pointer to an arbitrary row in the result
232 * @param $result
233 * @param $offset
234 * @return bool true on success, false on failure
236 function PMA_DBI_data_seek($result, $offset)
238 return mysqli_data_seek($result, $offset);
242 * Frees memory associated with the result
244 * @param mysqli_result $result
246 function PMA_DBI_free_result($result)
248 if ($result instanceof mysqli_result) {
249 mysqli_free_result($result);
254 * Check if there are any more query results from a multi query
256 * @param mysqli $link the mysqli object
257 * @return bool true or false
259 function PMA_DBI_more_results($link = null)
261 if (empty($link)) {
262 if (isset($GLOBALS['userlink'])) {
263 $link = $GLOBALS['userlink'];
264 } else {
265 return false;
268 return mysqli_more_results($link);
272 * Prepare next result from multi_query
274 * @param mysqli $link the mysqli object
275 * @return bool true or false
277 function PMA_DBI_next_result($link = null)
279 if (empty($link)) {
280 if (isset($GLOBALS['userlink'])) {
281 $link = $GLOBALS['userlink'];
282 } else {
283 return false;
286 return mysqli_next_result($link);
290 * Returns a string representing the type of connection used
292 * @param resource $link mysql link
293 * @return string type of connection used
295 function PMA_DBI_get_host_info($link = null)
297 if (null === $link) {
298 if (isset($GLOBALS['userlink'])) {
299 $link = $GLOBALS['userlink'];
300 } else {
301 return false;
304 return mysqli_get_host_info($link);
308 * Returns the version of the MySQL protocol used
310 * @param resource $link mysql link
311 * @return integer version of the MySQL protocol used
313 function PMA_DBI_get_proto_info($link = null)
315 if (null === $link) {
316 if (isset($GLOBALS['userlink'])) {
317 $link = $GLOBALS['userlink'];
318 } else {
319 return false;
322 return mysqli_get_proto_info($link);
326 * returns a string that represents the client library version
328 * @return string MySQL client library version
330 function PMA_DBI_get_client_info()
332 return mysqli_get_client_info();
336 * returns last error message or false if no errors occured
338 * @param resource $link mysql link
339 * @return string|bool $error or false
341 function PMA_DBI_getError($link = null)
343 $GLOBALS['errno'] = 0;
345 /* Treat false same as null because of controllink */
346 if ($link === false) {
347 $link = null;
350 if (null === $link && isset($GLOBALS['userlink'])) {
351 $link =& $GLOBALS['userlink'];
352 // Do not stop now. We still can get the error code
353 // with mysqli_connect_errno()
354 // } else {
355 // return false;
358 if (null !== $link) {
359 $error_number = mysqli_errno($link);
360 $error_message = mysqli_error($link);
361 } else {
362 $error_number = mysqli_connect_errno();
363 $error_message = mysqli_connect_error();
365 if (0 == $error_number) {
366 return false;
369 // keep the error number for further check after the call to PMA_DBI_getError()
370 $GLOBALS['errno'] = $error_number;
372 if (! empty($error_message)) {
373 $error_message = PMA_DBI_convert_message($error_message);
376 $error_message = htmlspecialchars($error_message);
378 if ($error_number == 2002) {
379 $error = '#' . ((string) $error_number) . ' - ' . __('The server is not responding') . ' ' . __('(or the local MySQL server\'s socket is not correctly configured)');
380 } else {
381 $error = '#' . ((string) $error_number) . ' - ' . $error_message;
383 return $error;
387 * returns the number of rows returned by last query
389 * @param mysqli_result $result
390 * @return string|int
392 function PMA_DBI_num_rows($result)
394 // see the note for PMA_DBI_try_query();
395 if (!is_bool($result)) {
396 return @mysqli_num_rows($result);
397 } else {
398 return 0;
403 * returns last inserted auto_increment id for given $link or $GLOBALS['userlink']
405 * @param mysqli $link the mysqli object
406 * @return string|int
408 function PMA_DBI_insert_id($link = null)
410 if (empty($link)) {
411 if (isset($GLOBALS['userlink'])) {
412 $link = $GLOBALS['userlink'];
413 } else {
414 return false;
417 // When no controluser is defined, using mysqli_insert_id($link)
418 // does not always return the last insert id due to a mixup with
419 // the tracking mechanism, but this works:
420 return PMA_DBI_fetch_value('SELECT LAST_INSERT_ID();', 0, 0, $link);
421 // Curiously, this problem does not happen with the mysql extension but
422 // there is another problem with BIGINT primary keys so PMA_DBI_insert_id()
423 // in the mysql extension also uses this logic.
427 * returns the number of rows affected by last query
429 * @param mysqli $link the mysqli object
430 * @param boolean $get_from_cache
431 * @return string|int
433 function PMA_DBI_affected_rows($link = null, $get_from_cache = true)
435 if (empty($link)) {
436 if (isset($GLOBALS['userlink'])) {
437 $link = $GLOBALS['userlink'];
438 } else {
439 return false;
442 if ($get_from_cache) {
443 return $GLOBALS['cached_affected_rows'];
444 } else {
445 return mysqli_affected_rows($link);
450 * returns metainfo for fields in $result
452 * @param mysqli_result $result
453 * @return array meta info for fields in $result
455 function PMA_DBI_get_fields_meta($result)
457 // Build an associative array for a type look up
458 $typeAr = array();
459 $typeAr[MYSQLI_TYPE_DECIMAL] = 'real';
460 $typeAr[MYSQLI_TYPE_NEWDECIMAL] = 'real';
461 $typeAr[MYSQLI_TYPE_BIT] = 'int';
462 $typeAr[MYSQLI_TYPE_TINY] = 'int';
463 $typeAr[MYSQLI_TYPE_SHORT] = 'int';
464 $typeAr[MYSQLI_TYPE_LONG] = 'int';
465 $typeAr[MYSQLI_TYPE_FLOAT] = 'real';
466 $typeAr[MYSQLI_TYPE_DOUBLE] = 'real';
467 $typeAr[MYSQLI_TYPE_NULL] = 'null';
468 $typeAr[MYSQLI_TYPE_TIMESTAMP] = 'timestamp';
469 $typeAr[MYSQLI_TYPE_LONGLONG] = 'int';
470 $typeAr[MYSQLI_TYPE_INT24] = 'int';
471 $typeAr[MYSQLI_TYPE_DATE] = 'date';
472 $typeAr[MYSQLI_TYPE_TIME] = 'time';
473 $typeAr[MYSQLI_TYPE_DATETIME] = 'datetime';
474 $typeAr[MYSQLI_TYPE_YEAR] = 'year';
475 $typeAr[MYSQLI_TYPE_NEWDATE] = 'date';
476 $typeAr[MYSQLI_TYPE_ENUM] = 'unknown';
477 $typeAr[MYSQLI_TYPE_SET] = 'unknown';
478 $typeAr[MYSQLI_TYPE_TINY_BLOB] = 'blob';
479 $typeAr[MYSQLI_TYPE_MEDIUM_BLOB] = 'blob';
480 $typeAr[MYSQLI_TYPE_LONG_BLOB] = 'blob';
481 $typeAr[MYSQLI_TYPE_BLOB] = 'blob';
482 $typeAr[MYSQLI_TYPE_VAR_STRING] = 'string';
483 $typeAr[MYSQLI_TYPE_STRING] = 'string';
484 // MySQL returns MYSQLI_TYPE_STRING for CHAR
485 // and MYSQLI_TYPE_CHAR === MYSQLI_TYPE_TINY
486 // so this would override TINYINT and mark all TINYINT as string
487 // https://sf.net/tracker/?func=detail&aid=1532111&group_id=23067&atid=377408
488 //$typeAr[MYSQLI_TYPE_CHAR] = 'string';
489 $typeAr[MYSQLI_TYPE_GEOMETRY] = 'geometry';
490 $typeAr[MYSQLI_TYPE_BIT] = 'bit';
492 $fields = mysqli_fetch_fields($result);
494 // this happens sometimes (seen under MySQL 4.0.25)
495 if (!is_array($fields)) {
496 return false;
499 foreach ($fields as $k => $field) {
500 $fields[$k]->_type = $field->type;
501 $fields[$k]->type = $typeAr[$field->type];
502 $fields[$k]->_flags = $field->flags;
503 $fields[$k]->flags = PMA_DBI_field_flags($result, $k);
505 // Enhance the field objects for mysql-extension compatibilty
506 //$flags = explode(' ', $fields[$k]->flags);
507 //array_unshift($flags, 'dummy');
508 $fields[$k]->multiple_key
509 = (int) (bool) ($fields[$k]->_flags & MYSQLI_MULTIPLE_KEY_FLAG);
510 $fields[$k]->primary_key
511 = (int) (bool) ($fields[$k]->_flags & MYSQLI_PRI_KEY_FLAG);
512 $fields[$k]->unique_key
513 = (int) (bool) ($fields[$k]->_flags & MYSQLI_UNIQUE_KEY_FLAG);
514 $fields[$k]->not_null
515 = (int) (bool) ($fields[$k]->_flags & MYSQLI_NOT_NULL_FLAG);
516 $fields[$k]->unsigned
517 = (int) (bool) ($fields[$k]->_flags & MYSQLI_UNSIGNED_FLAG);
518 $fields[$k]->zerofill
519 = (int) (bool) ($fields[$k]->_flags & MYSQLI_ZEROFILL_FLAG);
520 $fields[$k]->numeric
521 = (int) (bool) ($fields[$k]->_flags & MYSQLI_NUM_FLAG);
522 $fields[$k]->blob
523 = (int) (bool) ($fields[$k]->_flags & MYSQLI_BLOB_FLAG);
525 return $fields;
529 * return number of fields in given $result
531 * @param mysqli_result $result
532 * @return int field count
534 function PMA_DBI_num_fields($result)
536 return mysqli_num_fields($result);
540 * returns the length of the given field $i in $result
542 * @param mysqli_result $result
543 * @param int $i field
544 * @return int length of field
546 function PMA_DBI_field_len($result, $i)
548 return mysqli_fetch_field_direct($result, $i)->length;
552 * returns name of $i. field in $result
554 * @param mysqli_result $result
555 * @param int $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 * @param mysqli_result $result
567 * @param int $i field
568 * @return string field flags
570 function PMA_DBI_field_flags($result, $i)
572 // This is missing from PHP 5.2.5, see http://bugs.php.net/bug.php?id=44846
573 if (! defined('MYSQLI_ENUM_FLAG')) {
574 define('MYSQLI_ENUM_FLAG', 256); // see MySQL source include/mysql_com.h
576 $f = mysqli_fetch_field_direct($result, $i);
577 $type = $f->type;
578 $charsetnr = $f->charsetnr;
579 $f = $f->flags;
580 $flags = '';
581 if ($f & MYSQLI_UNIQUE_KEY_FLAG) { $flags .= 'unique ';}
582 if ($f & MYSQLI_NUM_FLAG) { $flags .= 'num ';}
583 if ($f & MYSQLI_PART_KEY_FLAG) { $flags .= 'part_key ';}
584 if ($f & MYSQLI_SET_FLAG) { $flags .= 'set ';}
585 if ($f & MYSQLI_TIMESTAMP_FLAG) { $flags .= 'timestamp ';}
586 if ($f & MYSQLI_AUTO_INCREMENT_FLAG) { $flags .= 'auto_increment ';}
587 if ($f & MYSQLI_ENUM_FLAG) { $flags .= 'enum ';}
588 // See http://dev.mysql.com/doc/refman/6.0/en/c-api-datatypes.html:
589 // to determine if a string is binary, we should not use MYSQLI_BINARY_FLAG
590 // but instead the charsetnr member of the MYSQL_FIELD
591 // structure. Watch out: some types like DATE returns 63 in charsetnr
592 // so we have to check also the type.
593 // Unfortunately there is no equivalent in the mysql extension.
594 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 ';}
595 if ($f & MYSQLI_ZEROFILL_FLAG) { $flags .= 'zerofill ';}
596 if ($f & MYSQLI_UNSIGNED_FLAG) { $flags .= 'unsigned ';}
597 if ($f & MYSQLI_BLOB_FLAG) { $flags .= 'blob ';}
598 if ($f & MYSQLI_MULTIPLE_KEY_FLAG) { $flags .= 'multiple_key ';}
599 if ($f & MYSQLI_UNIQUE_KEY_FLAG) { $flags .= 'unique_key ';}
600 if ($f & MYSQLI_PRI_KEY_FLAG) { $flags .= 'primary_key ';}
601 if ($f & MYSQLI_NOT_NULL_FLAG) { $flags .= 'not_null ';}
602 return trim($flags);