3.3.6-rc1
[phpmyadmin/dkf.git] / libraries / dbi / mysqli.dbi.lib.php
blob9672385df15e3f93dc06cf89defb154882d37821
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 require_once './libraries/logging.lib.php';
15 /**
16 * MySQL client API
18 if (!defined('PMA_MYSQL_CLIENT_API')) {
19 $client_api = explode('.', mysqli_get_client_info());
20 define('PMA_MYSQL_CLIENT_API', (int)sprintf('%d%02d%02d', $client_api[0], $client_api[1], intval($client_api[2])));
21 unset($client_api);
24 /**
25 * some PHP versions are reporting extra messages like "No index used in query"
28 mysqli_report(MYSQLI_REPORT_OFF);
30 /**
31 * some older mysql client libs are missing these constants ...
33 if (! defined('MYSQLI_BINARY_FLAG')) {
34 define('MYSQLI_BINARY_FLAG', 128);
37 /**
38 * @see http://bugs.php.net/36007
40 if (! defined('MYSQLI_TYPE_NEWDECIMAL')) {
41 define('MYSQLI_TYPE_NEWDECIMAL', 246);
43 if (! defined('MYSQLI_TYPE_BIT')) {
44 define('MYSQLI_TYPE_BIT', 16);
47 /**
48 * connects to the database server
50 * @uses $GLOBALS['cfg']['Server']
51 * @uses PMA_auth_fails()
52 * @uses PMA_DBI_postConnect()
53 * @uses MYSQLI_CLIENT_COMPRESS
54 * @uses MYSQLI_OPT_LOCAL_INFILE
55 * @uses strtolower()
56 * @uses mysqli_init()
57 * @uses mysqli_options()
58 * @uses mysqli_real_connect()
59 * @uses defined()
60 * @param string $user mysql user name
61 * @param string $password mysql user password
62 * @param boolean $is_controluser
63 * @param array $server host/port/socket
64 * @param boolean $auxiliary_connection (when true, don't go back to login if connection fails)
65 * @return mixed false on error or a mysqli object on success
67 function PMA_DBI_connect($user, $password, $is_controluser = false, $server = null, $auxiliary_connection = false)
69 if ($server) {
70 $server_port = (empty($server['port']))
71 ? ''
72 : (int)$server['port'];
73 $server_socket = (empty($server['socket']))
74 ? ''
75 : $server['socket'];
76 $server['host'] = (empty($server['host']))
77 ? 'localhost'
78 : $server['host'];
79 } else {
80 $server_port = (empty($GLOBALS['cfg']['Server']['port']))
81 ? false
82 : (int) $GLOBALS['cfg']['Server']['port'];
83 $server_socket = (empty($GLOBALS['cfg']['Server']['socket']))
84 ? null
85 : $GLOBALS['cfg']['Server']['socket'];
89 if (strtolower($GLOBALS['cfg']['Server']['connect_type']) == 'tcp') {
90 $GLOBALS['cfg']['Server']['socket'] = '';
93 // NULL enables connection to the default socket
95 $link = mysqli_init();
97 mysqli_options($link, MYSQLI_OPT_LOCAL_INFILE, true);
99 $client_flags = 0;
101 /* Optionally compress connection */
102 if ($GLOBALS['cfg']['Server']['compress'] && defined('MYSQLI_CLIENT_COMPRESS')) {
103 $client_flags |= MYSQLI_CLIENT_COMPRESS;
106 /* Optionally enable SSL */
107 if ($GLOBALS['cfg']['Server']['ssl'] && defined('MYSQLI_CLIENT_SSL')) {
108 $client_flags |= MYSQLI_CLIENT_SSL;
111 if (!$server) {
112 $return_value = @mysqli_real_connect($link, $GLOBALS['cfg']['Server']['host'], $user, $password, false, $server_port, $server_socket, $client_flags);
113 // Retry with empty password if we're allowed to
114 if ($return_value == false && isset($GLOBALS['cfg']['Server']['nopassword']) && $GLOBALS['cfg']['Server']['nopassword'] && !$is_controluser) {
115 $return_value = @mysqli_real_connect($link, $GLOBALS['cfg']['Server']['host'], $user, '', false, $server_port, $server_socket, $client_flags);
117 } else {
118 $return_value = @mysqli_real_connect($link, $server['host'], $user, $password, false, $server_port, $server_socket);
121 if ($return_value == false) {
122 if ($is_controluser) {
123 trigger_error($GLOBALS['strControluserFailed'], E_USER_WARNING);
124 return false;
126 // we could be calling PMA_DBI_connect() to connect to another
127 // server, for example in the Synchronize feature, so do not
128 // go back to main login if it fails
129 if (! $auxiliary_connection) {
130 PMA_log_user($user, 'mysql-denied');
131 PMA_auth_fails();
132 } else {
133 return false;
135 } else {
136 PMA_DBI_postConnect($link, $is_controluser);
139 return $link;
143 * selects given database
145 * @uses $GLOBALS['userlink']
146 * @uses PMA_convert_charset()
147 * @uses mysqli_select_db()
148 * @param string $dbname database name to select
149 * @param object mysqli $link the mysqli object
150 * @return boolean true or false
152 function PMA_DBI_select_db($dbname, $link = null)
154 if (empty($link)) {
155 if (isset($GLOBALS['userlink'])) {
156 $link = $GLOBALS['userlink'];
157 } else {
158 return false;
161 return mysqli_select_db($link, $dbname);
165 * runs a query and returns the result
167 * @uses PMA_DBI_QUERY_STORE
168 * @uses PMA_DBI_QUERY_UNBUFFERED
169 * @uses $GLOBALS['userlink']
170 * @uses PMA_convert_charset()
171 * @uses MYSQLI_STORE_RESULT
172 * @uses MYSQLI_USE_RESULT
173 * @uses mysqli_query()
174 * @uses defined()
175 * @param string $query query to execute
176 * @param object mysqli $link mysqli object
177 * @param integer $options
178 * @return mixed true, false or result object
180 function PMA_DBI_try_query($query, $link = null, $options = 0)
182 if ($options == ($options | PMA_DBI_QUERY_STORE)) {
183 $method = MYSQLI_STORE_RESULT;
184 } elseif ($options == ($options | PMA_DBI_QUERY_UNBUFFERED)) {
185 $method = MYSQLI_USE_RESULT;
186 } else {
187 $method = 0;
190 if (empty($link)) {
191 if (isset($GLOBALS['userlink'])) {
192 $link = $GLOBALS['userlink'];
193 } else {
194 return false;
198 if ($GLOBALS['cfg']['DBG']['sql']) {
199 $time = microtime(true);
201 $r = mysqli_query($link, $query, $method);
202 if ($GLOBALS['cfg']['DBG']['sql']) {
203 $time = microtime(true) - $time;
205 $hash = md5($query);
207 if (isset($_SESSION['debug']['queries'][$hash])) {
208 $_SESSION['debug']['queries'][$hash]['count']++;
209 } else {
210 $_SESSION['debug']['queries'][$hash] = array();
211 $_SESSION['debug']['queries'][$hash]['count'] = 1;
212 $_SESSION['debug']['queries'][$hash]['query'] = $query;
213 $_SESSION['debug']['queries'][$hash]['time'] = $time;
216 $trace = array();
217 foreach (debug_backtrace() as $trace_step) {
218 $trace[] = PMA_Error::relPath($trace_step['file']) . '#'
219 . $trace_step['line'] . ': '
220 . (isset($trace_step['class']) ? $trace_step['class'] : '')
221 //. (isset($trace_step['object']) ? get_class($trace_step['object']) : '')
222 . (isset($trace_step['type']) ? $trace_step['type'] : '')
223 . (isset($trace_step['function']) ? $trace_step['function'] : '')
224 . '('
225 . (isset($trace_step['params']) ? implode(', ', $trace_step['params']) : '')
226 . ')'
229 $_SESSION['debug']['queries'][$hash]['trace'][] = $trace;
232 if ($r != FALSE && PMA_Tracker::isActive() == TRUE ) {
233 PMA_Tracker::handleQuery($query);
236 return $r;
238 // From the PHP manual:
239 // "note: returns true on success or false on failure. For SELECT,
240 // SHOW, DESCRIBE or EXPLAIN, mysqli_query() will return a result object"
241 // so, do not use the return value to feed mysqli_num_rows() if it's
242 // a boolean
246 * returns array of rows with associative and numeric keys from $result
248 * @uses mysqli_fetch_array()
249 * @uses MYSQLI_BOTH
250 * @param object mysqli result $result
251 * @return array result rows
253 function PMA_DBI_fetch_array($result)
255 return mysqli_fetch_array($result, MYSQLI_BOTH);
259 * returns array of rows with associative keys from $result
261 * @uses mysqli_fetch_array()
262 * @uses MYSQLI_ASSOC
263 * @param object mysqli result $result
264 * @return array result rows
266 function PMA_DBI_fetch_assoc($result)
268 return mysqli_fetch_array($result, MYSQLI_ASSOC);
272 * returns array of rows with numeric keys from $result
274 * @uses mysqli_fetch_array()
275 * @uses MYSQLI_NUM
276 * @param object mysqli result $result
277 * @return array result rows
279 function PMA_DBI_fetch_row($result)
281 return mysqli_fetch_array($result, MYSQLI_NUM);
285 * Adjusts the result pointer to an arbitrary row in the result
287 * @uses mysqli_data_seek()
288 * @param $result
289 * @param $offset
290 * @return boolean true on success, false on failure
292 function PMA_DBI_data_seek($result, $offset)
294 return mysqli_data_seek($result, $offset);
298 * Frees the memory associated with the results
300 * @uses mysqli_result
301 * @uses func_get_args()
302 * @uses mysqli_free_result()
303 * @param result $result,... one or more mysql result resources
305 function PMA_DBI_free_result()
307 foreach (func_get_args() as $result) {
308 if ($result instanceof mysqli_result) {
309 mysqli_free_result($result);
315 * Returns a string representing the type of connection used
316 * @uses mysqli_get_host_info()
317 * @uses $GLOBALS['userlink'] as default for $link
318 * @param resource $link mysql link
319 * @return string type of connection used
321 function PMA_DBI_get_host_info($link = null)
323 if (null === $link) {
324 if (isset($GLOBALS['userlink'])) {
325 $link = $GLOBALS['userlink'];
326 } else {
327 return false;
330 return mysqli_get_host_info($link);
334 * Returns the version of the MySQL protocol used
335 * @uses mysqli_get_proto_info()
336 * @uses $GLOBALS['userlink'] as default for $link
337 * @param resource $link mysql link
338 * @return integer version of the MySQL protocol used
340 function PMA_DBI_get_proto_info($link = null)
342 if (null === $link) {
343 if (isset($GLOBALS['userlink'])) {
344 $link = $GLOBALS['userlink'];
345 } else {
346 return false;
349 return mysqli_get_proto_info($link);
353 * returns a string that represents the client library version
354 * @uses mysqli_get_client_info()
355 * @return string MySQL client library version
357 function PMA_DBI_get_client_info()
359 return mysqli_get_client_info();
363 * returns last error message or false if no errors occured
365 * @uses PMA_DBI_convert_message()
366 * @uses $GLOBALS['errno']
367 * @uses $GLOBALS['userlink']
368 * @uses $GLOBALS['strServerNotResponding']
369 * @uses $GLOBALS['strSocketProblem']
370 * @uses mysqli_errno()
371 * @uses mysqli_error()
372 * @uses mysqli_connect_errno()
373 * @uses mysqli_connect_error()
374 * @uses defined()
375 * @param resource $link mysql link
376 * @return string|boolean $error or false
378 function PMA_DBI_getError($link = null)
380 $GLOBALS['errno'] = 0;
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) . ' - ' . $GLOBALS['strServerNotResponding'] . ' ' . $GLOBALS['strSocketProblem'];
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);