2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Common Option Constants For DBI Functions
12 // PMA_DBI_try_query()
13 define('PMA_DBI_QUERY_STORE', 1); // Force STORE_RESULT method, ignored by classic MySQL.
14 define('PMA_DBI_QUERY_UNBUFFERED', 2); // Do not read whole query
15 // PMA_DBI_get_variable()
16 define('PMA_DBI_GETVAR_SESSION', 1);
17 define('PMA_DBI_GETVAR_GLOBAL', 2);
20 * Loads the mysql extensions if it is not loaded yet
22 * @param string $extension mysql extension to load
24 function PMA_DBI_checkAndLoadMysqlExtension($extension = 'mysql') {
25 if (! function_exists($extension . '_connect')) {
27 // check whether mysql is available
28 if (! function_exists($extension . '_connect')) {
38 * check for requested extension
40 if (! PMA_DBI_checkAndLoadMysqlExtension($GLOBALS['cfg']['Server']['extension'])) {
42 // if it fails try alternative extension ...
43 // and display an error ...
46 * @todo 2.7.1: add different messages for alternativ extension
47 * and complete fail (no alternativ extension too)
49 $GLOBALS['PMA_errors'][] =
50 sprintf(PMA_sanitize($GLOBALS['strCantLoad']),
51 $GLOBALS['cfg']['Server']['extension'])
52 .' - <a href="./Documentation.html#faqmysql" target="documentation">'
53 .$GLOBALS['strDocu'] . '</a>';
55 if ($GLOBALS['cfg']['Server']['extension'] === 'mysql') {
56 $alternativ_extension = 'mysqli';
58 $alternativ_extension = 'mysql';
61 if (! PMA_DBI_checkAndLoadMysqlExtension($alternativ_extension)) {
62 // if alternativ fails too ...
64 sprintf($GLOBALS['strCantLoad'],
65 $GLOBALS['cfg']['Server']['extension'])
66 . ' - [a@./Documentation.html#faqmysql@documentation]'
67 . $GLOBALS['strDocu'] . '[/a]');
70 $GLOBALS['cfg']['Server']['extension'] = $alternativ_extension;
71 unset($alternativ_extension);
75 * Including The DBI Plugin
77 require_once './libraries/dbi/' . $GLOBALS['cfg']['Server']['extension'] . '.dbi.lib.php';
82 function PMA_DBI_query($query, $link = null, $options = 0) {
83 $res = PMA_DBI_try_query($query, $link, $options)
84 or PMA_mysqlDie(PMA_DBI_getError($link), $query);
89 * converts charset of a mysql message, usally coming from mysql_error(),
90 * into PMA charset, usally UTF-8
91 * uses language to charset mapping from mysql/share/errmsg.txt
92 * and charset names to ISO charset from information_schema.CHARACTER_SETS
94 * @uses $GLOBALS['cfg']['IconvExtraParams']
95 * @uses $GLOBALS['charset'] as target charset
96 * @uses PMA_DBI_fetch_value() to get server_language
97 * @uses preg_match() to filter server_language
99 * @uses function_exists() to check for a convert function
100 * @uses iconv() to convert message
101 * @uses libiconv() to convert message
102 * @uses recode_string() to convert message
103 * @uses mb_convert_encoding() to convert message
104 * @param string $message
105 * @return string $message
107 function PMA_DBI_convert_message($message) {
108 // latin always last!
110 'japanese' => 'EUC-JP', //'ujis',
111 'japanese-sjis' => 'Shift-JIS', //'sjis',
112 'korean' => 'EUC-KR', //'euckr',
113 'russian' => 'KOI8-R', //'koi8r',
114 'ukrainian' => 'KOI8-U', //'koi8u',
115 'greek' => 'ISO-8859-7', //'greek',
116 'serbian' => 'CP1250', //'cp1250',
117 'estonian' => 'ISO-8859-13', //'latin7',
118 'slovak' => 'ISO-8859-2', //'latin2',
119 'czech' => 'ISO-8859-2', //'latin2',
120 'hungarian' => 'ISO-8859-2', //'latin2',
121 'polish' => 'ISO-8859-2', //'latin2',
122 'romanian' => 'ISO-8859-2', //'latin2',
123 'spanish' => 'CP1252', //'latin1',
124 'swedish' => 'CP1252', //'latin1',
125 'italian' => 'CP1252', //'latin1',
126 'norwegian-ny' => 'CP1252', //'latin1',
127 'norwegian' => 'CP1252', //'latin1',
128 'portuguese' => 'CP1252', //'latin1',
129 'danish' => 'CP1252', //'latin1',
130 'dutch' => 'CP1252', //'latin1',
131 'english' => 'CP1252', //'latin1',
132 'french' => 'CP1252', //'latin1',
133 'german' => 'CP1252', //'latin1',
136 if ($server_language = PMA_DBI_fetch_value('SHOW VARIABLES LIKE \'language\';', 0, 1)) {
138 if (preg_match('&(?:\\\|\\/)([^\\\\\/]*)(?:\\\|\\/)$&i', $server_language, $found)) {
139 $server_language = $found[1];
143 if (! empty($server_language) && isset($encodings[$server_language])) {
144 if (function_exists('iconv')) {
145 if ((@stristr
(PHP_OS
, 'AIX')) && (@strcasecmp
(ICONV_IMPL
, 'unknown') == 0) && (@strcasecmp
(ICONV_VERSION
, 'unknown') == 0)) {
146 require_once './libraries/iconv_wrapper.lib.php';
147 $message = PMA_aix_iconv_wrapper($encodings[$server_language],
148 $GLOBALS['charset'] . $GLOBALS['cfg']['IconvExtraParams'], $message);
150 $message = iconv($encodings[$server_language],
151 $GLOBALS['charset'] . $GLOBALS['cfg']['IconvExtraParams'], $message);
153 } elseif (function_exists('recode_string')) {
154 $message = recode_string($encodings[$server_language] . '..' . $GLOBALS['charset'],
156 } elseif (function_exists('libiconv')) {
157 $message = libiconv($encodings[$server_language], $GLOBALS['charset'], $message);
158 } elseif (function_exists('mb_convert_encoding')) {
159 // do not try unsupported charsets
160 if (! in_array($server_language, array('ukrainian', 'greek', 'serbian'))) {
161 $message = mb_convert_encoding($message, $GLOBALS['charset'],
162 $encodings[$server_language]);
167 * @todo lang not found, try all, what TODO ?
175 * returns array with table names for given db
177 * @param string $database name of database
178 * @param mixed $link mysql link resource|object
179 * @return array tables names
181 function PMA_DBI_get_tables($database, $link = null)
183 return PMA_DBI_fetch_result('SHOW TABLES FROM ' . PMA_backquote($database) . ';',
184 null, 0, $link, PMA_DBI_QUERY_STORE
);
188 * returns array of all tables in given db or dbs
189 * this function expects unquoted names:
191 * WRONG: `my_database`
192 * WRONG: my\_database
193 * if $tbl_is_group is true, $table is used as filter for table names
194 * if $tbl_is_group is 'comment, $table is used as filter for table comments
197 * PMA_DBI_get_tables_full('my_database');
198 * PMA_DBI_get_tables_full('my_database', 'my_table'));
199 * PMA_DBI_get_tables_full('my_database', 'my_tables_', true));
200 * PMA_DBI_get_tables_full('my_database', 'my_tables_', 'comment'));
203 * @uses PMA_MYSQL_INT_VERSION
204 * @uses PMA_DBI_fetch_result()
205 * @uses PMA_escape_mysql_wildcards()
206 * @uses PMA_backquote()
211 * @param string $databases database
212 * @param string $table table
213 * @param boolean|string $tbl_is_group $table is a table group
214 * @param resource $link mysql link
215 * @param integer $limit_offset zero-based offset for the count
216 * @param boolean|integer $limit_count number of tables to return
217 * @return array list of tables in given db(s)
219 function PMA_DBI_get_tables_full($database, $table = false,
220 $tbl_is_group = false, $link = null, $limit_offset = 0, $limit_count = false)
222 if (true === $limit_count) {
223 $limit_count = $GLOBALS['cfg']['MaxTableList'];
225 // prepare and check parameters
226 if (! is_array($database)) {
227 $databases = array($database);
229 $databases = $database;
234 if (PMA_MYSQL_INT_VERSION
>= 50002) {
235 // get table information from information_schema
237 if (true === $tbl_is_group) {
238 $sql_where_table = 'AND `TABLE_NAME` LIKE \''
239 . PMA_escape_mysql_wildcards(addslashes($table)) . '%\'';
240 } elseif ('comment' === $tbl_is_group) {
241 $sql_where_table = 'AND `TABLE_COMMENT` LIKE \''
242 . PMA_escape_mysql_wildcards(addslashes($table)) . '%\'';
244 $sql_where_table = 'AND `TABLE_NAME` = \'' . addslashes($table) . '\'';
247 $sql_where_table = '';
251 // `SCHEMA_FIELD_NAME` AS `SHOW_TABLE_STATUS_FIELD_NAME`
253 // on non-Windows servers,
254 // added BINARY in the WHERE clause to force a case sensitive
255 // comparison (if we are looking for the db Aa we don't want
256 // to find the db aa)
257 $this_databases = array_map('PMA_sqlAddslashes', $databases);
261 `TABLE_SCHEMA` AS `Db`,
262 `TABLE_NAME` AS `Name`,
263 `ENGINE` AS `Engine`,
265 `VERSION` AS `Version`,
266 `ROW_FORMAT` AS `Row_format`,
267 `TABLE_ROWS` AS `Rows`,
268 `AVG_ROW_LENGTH` AS `Avg_row_length`,
269 `DATA_LENGTH` AS `Data_length`,
270 `MAX_DATA_LENGTH` AS `Max_data_length`,
271 `INDEX_LENGTH` AS `Index_length`,
272 `DATA_FREE` AS `Data_free`,
273 `AUTO_INCREMENT` AS `Auto_increment`,
274 `CREATE_TIME` AS `Create_time`,
275 `UPDATE_TIME` AS `Update_time`,
276 `CHECK_TIME` AS `Check_time`,
277 `TABLE_COLLATION` AS `Collation`,
278 `CHECKSUM` AS `Checksum`,
279 `CREATE_OPTIONS` AS `Create_options`,
280 `TABLE_COMMENT` AS `Comment`
281 FROM `information_schema`.`TABLES`
282 WHERE ' . (PMA_IS_WINDOWS ?
'' : 'BINARY') . ' `TABLE_SCHEMA` IN (\'' . implode("', '", $this_databases) . '\')
283 ' . $sql_where_table;
286 $sql .= ' LIMIT ' . $limit_count . ' OFFSET ' . $limit_offset;
288 $tables = PMA_DBI_fetch_result($sql, array('TABLE_SCHEMA', 'TABLE_NAME'),
290 unset($sql_where_table, $sql);
292 // If permissions are wrong on even one database directory,
293 // information_schema does not return any table info for any database
294 // this is why we fall back to SHOW TABLE STATUS even for MySQL >= 50002
295 if (PMA_MYSQL_INT_VERSION
< 50002 ||
empty($tables)) {
296 foreach ($databases as $each_database) {
297 if (true === $tbl_is_group) {
298 $sql = 'SHOW TABLE STATUS FROM '
299 . PMA_backquote($each_database)
300 .' LIKE \'' . PMA_escape_mysql_wildcards(addslashes($table)) . '%\'';
302 $sql = 'SHOW TABLE STATUS FROM '
303 . PMA_backquote($each_database) . ';';
305 $each_tables = PMA_DBI_fetch_result($sql, 'Name', null, $link);
307 $each_tables = array_slice($each_tables, $limit_offset, $limit_count);
310 foreach ($each_tables as $table_name => $each_table) {
311 if ('comment' === $tbl_is_group
312 && 0 === strpos($each_table['Comment'], $table))
314 // remove table from list
315 unset($each_tables[$table_name]);
319 if (! isset($each_tables[$table_name]['Type'])
320 && isset($each_tables[$table_name]['Engine'])) {
321 // pma BC, same parts of PMA still uses 'Type'
322 $each_tables[$table_name]['Type']
323 =& $each_tables[$table_name]['Engine'];
324 } elseif (! isset($each_tables[$table_name]['Engine'])
325 && isset($each_tables[$table_name]['Type'])) {
326 // old MySQL reports Type, newer MySQL reports Engine
327 $each_tables[$table_name]['Engine']
328 =& $each_tables[$table_name]['Type'];
331 // MySQL forward compatibility
332 // so pma could use this array as if every server is of version >5.0
333 $each_tables[$table_name]['TABLE_SCHEMA'] = $each_database;
334 $each_tables[$table_name]['TABLE_NAME'] =& $each_tables[$table_name]['Name'];
335 $each_tables[$table_name]['ENGINE'] =& $each_tables[$table_name]['Engine'];
336 $each_tables[$table_name]['VERSION'] =& $each_tables[$table_name]['Version'];
337 $each_tables[$table_name]['ROW_FORMAT'] =& $each_tables[$table_name]['Row_format'];
338 $each_tables[$table_name]['TABLE_ROWS'] =& $each_tables[$table_name]['Rows'];
339 $each_tables[$table_name]['AVG_ROW_LENGTH'] =& $each_tables[$table_name]['Avg_row_length'];
340 $each_tables[$table_name]['DATA_LENGTH'] =& $each_tables[$table_name]['Data_length'];
341 $each_tables[$table_name]['MAX_DATA_LENGTH'] =& $each_tables[$table_name]['Max_data_length'];
342 $each_tables[$table_name]['INDEX_LENGTH'] =& $each_tables[$table_name]['Index_length'];
343 $each_tables[$table_name]['DATA_FREE'] =& $each_tables[$table_name]['Data_free'];
344 $each_tables[$table_name]['AUTO_INCREMENT'] =& $each_tables[$table_name]['Auto_increment'];
345 $each_tables[$table_name]['CREATE_TIME'] =& $each_tables[$table_name]['Create_time'];
346 $each_tables[$table_name]['UPDATE_TIME'] =& $each_tables[$table_name]['Update_time'];
347 $each_tables[$table_name]['CHECK_TIME'] =& $each_tables[$table_name]['Check_time'];
348 $each_tables[$table_name]['TABLE_COLLATION'] =& $each_tables[$table_name]['Collation'];
349 $each_tables[$table_name]['CHECKSUM'] =& $each_tables[$table_name]['Checksum'];
350 $each_tables[$table_name]['CREATE_OPTIONS'] =& $each_tables[$table_name]['Create_options'];
351 $each_tables[$table_name]['TABLE_COMMENT'] =& $each_tables[$table_name]['Comment'];
353 if (strtoupper($each_tables[$table_name]['Comment']) === 'VIEW') {
354 $each_tables[$table_name]['TABLE_TYPE'] = 'VIEW';
357 * @todo difference between 'TEMPORARY' and 'BASE TABLE' but how to detect?
359 $each_tables[$table_name]['TABLE_TYPE'] = 'BASE TABLE';
363 $tables[$each_database] = $each_tables;
367 if ($GLOBALS['cfg']['NaturalOrder']) {
368 foreach ($tables as $key => $val) {
369 uksort($tables[$key], 'strnatcasecmp');
373 if (! is_array($database)) {
374 if (isset($tables[$database])) {
375 return $tables[$database];
376 } elseif (isset($tables[strtolower($database)])) {
377 // on windows with lower_case_table_names = 1
379 // with SHOW DATABASES or information_schema.SCHEMATA: `Test`
380 // but information_schema.TABLES gives `test`
382 // http://sf.net/support/tracker.php?aid=1436171
383 return $tables[strtolower($database)];
393 * returns array with databases containing extended infos about them
395 * @todo move into PMA_List_Database?
396 * @param string $databases database
397 * @param boolean $force_stats retrieve stats also for MySQL < 5
398 * @param resource $link mysql link
399 * @param string $sort_by collumn to order by
400 * @param string $sort_order ASC or DESC
401 * @param integer $limit_offset starting offset for LIMIT
402 * @param bool|int $limit_count row count for LIMIT or true for $GLOBALS['cfg']['MaxDbList']
403 * @return array $databases
405 function PMA_DBI_get_databases_full($database = null, $force_stats = false,
406 $link = null, $sort_by = 'SCHEMA_NAME', $sort_order = 'ASC',
407 $limit_offset = 0, $limit_count = false)
409 $sort_order = strtoupper($sort_order);
411 if (true === $limit_count) {
412 $limit_count = $GLOBALS['cfg']['MaxDbList'];
415 // initialize to avoid errors when there are no databases
416 $databases = array();
418 $apply_limit_and_order_manual = true;
420 if (PMA_MYSQL_INT_VERSION
>= 50002) {
422 * if $GLOBALS['cfg']['NaturalOrder'] is enabled, we cannot use LIMIT
423 * cause MySQL does not support natural ordering, we have to do it afterward
425 if ($GLOBALS['cfg']['NaturalOrder']) {
429 $limit = ' LIMIT ' . $limit_count . ' OFFSET ' . $limit_offset;
432 $apply_limit_and_order_manual = false;
435 // get table information from information_schema
437 $sql_where_schema = 'WHERE `SCHEMA_NAME` LIKE \''
438 . addslashes($database) . '\'';
440 $sql_where_schema = '';
444 // `SCHEMA_FIELD_NAME` AS `SHOW_TABLE_STATUS_FIELD_NAME`
446 SELECT `information_schema`.`SCHEMATA`.*';
449 COUNT(`information_schema`.`TABLES`.`TABLE_SCHEMA`)
451 SUM(`information_schema`.`TABLES`.`TABLE_ROWS`)
452 AS `SCHEMA_TABLE_ROWS`,
453 SUM(`information_schema`.`TABLES`.`DATA_LENGTH`)
454 AS `SCHEMA_DATA_LENGTH`,
455 SUM(`information_schema`.`TABLES`.`MAX_DATA_LENGTH`)
456 AS `SCHEMA_MAX_DATA_LENGTH`,
457 SUM(`information_schema`.`TABLES`.`INDEX_LENGTH`)
458 AS `SCHEMA_INDEX_LENGTH`,
459 SUM(`information_schema`.`TABLES`.`DATA_LENGTH`
460 + `information_schema`.`TABLES`.`INDEX_LENGTH`)
462 SUM(`information_schema`.`TABLES`.`DATA_FREE`)
463 AS `SCHEMA_DATA_FREE`';
466 FROM `information_schema`.`SCHEMATA`';
469 LEFT JOIN `information_schema`.`TABLES`
470 ON BINARY `information_schema`.`TABLES`.`TABLE_SCHEMA`
471 = BINARY `information_schema`.`SCHEMATA`.`SCHEMA_NAME`';
474 ' . $sql_where_schema . '
475 GROUP BY BINARY `information_schema`.`SCHEMATA`.`SCHEMA_NAME`
476 ORDER BY BINARY ' . PMA_backquote($sort_by) . ' ' . $sort_order
478 $databases = PMA_DBI_fetch_result($sql, 'SCHEMA_NAME', null, $link);
480 $mysql_error = PMA_DBI_getError($link);
481 if (! count($databases) && $GLOBALS['errno']) {
482 PMA_mysqlDie($mysql_error, $sql);
485 // display only databases also in official database list
486 // f.e. to apply hide_db and only_db
487 $drops = array_diff(array_keys($databases), $GLOBALS['PMA_List_Database']->items
);
489 foreach ($drops as $drop) {
490 unset($databases[$drop]);
494 unset($sql_where_schema, $sql, $drops);
496 foreach ($GLOBALS['PMA_List_Database']->items
as $database_name) {
497 // MySQL forward compatibility
498 // so pma could use this array as if every server is of version >5.0
499 $databases[$database_name]['SCHEMA_NAME'] = $database_name;
502 require_once 'mysql_charsets.lib.php';
504 $databases[$database_name]['DEFAULT_COLLATION_NAME']
505 = PMA_getDbCollation($database_name);
507 // get additonal info about tables
508 $databases[$database_name]['SCHEMA_TABLES'] = 0;
509 $databases[$database_name]['SCHEMA_TABLE_ROWS'] = 0;
510 $databases[$database_name]['SCHEMA_DATA_LENGTH'] = 0;
511 $databases[$database_name]['SCHEMA_MAX_DATA_LENGTH'] = 0;
512 $databases[$database_name]['SCHEMA_INDEX_LENGTH'] = 0;
513 $databases[$database_name]['SCHEMA_LENGTH'] = 0;
514 $databases[$database_name]['SCHEMA_DATA_FREE'] = 0;
516 $res = PMA_DBI_query('SHOW TABLE STATUS FROM ' . PMA_backquote($database_name) . ';');
517 while ($row = PMA_DBI_fetch_assoc($res)) {
518 $databases[$database_name]['SCHEMA_TABLES']++
;
519 $databases[$database_name]['SCHEMA_TABLE_ROWS']
521 $databases[$database_name]['SCHEMA_DATA_LENGTH']
522 +
= $row['Data_length'];
523 $databases[$database_name]['SCHEMA_MAX_DATA_LENGTH']
524 +
= $row['Max_data_length'];
525 $databases[$database_name]['SCHEMA_INDEX_LENGTH']
526 +
= $row['Index_length'];
527 $databases[$database_name]['SCHEMA_DATA_FREE']
528 +
= $row['Data_free'];
529 $databases[$database_name]['SCHEMA_LENGTH']
530 +
= $row['Data_length'] +
$row['Index_length'];
532 PMA_DBI_free_result($res);
539 * apply limit and order manually now
540 * (caused by older MySQL < 5 or $GLOBALS['cfg']['NaturalOrder'])
542 if ($apply_limit_and_order_manual) {
545 * first apply ordering
547 if ($GLOBALS['cfg']['NaturalOrder']) {
548 $sorter = 'strnatcasecmp';
550 $sorter = 'strcasecmp';
554 // return -1 * strnatcasecmp($a["SCHEMA_TABLES"], $b["SCHEMA_TABLES"])
556 return ' . ($sort_order == 'ASC' ?
1 : -1) . ' * ' . $sorter . '($a["' . $sort_by . '"], $b["' . $sort_by . '"]);
559 usort($databases, create_function('$a, $b', $sort_function));
565 $databases = array_slice($databases, $limit_offset, $limit_count);
573 * returns detailed array with all columns for given table in database,
574 * or all tables/databases
576 * @param string $database name of database
577 * @param string $table name of table to retrieve columns from
578 * @param string $column name of specific column
579 * @param mixed $link mysql link resource
581 function PMA_DBI_get_columns_full($database = null, $table = null,
582 $column = null, $link = null)
586 if (PMA_MYSQL_INT_VERSION
>= 50002) {
587 $sql_wheres = array();
588 $array_keys = array();
590 // get columns information from information_schema
591 if (null !== $database) {
592 $sql_wheres[] = '`TABLE_SCHEMA` = \'' . addslashes($database) . '\' ';
594 $array_keys[] = 'TABLE_SCHEMA';
596 if (null !== $table) {
597 $sql_wheres[] = '`TABLE_NAME` = \'' . addslashes($table) . '\' ';
599 $array_keys[] = 'TABLE_NAME';
601 if (null !== $column) {
602 $sql_wheres[] = '`COLUMN_NAME` = \'' . addslashes($column) . '\' ';
604 $array_keys[] = 'COLUMN_NAME';
608 // `[SCHEMA_FIELD_NAME]` AS `[SHOW_FULL_COLUMNS_FIELD_NAME]`
611 `COLUMN_NAME` AS `Field`,
612 `COLUMN_TYPE` AS `Type`,
613 `COLLATION_NAME` AS `Collation`,
614 `IS_NULLABLE` AS `Null`,
615 `COLUMN_KEY` AS `Key`,
616 `COLUMN_DEFAULT` AS `Default`,
618 `PRIVILEGES` AS `Privileges`,
619 `COLUMN_COMMENT` AS `Comment`
620 FROM `information_schema`.`COLUMNS`';
621 if (count($sql_wheres)) {
622 $sql .= "\n" . ' WHERE ' . implode(' AND ', $sql_wheres);
625 $columns = PMA_DBI_fetch_result($sql, $array_keys, null, $link);
626 unset($sql_wheres, $sql);
628 if (null === $database) {
629 foreach ($GLOBALS['PMA_List_Database']->items
as $database) {
630 $columns[$database] = PMA_DBI_get_columns_full($database, null,
634 } elseif (null === $table) {
635 $tables = PMA_DBI_get_tables($database);
636 foreach ($tables as $table) {
637 $columns[$table] = PMA_DBI_get_columns_full(
638 $database, $table, null, $link);
643 $sql = 'SHOW FULL COLUMNS FROM '
644 . PMA_backquote($database) . '.' . PMA_backquote($table);
645 if (null !== $column) {
646 $sql .= " LIKE '" . $column . "'";
649 $columns = PMA_DBI_fetch_result($sql, 'Field', null, $link);
651 $ordinal_position = 1;
652 foreach ($columns as $column_name => $each_column) {
654 // MySQL forward compatibility
655 // so pma could use this array as if every server is of version >5.0
656 $columns[$column_name]['COLUMN_NAME'] =& $columns[$column_name]['Field'];
657 $columns[$column_name]['COLUMN_TYPE'] =& $columns[$column_name]['Type'];
658 $columns[$column_name]['COLLATION_NAME'] =& $columns[$column_name]['Collation'];
659 $columns[$column_name]['IS_NULLABLE'] =& $columns[$column_name]['Null'];
660 $columns[$column_name]['COLUMN_KEY'] =& $columns[$column_name]['Key'];
661 $columns[$column_name]['COLUMN_DEFAULT'] =& $columns[$column_name]['Default'];
662 $columns[$column_name]['EXTRA'] =& $columns[$column_name]['Extra'];
663 $columns[$column_name]['PRIVILEGES'] =& $columns[$column_name]['Privileges'];
664 $columns[$column_name]['COLUMN_COMMENT'] =& $columns[$column_name]['Comment'];
666 $columns[$column_name]['TABLE_CATALOG'] = null;
667 $columns[$column_name]['TABLE_SCHEMA'] = $database;
668 $columns[$column_name]['TABLE_NAME'] = $table;
669 $columns[$column_name]['ORDINAL_POSITION'] = $ordinal_position;
670 $columns[$column_name]['DATA_TYPE'] =
671 substr($columns[$column_name]['COLUMN_TYPE'], 0,
672 strpos($columns[$column_name]['COLUMN_TYPE'], '('));
674 * @todo guess CHARACTER_MAXIMUM_LENGTH from COLUMN_TYPE
676 $columns[$column_name]['CHARACTER_MAXIMUM_LENGTH'] = null;
678 * @todo guess CHARACTER_OCTET_LENGTH from CHARACTER_MAXIMUM_LENGTH
680 $columns[$column_name]['CHARACTER_OCTET_LENGTH'] = null;
681 $columns[$column_name]['NUMERIC_PRECISION'] = null;
682 $columns[$column_name]['NUMERIC_SCALE'] = null;
683 $columns[$column_name]['CHARACTER_SET_NAME'] =
684 substr($columns[$column_name]['COLLATION_NAME'], 0,
685 strpos($columns[$column_name]['COLLATION_NAME'], '_'));
690 if (null !== $column) {
692 $columns = current($columns);
700 * @todo should only return columns names, for more info use PMA_DBI_get_columns_full()
702 * @deprecated by PMA_DBI_get_columns() or PMA_DBI_get_columns_full()
703 * @param string $database name of database
704 * @param string $table name of table to retrieve columns from
705 * @param mixed $link mysql link resource
706 * @return array column info
708 function PMA_DBI_get_fields($database, $table, $link = null)
710 // here we use a try_query because when coming from
711 // tbl_create + tbl_properties.inc.php, the table does not exist
712 $fields = PMA_DBI_fetch_result(
714 FROM ' . PMA_backquote($database) . '.' . PMA_backquote($table),
716 if (! is_array($fields) ||
count($fields) < 1) {
723 * array PMA_DBI_get_columns(string $database, string $table, bool $full = false, mysql db link $link = null)
725 * @param string $database name of database
726 * @param string $table name of table to retrieve columns from
727 * @param boolean $full wether to return full info or only column names
728 * @param mixed $link mysql link resource
729 * @return array column names
731 function PMA_DBI_get_columns($database, $table, $full = false, $link = null)
733 $fields = PMA_DBI_fetch_result(
734 'SHOW ' . ($full ?
'FULL' : '') . ' COLUMNS
735 FROM ' . PMA_backquote($database) . '.' . PMA_backquote($table),
736 'Field', ($full ?
null : 'Field'), $link);
737 if (! is_array($fields) ||
count($fields) < 1) {
744 * returns value of given mysql server variable
746 * @param string $var mysql server variable name
747 * @param int $type PMA_DBI_GETVAR_SESSION|PMA_DBI_GETVAR_GLOBAL
748 * @param mixed $link mysql link resource|object
749 * @return mixed value for mysql server variable
751 function PMA_DBI_get_variable($var, $type = PMA_DBI_GETVAR_SESSION
, $link = null)
753 if ($link === null) {
754 if (isset($GLOBALS['userlink'])) {
755 $link = $GLOBALS['userlink'];
760 if (PMA_MYSQL_INT_VERSION
< 40002) {
764 case PMA_DBI_GETVAR_SESSION
:
765 $modifier = ' SESSION';
767 case PMA_DBI_GETVAR_GLOBAL
:
768 $modifier = ' GLOBAL';
773 return PMA_DBI_fetch_value(
774 'SHOW' . $modifier . ' VARIABLES LIKE \'' . $var . '\';', 0, 1, $link);
778 * @uses ./libraries/charset_conversion.lib.php
779 * @uses PMA_DBI_QUERY_STORE
780 * @uses PMA_REMOVED_NON_UTF_8
781 * @uses PMA_MYSQL_INT_VERSION
782 * @uses PMA_MYSQL_STR_VERSION
783 * @uses PMA_DBI_GETVAR_SESSION
784 * @uses PMA_DBI_fetch_value()
785 * @uses PMA_DBI_query()
786 * @uses PMA_DBI_get_variable()
787 * @uses $GLOBALS['collation_connection']
788 * @uses $GLOBALS['charset_connection']
789 * @uses $GLOBALS['available_languages']
790 * @uses $GLOBALS['mysql_charset_map']
791 * @uses $GLOBALS['charset']
792 * @uses $GLOBALS['lang']
793 * @uses $GLOBALS['cfg']['Lang']
794 * @uses $GLOBALS['cfg']['ColumnTypes']
803 * @param mixed $link mysql link resource|object
804 * @param boolean $is_controluser
806 function PMA_DBI_postConnect($link, $is_controluser = false)
808 if (!defined('PMA_MYSQL_INT_VERSION')) {
809 $mysql_version = PMA_DBI_fetch_value(
810 'SELECT VERSION()', 0, 0, $link, PMA_DBI_QUERY_STORE
);
811 if ($mysql_version) {
812 $match = explode('.', $mysql_version);
813 define('PMA_MYSQL_INT_VERSION',
814 (int) sprintf('%d%02d%02d', $match[0], $match[1],
816 define('PMA_MYSQL_STR_VERSION', $mysql_version);
817 unset($mysql_version, $match);
819 define('PMA_MYSQL_INT_VERSION', 32332);
820 define('PMA_MYSQL_STR_VERSION', '3.23.32');
824 if (!defined('PMA_ENGINE_KEYWORD')) {
825 if (PMA_MYSQL_INT_VERSION
>= 40102) {
826 define('PMA_ENGINE_KEYWORD','ENGINE');
828 define('PMA_ENGINE_KEYWORD','TYPE');
832 if (PMA_MYSQL_INT_VERSION
>= 40100) {
834 // If $lang is defined and we are on MySQL >= 4.1.x,
835 // we auto-switch the lang to its UTF-8 version (if it exists and user
836 // didn't force language)
837 if (!empty($GLOBALS['lang'])
838 && (substr($GLOBALS['lang'], -5) != 'utf-8')
839 && !isset($GLOBALS['cfg']['Lang'])) {
840 $lang_utf_8_version =
841 substr($GLOBALS['lang'], 0, strpos($GLOBALS['lang'], '-'))
843 if (!empty($GLOBALS['available_languages'][$lang_utf_8_version])) {
844 $GLOBALS['lang'] = $lang_utf_8_version;
845 $GLOBALS['charset'] = 'utf-8';
846 define('PMA_LANG_RELOAD', 1);
850 // and we remove the non-UTF-8 choices to avoid confusion
851 // (unless there is a forced language)
852 if (!defined('PMA_REMOVED_NON_UTF_8') && ! isset($GLOBALS['cfg']['Lang'])) {
853 foreach ($GLOBALS['available_languages'] as $each_lang => $dummy) {
854 if (substr($each_lang, -5) != 'utf-8') {
855 unset($GLOBALS['available_languages'][$each_lang]);
858 define('PMA_REMOVED_NON_UTF_8', 1);
861 $mysql_charset = $GLOBALS['mysql_charset_map'][$GLOBALS['charset']];
863 ||
empty($GLOBALS['collation_connection'])
864 ||
(strpos($GLOBALS['collation_connection'], '_')
865 ?
substr($GLOBALS['collation_connection'], 0, strpos($GLOBALS['collation_connection'], '_'))
866 : $GLOBALS['collation_connection']) == $mysql_charset) {
868 PMA_DBI_query('SET NAMES ' . $mysql_charset . ';', $link,
869 PMA_DBI_QUERY_STORE
);
871 PMA_DBI_query('SET CHARACTER SET ' . $mysql_charset . ';', $link,
872 PMA_DBI_QUERY_STORE
);
874 if (!empty($GLOBALS['collation_connection'])) {
875 PMA_DBI_query('SET collation_connection = \'' . $GLOBALS['collation_connection'] . '\';',
876 $link, PMA_DBI_QUERY_STORE
);
878 if (!$is_controluser) {
879 $GLOBALS['collation_connection'] = PMA_DBI_get_variable('collation_connection',
880 PMA_DBI_GETVAR_SESSION
, $link);
881 $GLOBALS['charset_connection'] = PMA_DBI_get_variable('character_set_connection',
882 PMA_DBI_GETVAR_SESSION
, $link);
885 // Add some field types to the list, this needs to be done once per session!
886 if (!in_array('BINARY', $GLOBALS['cfg']['ColumnTypes'])) {
887 $GLOBALS['cfg']['ColumnTypes'][] = 'BINARY';
889 if (!in_array('VARBINARY', $GLOBALS['cfg']['ColumnTypes'])) {
890 $GLOBALS['cfg']['ColumnTypes'][] = 'VARBINARY';
893 require_once './libraries/charset_conversion.lib.php';
898 * returns a single value from the given result or query,
899 * if the query or the result has more than one row or field
900 * the first field of the first row is returned
903 * $sql = 'SELECT `name` FROM `user` WHERE `id` = 123';
904 * $user_name = PMA_DBI_fetch_value($sql);
906 * // $user_name = 'John Doe'
911 * @uses PMA_DBI_try_query()
912 * @uses PMA_DBI_num_rows()
913 * @uses PMA_DBI_fetch_row()
914 * @uses PMA_DBI_fetch_assoc()
915 * @uses PMA_DBI_free_result()
916 * @param string|mysql_result $result query or mysql result
917 * @param integer $row_number row to fetch the value from,
918 * starting at 0, with 0 beeing default
919 * @param integer|string $field field to fetch the value from,
920 * starting at 0, with 0 beeing default
921 * @param resource $link mysql link
922 * @param mixed $options
923 * @return mixed value of first field in first row from result
924 * or false if not found
926 function PMA_DBI_fetch_value($result, $row_number = 0, $field = 0, $link = null, $options = 0) {
929 if (is_string($result)) {
930 $result = PMA_DBI_try_query($result, $link, $options | PMA_DBI_QUERY_STORE
);
933 // return false if result is empty or false
934 // or requested row is larger than rows in result
935 if (PMA_DBI_num_rows($result) < ($row_number +
1)) {
939 // if $field is an integer use non associative mysql fetch function
940 if (is_int($field)) {
941 $fetch_function = 'PMA_DBI_fetch_row';
943 $fetch_function = 'PMA_DBI_fetch_assoc';
947 for ($i = 0; $i <= $row_number; $i++
) {
948 $row = $fetch_function($result);
950 PMA_DBI_free_result($result);
952 // return requested field
953 if (isset($row[$field])) {
954 $value = $row[$field];
962 * returns only the first row from the result
965 * $sql = 'SELECT * FROM `user` WHERE `id` = 123';
966 * $user = PMA_DBI_fetch_single_row($sql);
968 * // $user = array('id' => 123, 'name' => 'John Doe')
972 * @uses PMA_DBI_try_query()
973 * @uses PMA_DBI_num_rows()
974 * @uses PMA_DBI_fetch_row()
975 * @uses PMA_DBI_fetch_assoc()
976 * @uses PMA_DBI_fetch_array()
977 * @uses PMA_DBI_free_result()
978 * @param string|mysql_result $result query or mysql result
979 * @param string $type NUM|ASSOC|BOTH
980 * returned array should either numeric
981 * associativ or booth
982 * @param resource $link mysql link
983 * @param mixed $options
984 * @return array|boolean first row from result
985 * or false if result is empty
987 function PMA_DBI_fetch_single_row($result, $type = 'ASSOC', $link = null, $options = 0) {
988 if (is_string($result)) {
989 $result = PMA_DBI_try_query($result, $link, $options | PMA_DBI_QUERY_STORE
);
992 // return null if result is empty or false
993 if (! PMA_DBI_num_rows($result)) {
999 $fetch_function = 'PMA_DBI_fetch_row';
1002 $fetch_function = 'PMA_DBI_fetch_assoc';
1006 $fetch_function = 'PMA_DBI_fetch_array';
1010 $row = $fetch_function($result);
1011 PMA_DBI_free_result($result);
1016 * returns all rows in the resultset in one array
1019 * $sql = 'SELECT * FROM `user`';
1020 * $users = PMA_DBI_fetch_result($sql);
1022 * // $users[] = array('id' => 123, 'name' => 'John Doe')
1024 * $sql = 'SELECT `id`, `name` FROM `user`';
1025 * $users = PMA_DBI_fetch_result($sql, 'id');
1027 * // $users['123'] = array('id' => 123, 'name' => 'John Doe')
1029 * $sql = 'SELECT `id`, `name` FROM `user`';
1030 * $users = PMA_DBI_fetch_result($sql, 0);
1032 * // $users['123'] = array(0 => 123, 1 => 'John Doe')
1034 * $sql = 'SELECT `id`, `name` FROM `user`';
1035 * $users = PMA_DBI_fetch_result($sql, 'id', 'name');
1037 * $users = PMA_DBI_fetch_result($sql, 0, 1);
1039 * // $users['123'] = 'John Doe'
1041 * $sql = 'SELECT `name` FROM `user`';
1042 * $users = PMA_DBI_fetch_result($sql);
1044 * // $users[] = 'John Doe'
1049 * @uses PMA_DBI_try_query()
1050 * @uses PMA_DBI_num_rows()
1051 * @uses PMA_DBI_num_fields()
1052 * @uses PMA_DBI_fetch_row()
1053 * @uses PMA_DBI_fetch_assoc()
1054 * @uses PMA_DBI_free_result()
1055 * @param string|mysql_result $result query or mysql result
1056 * @param string|integer $key field-name or offset
1057 * used as key for array
1058 * @param string|integer $value value-name or offset
1059 * used as value for array
1060 * @param resource $link mysql link
1061 * @param mixed $options
1062 * @return array resultrows or values indexed by $key
1064 function PMA_DBI_fetch_result($result, $key = null, $value = null,
1065 $link = null, $options = 0)
1067 $resultrows = array();
1069 if (is_string($result)) {
1070 $result = PMA_DBI_try_query($result, $link, $options);
1073 // return empty array if result is empty or false
1078 $fetch_function = 'PMA_DBI_fetch_assoc';
1080 // no nested array if only one field is in result
1081 if (null === $key && 1 === PMA_DBI_num_fields($result)) {
1083 $fetch_function = 'PMA_DBI_fetch_row';
1086 // if $key is an integer use non associative mysql fetch function
1088 $fetch_function = 'PMA_DBI_fetch_row';
1091 if (null === $key && null === $value) {
1092 while ($row = $fetch_function($result)) {
1093 $resultrows[] = $row;
1095 } elseif (null === $key) {
1096 while ($row = $fetch_function($result)) {
1097 $resultrows[] = $row[$value];
1099 } elseif (null === $value) {
1100 if (is_array($key)) {
1101 while ($row = $fetch_function($result)) {
1102 $result_target =& $resultrows;
1103 foreach ($key as $key_index) {
1104 if (! isset($result_target[$row[$key_index]])) {
1105 $result_target[$row[$key_index]] = array();
1107 $result_target =& $result_target[$row[$key_index]];
1109 $result_target = $row;
1112 while ($row = $fetch_function($result)) {
1113 $resultrows[$row[$key]] = $row;
1117 if (is_array($key)) {
1118 while ($row = $fetch_function($result)) {
1119 $result_target =& $resultrows;
1120 foreach ($key as $key_index) {
1121 if (! isset($result_target[$row[$key_index]])) {
1122 $result_target[$row[$key_index]] = array();
1124 $result_target =& $result_target[$row[$key_index]];
1126 $result_target = $row[$value];
1129 while ($row = $fetch_function($result)) {
1130 $resultrows[$row[$key]] = $row[$value];
1135 PMA_DBI_free_result($result);
1140 * return default table engine for given database
1142 * @return string default table engine
1144 function PMA_DBI_get_default_engine()
1146 if (PMA_MYSQL_INT_VERSION
> 50002) {
1147 return PMA_DBI_fetch_value('SHOW VARIABLES LIKE \'storage_engine\';', 0, 1);
1149 return PMA_DBI_fetch_value('SHOW VARIABLES LIKE \'table_type\';', 0, 1);
1154 * Get supported SQL compatibility modes
1156 * @return array supported SQL compatibility modes
1158 function PMA_DBI_getCompatibilities()
1160 if (PMA_MYSQL_INT_VERSION
< 40100) {
1163 $compats = array('NONE');
1164 if (PMA_MYSQL_INT_VERSION
>= 40101) {
1165 $compats[] = 'ANSI';
1167 $compats[] = 'MAXDB';
1168 $compats[] = 'MYSQL323';
1169 $compats[] = 'MYSQL40';
1170 $compats[] = 'MSSQL';
1171 $compats[] = 'ORACLE';
1172 // removed; in MySQL 5.0.33, this produces exports that
1173 // can't be read by POSTGRESQL (see our bug #1596328)
1174 //$compats[] = 'POSTGRESQL';
1175 if (PMA_MYSQL_INT_VERSION
>= 50002) {
1176 $compats[] = 'TRADITIONAL';
1183 * returns warnings for last query
1185 * @uses $GLOBALS['userlink']
1186 * @uses PMA_DBI_fetch_result()
1187 * @param resource mysql link $link mysql link resource
1188 * @return array warnings
1190 function PMA_DBI_get_warnings($link = null)
1192 if (PMA_MYSQL_INT_VERSION
< 40100) {
1197 if (isset($GLOBALS['userlink'])) {
1198 $link = $GLOBALS['userlink'];
1204 return PMA_DBI_fetch_result('SHOW WARNINGS', null, null, $link);
1208 * returns true (int > 0) if current user is superuser
1211 * @return integer $is_superuser
1213 function PMA_isSuperuser() {
1214 return PMA_DBI_try_query('SELECT COUNT(*) FROM mysql.user',
1215 $GLOBALS['userlink'], PMA_DBI_QUERY_STORE
);
1220 * returns an array of PROCEDURE or FUNCTION names for a db
1222 * @uses PMA_DBI_free_result()
1223 * @param string $db db name
1224 * @param string $which PROCEDURE | FUNCTION
1225 * @param resource $link mysql link
1227 * @return array the procedure names or function names
1229 function PMA_DBI_get_procedures_or_functions($db, $which, $link = null) {
1231 $shows = PMA_DBI_fetch_result('SHOW ' . $which . ' STATUS;', null, null, $link);
1233 foreach ($shows as $one_show) {
1234 if ($one_show['Db'] == $db && $one_show['Type'] == $which) {
1235 $result[] = $one_show['Name'];
1242 * returns the definition of a specific PROCEDURE or FUNCTION
1244 * @uses PMA_DBI_fetch_value()
1245 * @param string $db db name
1246 * @param string $which PROCEDURE | FUNCTION
1247 * @param string $proc_or_function_name the procedure name or function name
1248 * @param resource $link mysql link
1250 * @return string the procedure's or function's definition
1252 function PMA_DBI_get_procedure_or_function_def($db, $which, $proc_or_function_name, $link = null) {
1254 $returned_field = array('PROCEDURE' => 'Create Procedure', 'FUNCTION' => 'Create Function');
1255 $query = 'SHOW CREATE ' . $which . ' ' . PMA_backquote($db) . '.' . PMA_backquote($proc_or_function_name);
1256 return(PMA_DBI_fetch_value($query, 0, $returned_field[$which]));
1260 * returns details about the TRIGGERs of a specific table
1262 * @uses PMA_DBI_fetch_result()
1263 * @param string $db db name
1264 * @param string $table table name
1266 * @return array information about triggers (may be empty)
1268 function PMA_DBI_get_triggers($db, $table) {
1272 // available in INFORMATION_SCHEMA since MySQL 5.0.10
1273 // Note: in http://dev.mysql.com/doc/refman/5.0/en/faqs-triggers.html
1274 // their example uses WHERE TRIGGER_SCHEMA='dbname' so let's use this
1275 // instead of WHERE EVENT_OBJECT_SCHEMA='dbname'
1276 if (PMA_MYSQL_INT_VERSION
>= 50010) {
1277 $triggers = PMA_DBI_fetch_result("SELECT TRIGGER_SCHEMA, TRIGGER_NAME, EVENT_MANIPULATION, ACTION_TIMING, ACTION_STATEMENT, EVENT_OBJECT_SCHEMA, EVENT_OBJECT_TABLE FROM information_schema.TRIGGERS WHERE TRIGGER_SCHEMA= '" . PMA_sqlAddslashes($db,true) . "' and EVENT_OBJECT_TABLE = '" . PMA_sqlAddslashes($table, true) . "';");
1281 foreach ($triggers as $trigger) {
1282 $one_result = array();
1283 $one_result['name'] = $trigger['TRIGGER_NAME'];
1284 $one_result['action_timing'] = $trigger['ACTION_TIMING'];
1285 $one_result['event_manipulation'] = $trigger['EVENT_MANIPULATION'];
1287 $one_result['full_trigger_name'] = PMA_backquote($trigger['TRIGGER_SCHEMA']) . '.' . PMA_backquote($trigger['TRIGGER_NAME']);
1288 $one_result['drop'] = 'DROP TRIGGER IF EXISTS ' . $one_result['full_trigger_name'];
1289 $one_result['create'] = 'CREATE TRIGGER ' . $one_result['full_trigger_name'] . ' ' . $trigger['ACTION_TIMING']. ' ' . $trigger['EVENT_MANIPULATION'] . ' ON ' . PMA_backquote($trigger['EVENT_OBJECT_SCHEMA']) . '.' . PMA_backquote($trigger['EVENT_OBJECT_TABLE']) . "\n" . ' FOR EACH ROW ' . $trigger['ACTION_STATEMENT'] . "\n" . $delimiter . "\n";
1291 $result[] = $one_result;