no longer used
[phpmyadmin/crack.git] / libraries / database_interface.lib.php
blobf09b77e46d9a05dfb657ca209ac849539d151bfe
1 <?php
2 /* $Id$ */
3 // vim: expandtab sw=4 ts=4 sts=4:
5 /**
6 * Common Option Constants For DBI Functions
7 */
8 // PMA_DBI_try_query()
9 define('PMA_DBI_QUERY_STORE', 1); // Force STORE_RESULT method, ignored by classic MySQL.
10 define('PMA_DBI_QUERY_UNBUFFERED', 2); // Do not read whole query
11 // PMA_DBI_get_variable()
12 define('PMA_DBI_GETVAR_SESSION', 1);
13 define('PMA_DBI_GETVAR_GLOBAL', 2);
15 /**
16 * Loads the mysql extensions if it is not loaded yet
18 * @param string $extension mysql extension to load
20 function PMA_DBI_checkAndLoadMysqlExtension( $extension = 'mysql' ) {
21 if ( ! function_exists( $extension . '_connect' ) ) {
22 PMA_dl( $extension );
23 // check whether mysql is available
24 if ( ! function_exists( $extension . '_connect' ) ) {
25 return false;
29 return true;
33 /**
34 * check for requested extension
36 if ( ! PMA_DBI_checkAndLoadMysqlExtension( $GLOBALS['cfg']['Server']['extension'] ) ) {
38 // if it fails try alternative extension ...
39 // and display an error ...
41 // TODO 2.7.1: add different messages for alternativ extension
42 // and complete fail (no alternativ extension too)
43 $GLOBALS['PMA_errors'][] =
44 sprintf( PMA_sanitize( $GLOBALS['strCantLoad'] ),
45 $GLOBALS['cfg']['Server']['extension'] )
46 .' - <a href="./Documentation.html#faqmysql" target="documentation">'
47 .$GLOBALS['strDocu'] . '</a>';
49 if ( $GLOBALS['cfg']['Server']['extension'] === 'mysql' ) {
50 $alternativ_extension = 'mysqli';
51 } else {
52 $alternativ_extension = 'mysql';
55 if ( ! PMA_DBI_checkAndLoadMysqlExtension( $alternativ_extension ) ) {
56 // if alternativ fails too ...
57 header( 'Location: error.php'
58 . '?lang=' . urlencode( $available_languages[$lang][2] )
59 . '&char=' . urlencode( $charset )
60 . '&dir=' . urlencode( $text_dir )
61 . '&type=' . urlencode( $strError )
62 . '&error=' . urlencode(
63 sprintf( $GLOBALS['strCantLoad'],
64 $GLOBALS['cfg']['Server']['extension'] )
65 .' - [a@./Documentation.html#faqmysql@documentation]'
66 .$GLOBALS['strDocu'] . '[/a]' )
67 . '&' . SID
69 exit();
72 $GLOBALS['cfg']['Server']['extension'] = $alternativ_extension;
73 unset( $alternativ_extension );
76 /**
77 * Including The DBI Plugin
79 require_once('./libraries/dbi/' . $GLOBALS['cfg']['Server']['extension'] . '.dbi.lib.php');
81 /**
82 * Common Functions
84 function PMA_DBI_query($query, $link = null, $options = 0) {
85 $res = PMA_DBI_try_query($query, $link, $options)
86 or PMA_mysqlDie(PMA_DBI_getError($link), $query);
87 return $res;
90 /**
91 * converts charset of a mysql message, usally coming from mysql_error(),
92 * into PMA charset, usally UTF-8
93 * uses language to charset mapping from mysql/share/errmsg.txt
94 * and charset names to ISO charset from information_schema.CHARACTER_SETS
96 * @uses $GLOBALS['cfg']['IconvExtraParams']
97 * @uses $GLOBALS['charset'] as target charset
98 * @uses PMA_DBI_fetch_value() to get server_language
99 * @uses preg_match() to filter server_language
100 * @uses in_array()
101 * @uses function_exists() to check for a convert function
102 * @uses iconv() to convert message
103 * @uses libiconv() to convert message
104 * @uses recode_string() to convert message
105 * @uses mb_convert_encoding() to convert message
106 * @param string $message
107 * @return string $message
109 function PMA_DBI_convert_message( $message ) {
110 // latin always last!
111 $encodings = array(
112 'japanese' => 'EUC-JP', //'ujis',
113 'japanese-sjis' => 'Shift-JIS', //'sjis',
114 'korean' => 'EUC-KR', //'euckr',
115 'russian' => 'KOI8-R', //'koi8r',
116 'ukrainian' => 'KOI8-U', //'koi8u',
117 'greek' => 'ISO-8859-7', //'greek',
118 'serbian' => 'CP1250', //'cp1250',
119 'estonian' => 'ISO-8859-13', //'latin7',
120 'slovak' => 'ISO-8859-2', //'latin2',
121 'czech' => 'ISO-8859-2', //'latin2',
122 'hungarian' => 'ISO-8859-2', //'latin2',
123 'polish' => 'ISO-8859-2', //'latin2',
124 'romanian' => 'ISO-8859-2', //'latin2',
125 'spanish' => 'CP1252', //'latin1',
126 'swedish' => 'CP1252', //'latin1',
127 'italian' => 'CP1252', //'latin1',
128 'norwegian-ny' => 'CP1252', //'latin1',
129 'norwegian' => 'CP1252', //'latin1',
130 'portuguese' => 'CP1252', //'latin1',
131 'danish' => 'CP1252', //'latin1',
132 'dutch' => 'CP1252', //'latin1',
133 'english' => 'CP1252', //'latin1',
134 'french' => 'CP1252', //'latin1',
135 'german' => 'CP1252', //'latin1',
138 if ( $server_language = PMA_DBI_fetch_value( 'SHOW VARIABLES LIKE \'language\';', 0, 1 ) ) {
139 $found = array();
140 if ( preg_match( '&(?:\\\|\\/)([^\\\\\/]*)(?:\\\|\\/)$&i', $server_language, $found )) {
141 $server_language = $found[1];
145 if ( ! empty( $server_language ) && isset( $encodings[$server_language] ) ) {
146 if ( function_exists( 'iconv' ) ) {
147 if ((@stristr(PHP_OS, 'AIX')) && (@strcasecmp(ICONV_IMPL, 'unknown') == 0) && (@strcasecmp(ICONV_VERSION, 'unknown') == 0)) {
148 require_once('./liraries/iconv_wrapper.lib.php');
149 $message = PMA_aix_iconv_wrapper( $encodings[$server_language],
150 $GLOBALS['charset'] . $GLOBALS['cfg']['IconvExtraParams'], $message);
151 } else {
152 $message = iconv( $encodings[$server_language],
153 $GLOBALS['charset'] . $GLOBALS['cfg']['IconvExtraParams'], $message);
155 } elseif ( function_exists( 'recode_string' ) ) {
156 $message = recode_string( $encodings[$server_language] . '..' . $GLOBALS['charset'],
157 $message );
158 } elseif ( function_exists( 'libiconv' ) ) {
159 $message = libiconv( $encodings[$server_language], $GLOBALS['charset'], $message );
160 } elseif ( function_exists( 'mb_convert_encoding' ) ) {
161 // do not try unsupported charsets
162 if ( ! in_array( $server_language, array( 'ukrainian', 'greek', 'serbian' ) ) ) {
163 $message = mb_convert_encoding( $message, $GLOBALS['charset'],
164 $encodings[$server_language] );
167 } else {
168 // lang not found, try all
169 // what TODO ?
172 return $message;
176 * returns array with database names
178 * @return array $databases
180 function PMA_DBI_get_dblist($link = null)
182 $dbs_array = PMA_DBI_fetch_result('SHOW DATABASES;', $link);
184 // Before MySQL 4.0.2, SHOW DATABASES could send the
185 // whole list, so check if we really have access:
186 if (PMA_MYSQL_INT_VERSION < 40002 || !empty($GLOBALS['cfg']['Server']['hide_db'])) {
187 foreach ($dbs_array as $key => $db) {
188 if (!@PMA_DBI_select_db($db, $link) || (!empty($GLOBALS['cfg']['Server']['hide_db']) && preg_match('/' . $GLOBALS['cfg']['Server']['hide_db'] . '/', $db))) {
189 unset( $dbs_array[$key] );
192 // re-index values
193 $dbs_array = array_values( $dbs_array );
196 return $dbs_array;
200 * returns array with table names for given db
202 * @param string $database name of database
203 * @param mixed $link mysql link resource|object
204 * @return array tables names
206 function PMA_DBI_get_tables($database, $link = null)
208 return PMA_DBI_fetch_result('SHOW TABLES FROM ' . PMA_backquote($database) . ';',
209 null, 0, $link, PMA_DBI_QUERY_STORE);
213 * returns array of all tables in given db or dbs
214 * this function expects unqoted names:
215 * RIGHT: my_database
216 * WRONG: `my_database`
217 * WRONG: my\_database
218 * if $tbl_is_group is true, $table is used as filter for table names
219 * if $tbl_is_group is 'comment, $table is used as filter for table comments
221 * <code>
222 * PMA_DBI_get_tables_full( 'my_database' );
223 * PMA_DBI_get_tables_full( 'my_database', 'my_table' ) );
224 * PMA_DBI_get_tables_full( 'my_database', 'my_tables_', true ) );
225 * PMA_DBI_get_tables_full( 'my_database', 'my_tables_', 'comment' ) );
226 * </code>
228 * @uses PMA_MYSQL_INT_VERSION
229 * @uses PMA_DBI_fetch_result()
230 * @uses PMA_escape_mysql_wildcards()
231 * @uses PMA_backquote()
232 * @uses is_array()
233 * @uses addslashes()
234 * @uses strpos()
235 * @uses strtoupper()
236 * @param string $databases database
237 * @param string $table table
238 * @param boolean|string $tbl_is_group $table is a table group
239 * @param resource $link mysql link
240 * @return array list of tbales in given db(s)
242 function PMA_DBI_get_tables_full($database, $table = false,
243 $tbl_is_group = false, $link = null)
245 // prepare and check parameters
246 if ( ! is_array($database) ) {
247 $databases = array(addslashes($database));
248 } else {
249 $databases = array_map('addslashes', $database);
252 $tables = array();
254 if ( PMA_MYSQL_INT_VERSION >= 50002 ) {
255 // get table information from information_schema
256 if ( $table ) {
257 if ( true === $tbl_is_group ) {
258 $sql_where_table = 'AND `TABLE_NAME` LIKE \''
259 . PMA_escape_mysql_wildcards(addslashes($table)) . '%\'';
260 } elseif ( 'comment' === $tbl_is_group ) {
261 $sql_where_table = 'AND `TABLE_COMMENT` LIKE \''
262 . PMA_escape_mysql_wildcards(addslashes($table)) . '%\'';
263 } else {
264 $sql_where_table = 'AND `TABLE_NAME` = \'' . addslashes($table) . '\'';
266 } else {
267 $sql_where_table = '';
270 // for PMA bc:
271 // `SCHEMA_FIELD_NAME` AS `SHOW_TABLE_STATUS_FIELD_NAME`
272 $sql = '
273 SELECT *,
274 `TABLE_SCHEMA` AS `Db`,
275 `TABLE_NAME` AS `Name`,
276 `ENGINE` AS `Engine`,
277 `ENGINE` AS `Type`,
278 `VERSION` AS `Version`,
279 `ROW_FORMAT` AS `Row_format`,
280 `TABLE_ROWS` AS `Rows`,
281 `AVG_ROW_LENGTH` AS `Avg_row_length`,
282 `DATA_LENGTH` AS `Data_length`,
283 `MAX_DATA_LENGTH` AS `Max_data_length`,
284 `INDEX_LENGTH` AS `Index_length`,
285 `DATA_FREE` AS `Data_free`,
286 `AUTO_INCREMENT` AS `Auto_increment`,
287 `CREATE_TIME` AS `Create_time`,
288 `UPDATE_TIME` AS `Update_time`,
289 `CHECK_TIME` AS `Check_time`,
290 `TABLE_COLLATION` AS `Collation`,
291 `CHECKSUM` AS `Checksum`,
292 `CREATE_OPTIONS` AS `Create_options`,
293 `TABLE_COMMENT` AS `Comment`
294 FROM `information_schema`.`TABLES`
295 WHERE `TABLE_SCHEMA` IN (\'' . implode("', '", $databases) . '\')
296 ' . $sql_where_table;
297 $tables = PMA_DBI_fetch_result($sql, array('TABLE_SCHEMA', 'TABLE_NAME'),
298 null, $link);
299 unset( $sql_where_table, $sql );
300 } else {
301 foreach ( $databases as $each_database ) {
302 if ( true === $tbl_is_group ) {
303 $sql = 'SHOW TABLE STATUS FROM '
304 . PMA_backquote($each_database)
305 .' LIKE \'' . PMA_escape_mysql_wildcards(addslashes($table)) . '%\'';
306 } else {
307 $sql = 'SHOW TABLE STATUS FROM '
308 . PMA_backquote($each_database) . ';';
310 $each_tables = PMA_DBI_fetch_result($sql, 'Name', null, $link);
311 foreach ( $each_tables as $table_name => $each_table ) {
312 if ( 'comment' === $tbl_is_group
313 && 0 === strpos($each_table['Comment'], $table) )
315 // remove table from list
316 unset( $each_tables[$table_name] );
317 continue;
320 if ( ! isset( $each_tables[$table_name]['Type'] )
321 && isset( $each_tables[$table_name]['Engine'] ) ) {
322 // pma BC, same parts of PMA still uses 'Type'
323 $each_tables[$table_name]['Type']
324 =& $each_tables[$table_name]['Engine'];
325 } elseif ( ! isset( $each_tables[$table_name]['Engine'] )
326 && isset( $each_tables[$table_name]['Type'] ) ) {
327 // old MySQL reports Type, newer MySQL reports Engine
328 $each_tables[$table_name]['Engine']
329 =& $each_tables[$table_name]['Type'];
332 // MySQL forward compatibility
333 // so pma could use this array as if every server is of version >5.0
334 $each_tables[$table_name]['TABLE_SCHEMA'] = $each_database;
335 $each_tables[$table_name]['TABLE_NAME'] =& $each_tables[$table_name]['Name'];
336 $each_tables[$table_name]['ENGINE'] =& $each_tables[$table_name]['Engine'];
337 $each_tables[$table_name]['VERSION'] =& $each_tables[$table_name]['Version'];
338 $each_tables[$table_name]['ROW_FORMAT'] =& $each_tables[$table_name]['Row_format'];
339 $each_tables[$table_name]['TABLE_ROWS'] =& $each_tables[$table_name]['Rows'];
340 $each_tables[$table_name]['AVG_ROW_LENGTH'] =& $each_tables[$table_name]['Avg_row_length'];
341 $each_tables[$table_name]['DATA_LENGTH'] =& $each_tables[$table_name]['Data_length'];
342 $each_tables[$table_name]['MAX_DATA_LENGTH'] =& $each_tables[$table_name]['Max_data_length'];
343 $each_tables[$table_name]['INDEX_LENGTH'] =& $each_tables[$table_name]['Index_length'];
344 $each_tables[$table_name]['DATA_FREE'] =& $each_tables[$table_name]['Data_free'];
345 $each_tables[$table_name]['AUTO_INCREMENT'] =& $each_tables[$table_name]['Auto_increment'];
346 $each_tables[$table_name]['CREATE_TIME'] =& $each_tables[$table_name]['Create_time'];
347 $each_tables[$table_name]['UPDATE_TIME'] =& $each_tables[$table_name]['Update_time'];
348 $each_tables[$table_name]['CHECK_TIME'] =& $each_tables[$table_name]['Check_time'];
349 $each_tables[$table_name]['TABLE_COLLATION'] =& $each_tables[$table_name]['Collation'];
350 $each_tables[$table_name]['CHECKSUM'] =& $each_tables[$table_name]['Checksum'];
351 $each_tables[$table_name]['CREATE_OPTIONS'] =& $each_tables[$table_name]['Create_options'];
352 $each_tables[$table_name]['TABLE_COMMENT'] =& $each_tables[$table_name]['Comment'];
354 if ( strtoupper( $each_tables[$table_name]['Comment'] ) === 'VIEW' ) {
355 $each_tables[$table_name]['TABLE_TYPE'] = 'VIEW';
356 } else {
357 // TODO difference between 'TEMPORARY' and 'BASE TABLE'
358 // 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
378 // MySQL returns
379 // with SHOW DATABASES or information_schema.SCHEMATA: `Test`
380 // but information_schema.TABLES gives `test`
381 // bug #1436171
382 // sf.net/tracker/?func=detail&aid=1436171&group_id=23067&atid=377408
383 return $tables[strtolower($database)];
384 } else {
385 return $tables;
387 } else {
388 return $tables;
393 * returns array with databases containing extended infos about them
395 * @param string $databases database
396 * @param boolean $force_stats retrieve stats also for MySQL < 5
397 * @param resource $link mysql link
398 * @return array $databases
400 function PMA_DBI_get_databases_full( $database = null, $force_stats = false, $link = null ) {
402 // initialize to avoid errors when there are no databases
403 $databases = array();
405 if ( PMA_MYSQL_INT_VERSION >= 50002 ) {
406 // get table information from information_schema
407 if ( $database ) {
408 $sql_where_schema = 'WHERE `SCHEMA_NAME` LIKE \''
409 . addslashes( $database ) . '\'';
410 } else {
411 $sql_where_schema = '';
414 // for PMA bc:
415 // `SCHEMA_FIELD_NAME` AS `SHOW_TABLE_STATUS_FIELD_NAME`
416 $sql = '
417 SELECT `information_schema`.`SCHEMATA`.*,
418 COUNT(`information_schema`.`TABLES`.`TABLE_SCHEMA`)
419 AS `SCHEMA_TABLES`,
420 SUM(`information_schema`.`TABLES`.`TABLE_ROWS`)
421 AS `SCHEMA_TABLE_ROWS`,
422 SUM(`information_schema`.`TABLES`.`DATA_LENGTH`)
423 AS `SCHEMA_DATA_LENGTH`,
424 SUM(`information_schema`.`TABLES`.`MAX_DATA_LENGTH`)
425 AS `SCHEMA_MAX_DATA_LENGTH`,
426 SUM(`information_schema`.`TABLES`.`INDEX_LENGTH`)
427 AS `SCHEMA_INDEX_LENGTH`,
428 SUM(`information_schema`.`TABLES`.`DATA_LENGTH`
429 + `information_schema`.`TABLES`.`INDEX_LENGTH`)
430 AS `SCHEMA_LENGTH`,
431 SUM(`information_schema`.`TABLES`.`DATA_FREE`)
432 AS `SCHEMA_DATA_FREE`
433 FROM `information_schema`.`SCHEMATA`
434 LEFT JOIN `information_schema`.`TABLES`
435 ON `information_schema`.`TABLES`.`TABLE_SCHEMA`
436 = `information_schema`.`SCHEMATA`.`SCHEMA_NAME`
437 ' . $sql_where_schema . '
438 GROUP BY `information_schema`.`SCHEMATA`.`SCHEMA_NAME`';
439 $databases = PMA_DBI_fetch_result( $sql, 'SCHEMA_NAME', null, $link );
440 unset( $sql_where_schema, $sql );
441 } else {
442 foreach ( PMA_DBI_get_dblist( $link ) as $database_name ) {
443 // MySQL forward compatibility
444 // so pma could use this array as if every server is of version >5.0
445 $databases[$database_name]['SCHEMA_NAME'] = $database_name;
447 if ( $force_stats ) {
448 require_once 'mysql_charsets.lib.php';
450 $databases[$database_name]['DEFAULT_COLLATION_NAME']
451 = PMA_getDbCollation( $database_name );
453 // get additonal info about tables
454 $databases[$database_name]['SCHEMA_TABLES'] = 0;
455 $databases[$database_name]['SCHEMA_TABLE_ROWS'] = 0;
456 $databases[$database_name]['SCHEMA_DATA_LENGTH'] = 0;
457 $databases[$database_name]['SCHEMA_MAX_DATA_LENGTH'] = 0;
458 $databases[$database_name]['SCHEMA_INDEX_LENGTH'] = 0;
459 $databases[$database_name]['SCHEMA_LENGTH'] = 0;
460 $databases[$database_name]['SCHEMA_DATA_FREE'] = 0;
462 $res = PMA_DBI_query('SHOW TABLE STATUS FROM ' . PMA_backquote( $database_name ) . ';');
463 while ( $row = PMA_DBI_fetch_assoc( $res ) ) {
464 $databases[$database_name]['SCHEMA_TABLES']++;
465 $databases[$database_name]['SCHEMA_TABLE_ROWS']
466 += $row['Rows'];
467 $databases[$database_name]['SCHEMA_DATA_LENGTH']
468 += $row['Data_length'];
469 $databases[$database_name]['SCHEMA_MAX_DATA_LENGTH']
470 += $row['Max_data_length'];
471 $databases[$database_name]['SCHEMA_INDEX_LENGTH']
472 += $row['Index_length'];
473 $databases[$database_name]['SCHEMA_DATA_FREE']
474 += $row['Data_free'];
475 $databases[$database_name]['SCHEMA_LENGTH']
476 += $row['Data_length'] + $row['Index_length'];
478 PMA_DBI_free_result( $res );
479 unset( $res );
484 if ( $GLOBALS['cfg']['NaturalOrder'] ) {
485 uksort( $databases, 'strnatcasecmp' );
488 return $databases;
492 * returns detailed array with all columns for given table in database,
493 * or all tables/databases
495 * @param string $database name of database
496 * @param string $table name of table to retrieve columns from
497 * @param string $column name of specific column
498 * @param mixed $link mysql link resource
500 function PMA_DBI_get_columns_full($database = null, $table = null,
501 $column = null, $link = null)
503 $columns = array();
505 if ( PMA_MYSQL_INT_VERSION >= 50002 ) {
506 $sql_wheres = array();
507 $array_keys = array();
509 // get columns information from information_schema
510 if ( null !== $database ) {
511 $sql_wheres[] = '`TABLE_SCHEMA` = \'' . addslashes($database) . '\' ';
512 } else {
513 $array_keys[] = 'TABLE_SCHEMA';
515 if ( null !== $table ) {
516 $sql_wheres[] = '`TABLE_NAME` = \'' . addslashes($table) . '\' ';
517 } else {
518 $array_keys[] = 'TABLE_NAME';
520 if ( null !== $column ) {
521 $sql_wheres[] = '`COLUMN_NAME` = \'' . addslashes($column) . '\' ';
522 } else {
523 $array_keys[] = 'COLUMN_NAME';
526 // for PMA bc:
527 // `[SCHEMA_FIELD_NAME]` AS `[SHOW_FULL_COLUMNS_FIELD_NAME]`
528 $sql = '
529 SELECT *,
530 `COLUMN_NAME` AS `Field`,
531 `COLUMN_TYPE` AS `Type`,
532 `COLLATION_NAME` AS `Collation`,
533 `IS_NULLABLE` AS `Null`,
534 `COLUMN_KEY` AS `Key`,
535 `COLUMN_DEFAULT` AS `Default`,
536 `EXTRA` AS `Extra`,
537 `PRIVILEGES` AS `Privileges`,
538 `COLUMN_COMMENT` AS `Comment`
539 FROM `information_schema`.`COLUMNS`';
540 if ( count($sql_wheres) ) {
541 $sql .= "\n" . ' WHERE ' . implode(' AND ', $sql_wheres);
544 $columns = PMA_DBI_fetch_result($sql, $array_keys, null, $link);
545 unset( $sql_wheres, $sql );
546 } else {
547 if ( null === $database ) {
548 $databases = PMA_DBI_get_dblist();
549 foreach ( $databases as $database ) {
550 $columns[$database] = PMA_DBI_get_columns_full($database, null,
551 null, $link);
553 return $columns;
554 } elseif ( null === $table ) {
555 $tables = PMA_DBI_get_tables($database);
556 foreach ( $tables as $table ) {
557 $columns[$table] = PMA_DBI_get_columns_full(
558 $database, $table, null, $link);
560 return $columns;
563 $sql = 'SHOW FULL COLUMNS FROM '
564 . PMA_backquote($database) . '.' . PMA_backquote($table);
565 if ( null !== $column ) {
566 $sql .= " LIKE '" . $column . "'";
569 $columns = PMA_DBI_fetch_result( $sql, 'Field', null, $link );
571 $ordinal_position = 1;
572 foreach ( $columns as $column_name => $each_column ) {
574 // MySQL forward compatibility
575 // so pma could use this array as if every server is of version >5.0
576 $columns[$column_name]['COLUMN_NAME'] =& $columns[$column_name]['Field'];
577 $columns[$column_name]['COLUMN_TYPE'] =& $columns[$column_name]['Type'];
578 $columns[$column_name]['COLLATION_NAME'] =& $columns[$column_name]['Collation'];
579 $columns[$column_name]['IS_NULLABLE'] =& $columns[$column_name]['Null'];
580 $columns[$column_name]['COLUMN_KEY'] =& $columns[$column_name]['Key'];
581 $columns[$column_name]['COLUMN_DEFAULT'] =& $columns[$column_name]['Default'];
582 $columns[$column_name]['EXTRA'] =& $columns[$column_name]['Extra'];
583 $columns[$column_name]['PRIVILEGES'] =& $columns[$column_name]['Privileges'];
584 $columns[$column_name]['COLUMN_COMMENT'] =& $columns[$column_name]['Comment'];
586 $columns[$column_name]['TABLE_CATALOG'] = null;
587 $columns[$column_name]['TABLE_SCHEMA'] = $database;
588 $columns[$column_name]['TABLE_NAME'] = $table;
589 $columns[$column_name]['ORDINAL_POSITION'] = $ordinal_position;
590 $columns[$column_name]['DATA_TYPE'] =
591 substr($columns[$column_name]['COLUMN_TYPE'], 0,
592 strpos($columns[$column_name]['COLUMN_TYPE'], '('));
593 // @TODO guess CHARACTER_MAXIMUM_LENGTH from COLUMN_TYPE
594 $columns[$column_name]['CHARACTER_MAXIMUM_LENGTH'] = null;
595 // @TODO guess CHARACTER_OCTET_LENGTH from CHARACTER_MAXIMUM_LENGTH
596 $columns[$column_name]['CHARACTER_OCTET_LENGTH'] = null;
597 $columns[$column_name]['NUMERIC_PRECISION'] = null;
598 $columns[$column_name]['NUMERIC_SCALE'] = null;
599 $columns[$column_name]['CHARACTER_SET_NAME'] =
600 substr($columns[$column_name]['COLLATION_NAME'], 0,
601 strpos($columns[$column_name]['COLLATION_NAME'], '_'));
603 $ordinal_position++;
606 if ( null !== $column ) {
607 reset($columns);
608 $columns = current($columns);
612 return $columns;
616 * @TODO should only return columns names, for more info use PMA_DBI_get_columns_full()
618 * @deprecated by PMA_DBI_get_columns() or PMA_DBI_get_columns_full()
619 * @param string $database name of database
620 * @param string $table name of table to retrieve columns from
621 * @param mixed $link mysql link resource
622 * @return array column info
624 function PMA_DBI_get_fields($database, $table, $link = null)
626 // here we use a try_query because when coming from
627 // tbl_create + tbl_properties.inc.php, the table does not exist
628 $fields = PMA_DBI_fetch_result(
629 'SHOW FULL COLUMNS
630 FROM ' . PMA_backquote($database) . '.' . PMA_backquote($table),
631 null, null, $link);
632 if ( ! is_array($fields) || count($fields) < 1 ) {
633 return false;
635 return $fields;
639 * array PMA_DBI_get_columns(string $database, string $table, bool $full = false, mysql db link $link = null)
641 * @param string $database name of database
642 * @param string $table name of table to retrieve columns from
643 * @param boolean $full wether to return full info or only column names
644 * @param mixed $link mysql link resource
645 * @return array column names
647 function PMA_DBI_get_columns($database, $table, $full = false, $link = null)
649 $fields = PMA_DBI_fetch_result(
650 'SHOW ' . ($full ? 'FULL' : '') . ' COLUMNS
651 FROM ' . PMA_backquote($database) . '.' . PMA_backquote($table),
652 'Fields', ($full ? null : 'Fields'), $link);
653 if ( ! is_array($fields) || count($fields) < 1 ) {
654 return false;
656 return $fields;
660 * returns value of given mysql server variable
662 * @param string $var mysql server variable name
663 * @param int $type PMA_DBI_GETVAR_SESSION|PMA_DBI_GETVAR_GLOBAL
664 * @param mixed $link mysql link resource|object
665 * @return mixed value for mysql server variable
667 function PMA_DBI_get_variable($var, $type = PMA_DBI_GETVAR_SESSION, $link = null)
669 if ($link === null) {
670 if (isset($GLOBALS['userlink'])) {
671 $link = $GLOBALS['userlink'];
672 } else {
673 return false;
676 if (PMA_MYSQL_INT_VERSION < 40002) {
677 $type = 0;
679 switch ($type) {
680 case PMA_DBI_GETVAR_SESSION:
681 $modifier = ' SESSION';
682 break;
683 case PMA_DBI_GETVAR_GLOBAL:
684 $modifier = ' GLOBAL';
685 break;
686 default:
687 $modifier = '';
689 return PMA_DBI_fetch_value(
690 'SHOW' . $modifier . ' VARIABLES LIKE \'' . $var . '\';', 0, 1, $link);
694 * @uses ./libraries/charset_conversion.lib.php
695 * @uses PMA_DBI_QUERY_STORE
696 * @uses PMA_REMOVED_NON_UTF_8
697 * @uses PMA_MYSQL_INT_VERSION
698 * @uses PMA_MYSQL_STR_VERSION
699 * @uses PMA_DBI_GETVAR_SESSION
700 * @uses PMA_DBI_fetch_value()
701 * @uses PMA_DBI_query()
702 * @uses PMA_DBI_get_variable()
703 * @uses $GLOBALS['collation_connection']
704 * @uses $GLOBALS['charset_connection']
705 * @uses $GLOBALS['available_languages']
706 * @uses $GLOBALS['mysql_charset_map']
707 * @uses $GLOBALS['charset']
708 * @uses $GLOBALS['lang']
709 * @uses $GLOBALS['cfg']['Lang']
710 * @uses $GLOBALS['cfg']['ColumnTypes']
711 * @uses defined()
712 * @uses explode()
713 * @uses sprintf()
714 * @uses intval()
715 * @uses define()
716 * @uses defined()
717 * @uses substr()
718 * @uses count()
719 * @param mixed $link mysql link resource|object
720 * @param boolean $is_controluser
722 function PMA_DBI_postConnect($link, $is_controluser = false)
724 if (!defined('PMA_MYSQL_INT_VERSION')) {
725 $mysql_version = PMA_DBI_fetch_value(
726 'SELECT VERSION()', 0, 0, $link, PMA_DBI_QUERY_STORE);
727 if ( $mysql_version ) {
728 $match = explode('.', $mysql_version);
729 define('PMA_MYSQL_INT_VERSION',
730 (int) sprintf('%d%02d%02d', $match[0], $match[1],
731 intval($match[2])));
732 define('PMA_MYSQL_STR_VERSION', $mysql_version);
733 unset($mysql_version, $match);
734 } else {
735 define('PMA_MYSQL_INT_VERSION', 32332);
736 define('PMA_MYSQL_STR_VERSION', '3.23.32');
740 if (!defined('PMA_ENGINE_KEYWORD')) {
741 if (PMA_MYSQL_INT_VERSION >= 40102) {
742 define('PMA_ENGINE_KEYWORD','ENGINE');
743 } else {
744 define('PMA_ENGINE_KEYWORD','TYPE');
748 if (PMA_MYSQL_INT_VERSION >= 40100) {
750 // If $lang is defined and we are on MySQL >= 4.1.x,
751 // we auto-switch the lang to its UTF-8 version (if it exists and user
752 // didn't force language)
753 if ( !empty($GLOBALS['lang'])
754 && (substr($GLOBALS['lang'], -5) != 'utf-8')
755 && !isset($GLOBALS['cfg']['Lang']) ) {
756 $lang_utf_8_version =
757 substr($GLOBALS['lang'], 0, strpos($GLOBALS['lang'], '-'))
758 . '-utf-8';
759 if (!empty($GLOBALS['available_languages'][$lang_utf_8_version])) {
760 $GLOBALS['lang'] = $lang_utf_8_version;
761 $GLOBALS['charset'] = 'utf-8';
765 // and we remove the non-UTF-8 choices to avoid confusion
766 if (!defined('PMA_REMOVED_NON_UTF_8')) {
767 foreach ( $GLOBALS['available_languages'] as $each_lang => $dummy ) {
768 if ( substr($each_lang, -5) != 'utf-8' ) {
769 unset( $GLOBALS['available_languages'][$each_lang] );
772 define('PMA_REMOVED_NON_UTF_8', 1);
775 $mysql_charset = $GLOBALS['mysql_charset_map'][$GLOBALS['charset']];
776 if ( $is_controluser
777 || empty($GLOBALS['collation_connection'])
778 || (strpos($GLOBALS['collation_connection'], '_')
779 ? substr($GLOBALS['collation_connection'], 0, strpos($GLOBALS['collation_connection'], '_'))
780 : $GLOBALS['collation_connection']) == $mysql_charset) {
782 PMA_DBI_query('SET NAMES ' . $mysql_charset . ';', $link,
783 PMA_DBI_QUERY_STORE);
784 } else {
785 PMA_DBI_query('SET CHARACTER SET ' . $mysql_charset . ';', $link,
786 PMA_DBI_QUERY_STORE);
788 if (!empty($GLOBALS['collation_connection'])) {
789 PMA_DBI_query('SET collation_connection = \'' . $GLOBALS['collation_connection'] . '\';',
790 $link, PMA_DBI_QUERY_STORE);
792 if (!$is_controluser) {
793 $GLOBALS['collation_connection'] = PMA_DBI_get_variable('collation_connection',
794 PMA_DBI_GETVAR_SESSION, $link);
795 $GLOBALS['charset_connection'] = PMA_DBI_get_variable('character_set_connection',
796 PMA_DBI_GETVAR_SESSION, $link);
799 // Add some field types to the list, this needs to be done once per session!
800 if ($GLOBALS['cfg']['ColumnTypes'][count($GLOBALS['cfg']['ColumnTypes']) - 1] != 'VARBINARY') {
801 $GLOBALS['cfg']['ColumnTypes'][] = 'BINARY';
802 $GLOBALS['cfg']['ColumnTypes'][] = 'VARBINARY';
805 } else {
806 require_once('./libraries/charset_conversion.lib.php');
811 * returns a single value from the given result or query,
812 * if the query or the result has more than one row or field
813 * the first field of the first row is returned
815 * <code>
816 * $sql = 'SELECT `name` FROM `user` WHERE `id` = 123';
817 * $user_name = PMA_DBI_fetch_value( $sql );
818 * // produces
819 * // $user_name = 'John Doe'
820 * </code>
822 * @uses is_string()
823 * @uses is_int()
824 * @uses PMA_DBI_try_query()
825 * @uses PMA_DBI_num_rows()
826 * @uses PMA_DBI_fetch_row()
827 * @uses PMA_DBI_fetch_assoc()
828 * @uses PMA_DBI_free_result()
829 * @param string|mysql_result $result query or mysql result
830 * @param integer $row_number row to fetch the value from,
831 * starting at 0, with 0 beeing default
832 * @param integer|string $field field to fetch the value from,
833 * starting at 0, with 0 beeing default
834 * @param resource $link mysql link
835 * @param mixed $options
836 * @return mixed value of first field in first row from result
837 * or false if not found
839 function PMA_DBI_fetch_value( $result, $row_number = 0, $field = 0, $link = null, $options = 0 ) {
840 $value = false;
842 if ( is_string( $result ) ) {
843 $result = PMA_DBI_try_query( $result, $link, $options | PMA_DBI_QUERY_STORE );
846 // return false if result is empty or false
847 // or requested row is larger than rows in result
848 if ( PMA_DBI_num_rows( $result ) < ( $row_number + 1 ) ) {
849 return $value;
852 // if $field is an integer use non associative mysql fetch function
853 if ( is_int( $field ) ) {
854 $fetch_function = 'PMA_DBI_fetch_row';
855 } else {
856 $fetch_function = 'PMA_DBI_fetch_assoc';
859 // get requested row
860 for ( $i = 0; $i <= $row_number; $i++ ) {
861 $row = $fetch_function( $result );
863 PMA_DBI_free_result( $result );
865 // return requested field
866 if ( isset( $row[$field] ) ) {
867 $value = $row[$field];
869 unset( $row );
871 return $value;
875 * returns only the first row from the result
877 * <code>
878 * $sql = 'SELECT * FROM `user` WHERE `id` = 123';
879 * $user = PMA_DBI_fetch_single_row( $sql );
880 * // produces
881 * // $user = array( 'id' => 123, 'name' => 'John Doe' )
882 * </code>
884 * @uses is_string()
885 * @uses PMA_DBI_try_query()
886 * @uses PMA_DBI_num_rows()
887 * @uses PMA_DBI_fetch_row()
888 * @uses PMA_DBI_fetch_assoc()
889 * @uses PMA_DBI_fetch_array()
890 * @uses PMA_DBI_free_result()
891 * @param string|mysql_result $result query or mysql result
892 * @param string $type NUM|ASSOC|BOTH
893 * returned array should either numeric
894 * associativ or booth
895 * @param resource $link mysql link
896 * @param mixed $options
897 * @return array|boolean first row from result
898 * or false if result is empty
900 function PMA_DBI_fetch_single_row( $result, $type = 'ASSOC', $link = null, $options = 0 ) {
901 if ( is_string( $result ) ) {
902 $result = PMA_DBI_try_query( $result, $link, $options | PMA_DBI_QUERY_STORE );
905 // return null if result is empty or false
906 if ( ! PMA_DBI_num_rows( $result ) ) {
907 return false;
910 switch ( $type ) {
911 case 'NUM' :
912 $fetch_function = 'PMA_DBI_fetch_row';
913 break;
914 case 'ASSOC' :
915 $fetch_function = 'PMA_DBI_fetch_assoc';
916 break;
917 case 'BOTH' :
918 default :
919 $fetch_function = 'PMA_DBI_fetch_array';
920 break;
923 $row = $fetch_function( $result );
924 PMA_DBI_free_result( $result );
925 return $row;
929 * returns all rows in the resultset in one array
931 * <code>
932 * $sql = 'SELECT * FROM `user`';
933 * $users = PMA_DBI_fetch_result( $sql );
934 * // produces
935 * // $users[] = array( 'id' => 123, 'name' => 'John Doe' )
937 * $sql = 'SELECT `id`, `name` FROM `user`';
938 * $users = PMA_DBI_fetch_result( $sql, 'id' );
939 * // produces
940 * // $users['123'] = array( 'id' => 123, 'name' => 'John Doe' )
942 * $sql = 'SELECT `id`, `name` FROM `user`';
943 * $users = PMA_DBI_fetch_result( $sql, 0 );
944 * // produces
945 * // $users['123'] = array( 0 => 123, 1 => 'John Doe' )
947 * $sql = 'SELECT `id`, `name` FROM `user`';
948 * $users = PMA_DBI_fetch_result( $sql, 'id', 'name' );
949 * // or
950 * $users = PMA_DBI_fetch_result( $sql, 0, 1 );
951 * // produces
952 * // $users['123'] = 'John Doe'
954 * $sql = 'SELECT `name` FROM `user`';
955 * $users = PMA_DBI_fetch_result( $sql );
956 * // produces
957 * // $users[] = 'John Doe'
958 * </code>
960 * @uses is_string()
961 * @uses is_int()
962 * @uses PMA_DBI_try_query()
963 * @uses PMA_DBI_num_rows()
964 * @uses PMA_DBI_num_fields()
965 * @uses PMA_DBI_fetch_row()
966 * @uses PMA_DBI_fetch_assoc()
967 * @uses PMA_DBI_free_result()
968 * @param string|mysql_result $result query or mysql result
969 * @param string|integer $key field-name or offset
970 * used as key for array
971 * @param string|integer $value value-name or offset
972 * used as value for array
973 * @param resource $link mysql link
974 * @param mixed $options
975 * @return array resultrows or values indexed by $key
977 function PMA_DBI_fetch_result( $result, $key = null, $value = null,
978 $link = null, $options = 0 )
980 $resultrows = array();
982 if ( is_string($result) ) {
983 $result = PMA_DBI_try_query($result, $link, $options);
986 // return empty array if result is empty or false
987 if ( ! $result ) {
988 return $resultrows;
991 $fetch_function = 'PMA_DBI_fetch_assoc';
993 // no nested array if only one field is in result
994 if ( null === $key && 1 === PMA_DBI_num_fields($result) ) {
995 $value = 0;
996 $fetch_function = 'PMA_DBI_fetch_row';
999 // if $key is an integer use non associative mysql fetch function
1000 if ( is_int($key) ) {
1001 $fetch_function = 'PMA_DBI_fetch_row';
1004 if ( null === $key && null === $value ) {
1005 while ( $row = $fetch_function($result) ) {
1006 $resultrows[] = $row;
1008 } elseif ( null === $key ) {
1009 while ( $row = $fetch_function($result) ) {
1010 $resultrows[] = $row[$value];
1012 } elseif ( null === $value ) {
1013 if ( is_array($key) ) {
1014 while ( $row = $fetch_function($result) ) {
1015 $result_target =& $resultrows;
1016 foreach ( $key as $key_index ) {
1017 if ( ! isset( $result_target[$row[$key_index]] ) ) {
1018 $result_target[$row[$key_index]] = array();
1020 $result_target =& $result_target[$row[$key_index]];
1022 $result_target = $row;
1024 } else {
1025 while ( $row = $fetch_function($result) ) {
1026 $resultrows[$row[$key]] = $row;
1029 } else {
1030 if ( is_array($key) ) {
1031 while ( $row = $fetch_function($result) ) {
1032 $result_target =& $resultrows;
1033 foreach ( $key as $key_index ) {
1034 if ( ! isset( $result_target[$row[$key_index]] ) ) {
1035 $result_target[$row[$key_index]] = array();
1037 $result_target =& $result_target[$row[$key_index]];
1039 $result_target = $row[$value];
1041 } else {
1042 while ( $row = $fetch_function($result) ) {
1043 $resultrows[$row[$key]] = $row[$value];
1048 PMA_DBI_free_result($result);
1049 return $resultrows;
1053 * return default table engine for given database
1055 * @return string default table engine
1057 function PMA_DBI_get_default_engine()
1059 if ( PMA_MYSQL_INT_VERSION > 50002 ) {
1060 return PMA_DBI_fetch_value( 'SHOW VARIABLES LIKE \'storage_engine\';', 0, 1 );
1061 } else {
1062 return PMA_DBI_fetch_value( 'SHOW VARIABLES LIKE \'table_type\';', 0, 1 );
1067 * Get supported SQL compatibility modes
1069 * @return array supported SQL compatibility modes
1071 function PMA_DBI_getCompatibilities()
1073 if (PMA_MYSQL_INT_VERSION < 40100) {
1074 return array();
1076 $compats = array('NONE');
1077 if (PMA_MYSQL_INT_VERSION >= 40101) {
1078 $compats[] = 'ANSI';
1079 $compats[] = 'DB2';
1080 $compats[] = 'MAXDB';
1081 $compats[] = 'MYSQL323';
1082 $compats[] = 'MYSQL40';
1083 $compats[] = 'MSSQL';
1084 $compats[] = 'ORACLE';
1085 $compats[] = 'POSTGRESQL';
1086 if (PMA_MYSQL_INT_VERSION >= 50002) {
1087 $compats[] = 'TRADITIONAL';
1090 return $compats;