PMA_DBI_fetch_single_row() shouldn't allow to set the $options argument
[phpmyadmin.git] / libraries / database_interface.lib.php
blob1b10c04513a329a76fdb0653eb02fe976d677ef8
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Common Option Constants For DBI Functions
6 * @package phpMyAdmin
7 */
8 if (! defined('PHPMYADMIN')) {
9 exit;
12 /**
15 // PMA_DBI_try_query()
16 define('PMA_DBI_QUERY_STORE', 1); // Force STORE_RESULT method, ignored by classic MySQL.
17 define('PMA_DBI_QUERY_UNBUFFERED', 2); // Do not read whole query
18 // PMA_DBI_get_variable()
19 define('PMA_DBI_GETVAR_SESSION', 1);
20 define('PMA_DBI_GETVAR_GLOBAL', 2);
22 /**
23 * Checks one of the mysql extensions
25 * @param string $extension mysql extension to check
26 * @return bool
28 function PMA_DBI_checkMysqlExtension($extension = 'mysql')
30 if (! function_exists($extension . '_connect')) {
31 return false;
34 return true;
37 /**
38 * check for requested extension
40 if (! PMA_DBI_checkMysqlExtension($GLOBALS['cfg']['Server']['extension'])) {
42 // if it fails try alternative extension ...
43 // and display an error ...
45 /**
46 * @todo add different messages for alternative extension
47 * and complete fail (no alternative extension too)
49 PMA_warnMissingExtension($GLOBALS['cfg']['Server']['extension'], false, PMA_showDocu('faqmysql'));
51 if ($GLOBALS['cfg']['Server']['extension'] === 'mysql') {
52 $alternativ_extension = 'mysqli';
53 } else {
54 $alternativ_extension = 'mysql';
57 if (! PMA_DBI_checkMysqlExtension($alternativ_extension)) {
58 // if alternative fails too ...
59 PMA_warnMissingExtension($GLOBALS['cfg']['Server']['extension'], true, PMA_showDocu('faqmysql'));
62 $GLOBALS['cfg']['Server']['extension'] = $alternativ_extension;
63 unset($alternativ_extension);
66 /**
67 * Including The DBI Plugin
69 require_once './libraries/dbi/' . $GLOBALS['cfg']['Server']['extension'] . '.dbi.lib.php';
71 /**
72 * runs a query
74 * @param string $query SQL query to execte
75 * @param mixed $link optional database link to use
76 * @param int $options optional query options
77 * @param bool $cache_affected_rows whether to cache affected rows
78 * @return mixed
80 function PMA_DBI_query($query, $link = null, $options = 0, $cache_affected_rows = true)
82 $res = PMA_DBI_try_query($query, $link, $options, $cache_affected_rows)
83 or PMA_mysqlDie(PMA_DBI_getError($link), $query);
84 return $res;
87 /**
88 * runs a query and returns the result
90 * @param string $query query to run
91 * @param resource $link mysql link resource
92 * @param integer $options
93 * @param bool $cache_affected_rows
94 * @return mixed
96 function PMA_DBI_try_query($query, $link = null, $options = 0, $cache_affected_rows = true)
98 if (empty($link)) {
99 if (isset($GLOBALS['userlink'])) {
100 $link = $GLOBALS['userlink'];
101 } else {
102 return false;
106 if ($GLOBALS['cfg']['DBG']['sql']) {
107 $time = microtime(true);
110 $r = PMA_DBI_real_query($query, $link, $options);
112 if ($cache_affected_rows) {
113 $GLOBALS['cached_affected_rows'] = PMA_DBI_affected_rows($link, $get_from_cache = false);
116 if ($GLOBALS['cfg']['DBG']['sql']) {
117 $time = microtime(true) - $time;
119 $hash = md5($query);
121 if (isset($_SESSION['debug']['queries'][$hash])) {
122 $_SESSION['debug']['queries'][$hash]['count']++;
123 } else {
124 $_SESSION['debug']['queries'][$hash] = array();
125 $_SESSION['debug']['queries'][$hash]['count'] = 1;
126 $_SESSION['debug']['queries'][$hash]['query'] = $query;
127 $_SESSION['debug']['queries'][$hash]['time'] = $time;
130 $trace = array();
131 foreach (debug_backtrace() as $trace_step) {
132 $trace[] = PMA_Error::relPath($trace_step['file']) . '#'
133 . $trace_step['line'] . ': '
134 . (isset($trace_step['class']) ? $trace_step['class'] : '')
135 //. (isset($trace_step['object']) ? get_class($trace_step['object']) : '')
136 . (isset($trace_step['type']) ? $trace_step['type'] : '')
137 . (isset($trace_step['function']) ? $trace_step['function'] : '')
138 . '('
139 . (isset($trace_step['params']) ? implode(', ', $trace_step['params']) : '')
140 . ')'
143 $_SESSION['debug']['queries'][$hash]['trace'][] = $trace;
145 if ($r != false && PMA_Tracker::isActive() == true ) {
146 PMA_Tracker::handleQuery($query);
149 return $r;
153 * converts charset of a mysql message, usually coming from mysql_error(),
154 * into PMA charset, usally UTF-8
155 * uses language to charset mapping from mysql/share/errmsg.txt
156 * and charset names to ISO charset from information_schema.CHARACTER_SETS
158 * @param string $message
159 * @return string $message
161 function PMA_DBI_convert_message($message)
163 // latin always last!
164 $encodings = array(
165 'japanese' => 'EUC-JP', //'ujis',
166 'japanese-sjis' => 'Shift-JIS', //'sjis',
167 'korean' => 'EUC-KR', //'euckr',
168 'russian' => 'KOI8-R', //'koi8r',
169 'ukrainian' => 'KOI8-U', //'koi8u',
170 'greek' => 'ISO-8859-7', //'greek',
171 'serbian' => 'CP1250', //'cp1250',
172 'estonian' => 'ISO-8859-13', //'latin7',
173 'slovak' => 'ISO-8859-2', //'latin2',
174 'czech' => 'ISO-8859-2', //'latin2',
175 'hungarian' => 'ISO-8859-2', //'latin2',
176 'polish' => 'ISO-8859-2', //'latin2',
177 'romanian' => 'ISO-8859-2', //'latin2',
178 'spanish' => 'CP1252', //'latin1',
179 'swedish' => 'CP1252', //'latin1',
180 'italian' => 'CP1252', //'latin1',
181 'norwegian-ny' => 'CP1252', //'latin1',
182 'norwegian' => 'CP1252', //'latin1',
183 'portuguese' => 'CP1252', //'latin1',
184 'danish' => 'CP1252', //'latin1',
185 'dutch' => 'CP1252', //'latin1',
186 'english' => 'CP1252', //'latin1',
187 'french' => 'CP1252', //'latin1',
188 'german' => 'CP1252', //'latin1',
191 if ($server_language = PMA_DBI_fetch_value('SHOW VARIABLES LIKE \'language\';', 0, 1)) {
192 $found = array();
193 if (preg_match('&(?:\\\|\\/)([^\\\\\/]*)(?:\\\|\\/)$&i', $server_language, $found)) {
194 $server_language = $found[1];
198 if (! empty($server_language) && isset($encodings[$server_language])) {
199 if (function_exists('iconv')) {
200 if ((@stristr(PHP_OS, 'AIX')) && (@strcasecmp(ICONV_IMPL, 'unknown') == 0) && (@strcasecmp(ICONV_VERSION, 'unknown') == 0)) {
201 require_once './libraries/iconv_wrapper.lib.php';
202 $message = PMA_aix_iconv_wrapper($encodings[$server_language],
203 'utf-8' . $GLOBALS['cfg']['IconvExtraParams'], $message);
204 } else {
205 $message = iconv($encodings[$server_language],
206 'utf-8' . $GLOBALS['cfg']['IconvExtraParams'], $message);
208 } elseif (function_exists('recode_string')) {
209 $message = recode_string($encodings[$server_language] . '..' . 'utf-8',
210 $message);
211 } elseif (function_exists('libiconv')) {
212 $message = libiconv($encodings[$server_language], 'utf-8', $message);
213 } elseif (function_exists('mb_convert_encoding')) {
214 // do not try unsupported charsets
215 if (! in_array($server_language, array('ukrainian', 'greek', 'serbian'))) {
216 $message = mb_convert_encoding($message, 'utf-8',
217 $encodings[$server_language]);
220 } else {
222 * @todo lang not found, try all, what TODO ?
226 return $message;
230 * returns array with table names for given db
232 * @param string $database name of database
233 * @param mixed $link mysql link resource|object
234 * @return array tables names
236 function PMA_DBI_get_tables($database, $link = null)
238 return PMA_DBI_fetch_result('SHOW TABLES FROM ' . PMA_backquote($database) . ';',
239 null, 0, $link, PMA_DBI_QUERY_STORE);
243 * usort comparison callback
245 * @param string $a first argument to sort
246 * @param string $b second argument to sort
248 * @return integer a value representing whether $a should be before $b in the
249 * sorted array or not
251 * @access private
253 function PMA_usort_comparison_callback($a, $b)
255 if ($GLOBALS['cfg']['NaturalOrder']) {
256 $sorter = 'strnatcasecmp';
257 } else {
258 $sorter = 'strcasecmp';
260 /* No sorting when key is not present */
261 if (! isset($a[$GLOBALS['callback_sort_by']]) || ! isset($b[$GLOBALS['callback_sort_by']])) {
262 return 0;
264 // produces f.e.:
265 // return -1 * strnatcasecmp($a["SCHEMA_TABLES"], $b["SCHEMA_TABLES"])
266 return ($GLOBALS['callback_sort_order'] == 'ASC' ? 1 : -1) * $sorter($a[$GLOBALS['callback_sort_by']], $b[$GLOBALS['callback_sort_by']]);
267 } // end of the 'PMA_usort_comparison_callback()' function
270 * returns array of all tables in given db or dbs
271 * this function expects unquoted names:
272 * RIGHT: my_database
273 * WRONG: `my_database`
274 * WRONG: my\_database
275 * if $tbl_is_group is true, $table is used as filter for table names
276 * if $tbl_is_group is 'comment, $table is used as filter for table comments
278 * <code>
279 * PMA_DBI_get_tables_full('my_database');
280 * PMA_DBI_get_tables_full('my_database', 'my_table'));
281 * PMA_DBI_get_tables_full('my_database', 'my_tables_', true));
282 * PMA_DBI_get_tables_full('my_database', 'my_tables_', 'comment'));
283 * </code>
285 * @todo move into PMA_Table
286 * @param string $database database
287 * @param string|bool $table table or false
288 * @param boolean|string $tbl_is_group $table is a table group
289 * @param mixed $link mysql link
290 * @param integer $limit_offset zero-based offset for the count
291 * @param boolean|integer $limit_count number of tables to return
292 * @param string $sort_by table attribute to sort by
293 * @param string $sort_order direction to sort (ASC or DESC)
294 * @return array list of tables in given db(s)
296 function PMA_DBI_get_tables_full($database, $table = false, $tbl_is_group = false, $link = null,
297 $limit_offset = 0, $limit_count = false, $sort_by = 'Name', $sort_order = 'ASC')
299 if (true === $limit_count) {
300 $limit_count = $GLOBALS['cfg']['MaxTableList'];
302 // prepare and check parameters
303 if (! is_array($database)) {
304 $databases = array($database);
305 } else {
306 $databases = $database;
309 $tables = array();
311 if (! $GLOBALS['cfg']['Server']['DisableIS']) {
312 // get table information from information_schema
313 if ($table) {
314 if (true === $tbl_is_group) {
315 $sql_where_table = 'AND `TABLE_NAME` LIKE \''
316 . PMA_escape_mysql_wildcards(PMA_sqlAddSlashes($table)) . '%\'';
317 } elseif ('comment' === $tbl_is_group) {
318 $sql_where_table = 'AND `TABLE_COMMENT` LIKE \''
319 . PMA_escape_mysql_wildcards(PMA_sqlAddSlashes($table)) . '%\'';
320 } else {
321 $sql_where_table = 'AND `TABLE_NAME` = \'' . PMA_sqlAddSlashes($table) . '\'';
323 } else {
324 $sql_where_table = '';
327 // for PMA bc:
328 // `SCHEMA_FIELD_NAME` AS `SHOW_TABLE_STATUS_FIELD_NAME`
330 // on non-Windows servers,
331 // added BINARY in the WHERE clause to force a case sensitive
332 // comparison (if we are looking for the db Aa we don't want
333 // to find the db aa)
334 $this_databases = array_map('PMA_sqlAddSlashes', $databases);
336 $sql = '
337 SELECT *,
338 `TABLE_SCHEMA` AS `Db`,
339 `TABLE_NAME` AS `Name`,
340 `TABLE_TYPE` AS `TABLE_TYPE`,
341 `ENGINE` AS `Engine`,
342 `ENGINE` AS `Type`,
343 `VERSION` AS `Version`,
344 `ROW_FORMAT` AS `Row_format`,
345 `TABLE_ROWS` AS `Rows`,
346 `AVG_ROW_LENGTH` AS `Avg_row_length`,
347 `DATA_LENGTH` AS `Data_length`,
348 `MAX_DATA_LENGTH` AS `Max_data_length`,
349 `INDEX_LENGTH` AS `Index_length`,
350 `DATA_FREE` AS `Data_free`,
351 `AUTO_INCREMENT` AS `Auto_increment`,
352 `CREATE_TIME` AS `Create_time`,
353 `UPDATE_TIME` AS `Update_time`,
354 `CHECK_TIME` AS `Check_time`,
355 `TABLE_COLLATION` AS `Collation`,
356 `CHECKSUM` AS `Checksum`,
357 `CREATE_OPTIONS` AS `Create_options`,
358 `TABLE_COMMENT` AS `Comment`
359 FROM `information_schema`.`TABLES`
360 WHERE ' . (PMA_IS_WINDOWS ? '' : 'BINARY') . ' `TABLE_SCHEMA` IN (\'' . implode("', '", $this_databases) . '\')
361 ' . $sql_where_table;
363 // Sort the tables
364 $sql .= " ORDER BY $sort_by $sort_order";
366 if ($limit_count) {
367 $sql .= ' LIMIT ' . $limit_count . ' OFFSET ' . $limit_offset;
370 $tables = PMA_DBI_fetch_result($sql, array('TABLE_SCHEMA', 'TABLE_NAME'),
371 null, $link);
372 unset($sql_where_table, $sql);
373 if ($sort_by == 'Name' && $GLOBALS['cfg']['NaturalOrder']) {
374 // here, the array's first key is by schema name
375 foreach ($tables as $one_database_name => $one_database_tables) {
376 uksort($one_database_tables, 'strnatcasecmp');
378 if ($sort_order == 'DESC') {
379 $one_database_tables = array_reverse($one_database_tables);
381 $tables[$one_database_name] = $one_database_tables;
384 } // end (get information from table schema)
386 // If permissions are wrong on even one database directory,
387 // information_schema does not return any table info for any database
388 // this is why we fall back to SHOW TABLE STATUS even for MySQL >= 50002
389 if (empty($tables)) {
390 foreach ($databases as $each_database) {
391 if ($table || (true === $tbl_is_group)) {
392 $sql = 'SHOW TABLE STATUS FROM '
393 . PMA_backquote($each_database)
394 .' LIKE \'' . PMA_escape_mysql_wildcards(PMA_sqlAddSlashes($table, true)) . '%\'';
395 } else {
396 $sql = 'SHOW TABLE STATUS FROM '
397 . PMA_backquote($each_database);
400 $each_tables = PMA_DBI_fetch_result($sql, 'Name', null, $link);
402 // Sort naturally if the config allows it and we're sorting
403 // the Name column.
404 if ($sort_by == 'Name' && $GLOBALS['cfg']['NaturalOrder']) {
405 uksort($each_tables, 'strnatcasecmp');
407 if ($sort_order == 'DESC') {
408 $each_tables = array_reverse($each_tables);
410 } else {
411 // Prepare to sort by creating array of the selected sort
412 // value to pass to array_multisort
414 // Size = Data_length + Index_length
415 if ($sort_by == 'Data_length') {
416 foreach ($each_tables as $table_name => $table_data) {
417 ${$sort_by}[$table_name] = strtolower($table_data['Data_length'] + $table_data['Index_length']);
419 } else {
420 foreach ($each_tables as $table_name => $table_data) {
421 ${$sort_by}[$table_name] = strtolower($table_data[$sort_by]);
425 if ($sort_order == 'DESC') {
426 array_multisort($$sort_by, SORT_DESC, $each_tables);
427 } else {
428 array_multisort($$sort_by, SORT_ASC, $each_tables);
431 // cleanup the temporary sort array
432 unset($$sort_by);
435 if ($limit_count) {
436 $each_tables = array_slice($each_tables, $limit_offset, $limit_count);
439 foreach ($each_tables as $table_name => $each_table) {
440 if ('comment' === $tbl_is_group
441 && 0 === strpos($each_table['Comment'], $table))
443 // remove table from list
444 unset($each_tables[$table_name]);
445 continue;
448 if (! isset($each_tables[$table_name]['Type'])
449 && isset($each_tables[$table_name]['Engine'])) {
450 // pma BC, same parts of PMA still uses 'Type'
451 $each_tables[$table_name]['Type']
452 =& $each_tables[$table_name]['Engine'];
453 } elseif (! isset($each_tables[$table_name]['Engine'])
454 && isset($each_tables[$table_name]['Type'])) {
455 // old MySQL reports Type, newer MySQL reports Engine
456 $each_tables[$table_name]['Engine']
457 =& $each_tables[$table_name]['Type'];
460 // MySQL forward compatibility
461 // so pma could use this array as if every server is of version >5.0
462 $each_tables[$table_name]['TABLE_SCHEMA'] = $each_database;
463 $each_tables[$table_name]['TABLE_NAME'] =& $each_tables[$table_name]['Name'];
464 $each_tables[$table_name]['ENGINE'] =& $each_tables[$table_name]['Engine'];
465 $each_tables[$table_name]['VERSION'] =& $each_tables[$table_name]['Version'];
466 $each_tables[$table_name]['ROW_FORMAT'] =& $each_tables[$table_name]['Row_format'];
467 $each_tables[$table_name]['TABLE_ROWS'] =& $each_tables[$table_name]['Rows'];
468 $each_tables[$table_name]['AVG_ROW_LENGTH'] =& $each_tables[$table_name]['Avg_row_length'];
469 $each_tables[$table_name]['DATA_LENGTH'] =& $each_tables[$table_name]['Data_length'];
470 $each_tables[$table_name]['MAX_DATA_LENGTH'] =& $each_tables[$table_name]['Max_data_length'];
471 $each_tables[$table_name]['INDEX_LENGTH'] =& $each_tables[$table_name]['Index_length'];
472 $each_tables[$table_name]['DATA_FREE'] =& $each_tables[$table_name]['Data_free'];
473 $each_tables[$table_name]['AUTO_INCREMENT'] =& $each_tables[$table_name]['Auto_increment'];
474 $each_tables[$table_name]['CREATE_TIME'] =& $each_tables[$table_name]['Create_time'];
475 $each_tables[$table_name]['UPDATE_TIME'] =& $each_tables[$table_name]['Update_time'];
476 $each_tables[$table_name]['CHECK_TIME'] =& $each_tables[$table_name]['Check_time'];
477 $each_tables[$table_name]['TABLE_COLLATION'] =& $each_tables[$table_name]['Collation'];
478 $each_tables[$table_name]['CHECKSUM'] =& $each_tables[$table_name]['Checksum'];
479 $each_tables[$table_name]['CREATE_OPTIONS'] =& $each_tables[$table_name]['Create_options'];
480 $each_tables[$table_name]['TABLE_COMMENT'] =& $each_tables[$table_name]['Comment'];
482 if (strtoupper($each_tables[$table_name]['Comment']) === 'VIEW'
483 && $each_tables[$table_name]['Engine'] == null) {
484 $each_tables[$table_name]['TABLE_TYPE'] = 'VIEW';
485 } else {
487 * @todo difference between 'TEMPORARY' and 'BASE TABLE' but how to detect?
489 $each_tables[$table_name]['TABLE_TYPE'] = 'BASE TABLE';
493 $tables[$each_database] = $each_tables;
497 // cache table data
498 // so PMA_Table does not require to issue SHOW TABLE STATUS again
499 // Note: I don't see why we would need array_merge_recursive() here,
500 // as it creates double entries for the same table (for example a double
501 // entry for Comment when changing the storage engine in Operations)
502 // Note 2: Instead of array_merge(), simply use the + operator because
503 // array_merge() renumbers numeric keys starting with 0, therefore
504 // we would lose a db name thats consists only of numbers
505 foreach ($tables as $one_database => $its_tables) {
506 if (isset(PMA_Table::$cache[$one_database])) {
507 PMA_Table::$cache[$one_database] = PMA_Table::$cache[$one_database] + $tables[$one_database];
508 } else {
509 PMA_Table::$cache[$one_database] = $tables[$one_database];
512 unset($one_database, $its_tables);
514 if (! is_array($database)) {
515 if (isset($tables[$database])) {
516 return $tables[$database];
517 } elseif (isset($tables[strtolower($database)])) {
518 // on windows with lower_case_table_names = 1
519 // MySQL returns
520 // with SHOW DATABASES or information_schema.SCHEMATA: `Test`
521 // but information_schema.TABLES gives `test`
522 // bug #1436171
523 // http://sf.net/support/tracker.php?aid=1436171
524 return $tables[strtolower($database)];
525 } else {
526 return $tables;
528 } else {
529 return $tables;
534 * returns array with databases containing extended infos about them
536 * @todo move into PMA_List_Database?
537 * @param string $database database
538 * @param boolean $force_stats retrieve stats also for MySQL < 5
539 * @param resource $link mysql link
540 * @param string $sort_by column to order by
541 * @param string $sort_order ASC or DESC
542 * @param integer $limit_offset starting offset for LIMIT
543 * @param bool|int $limit_count row count for LIMIT or true for $GLOBALS['cfg']['MaxDbList']
544 * @return array $databases
546 function PMA_DBI_get_databases_full($database = null, $force_stats = false,
547 $link = null, $sort_by = 'SCHEMA_NAME', $sort_order = 'ASC',
548 $limit_offset = 0, $limit_count = false)
550 $sort_order = strtoupper($sort_order);
552 if (true === $limit_count) {
553 $limit_count = $GLOBALS['cfg']['MaxDbList'];
556 // initialize to avoid errors when there are no databases
557 $databases = array();
559 $apply_limit_and_order_manual = true;
561 if (! $GLOBALS['cfg']['Server']['DisableIS']) {
563 * if $GLOBALS['cfg']['NaturalOrder'] is enabled, we cannot use LIMIT
564 * cause MySQL does not support natural ordering, we have to do it afterward
566 $limit = '';
567 if (!$GLOBALS['cfg']['NaturalOrder']) {
568 if ($limit_count) {
569 $limit = ' LIMIT ' . $limit_count . ' OFFSET ' . $limit_offset;
572 $apply_limit_and_order_manual = false;
575 // get table information from information_schema
576 if ($database) {
577 $sql_where_schema = 'WHERE `SCHEMA_NAME` LIKE \''
578 . PMA_sqlAddSlashes($database) . '\'';
579 } else {
580 $sql_where_schema = '';
583 // for PMA bc:
584 // `SCHEMA_FIELD_NAME` AS `SHOW_TABLE_STATUS_FIELD_NAME`
585 $sql = '
586 SELECT `information_schema`.`SCHEMATA`.*';
587 if ($force_stats) {
588 $sql .= ',
589 COUNT(`information_schema`.`TABLES`.`TABLE_SCHEMA`)
590 AS `SCHEMA_TABLES`,
591 SUM(`information_schema`.`TABLES`.`TABLE_ROWS`)
592 AS `SCHEMA_TABLE_ROWS`,
593 SUM(`information_schema`.`TABLES`.`DATA_LENGTH`)
594 AS `SCHEMA_DATA_LENGTH`,
595 SUM(`information_schema`.`TABLES`.`MAX_DATA_LENGTH`)
596 AS `SCHEMA_MAX_DATA_LENGTH`,
597 SUM(`information_schema`.`TABLES`.`INDEX_LENGTH`)
598 AS `SCHEMA_INDEX_LENGTH`,
599 SUM(`information_schema`.`TABLES`.`DATA_LENGTH`
600 + `information_schema`.`TABLES`.`INDEX_LENGTH`)
601 AS `SCHEMA_LENGTH`,
602 SUM(`information_schema`.`TABLES`.`DATA_FREE`)
603 AS `SCHEMA_DATA_FREE`';
605 $sql .= '
606 FROM `information_schema`.`SCHEMATA`';
607 if ($force_stats) {
608 $sql .= '
609 LEFT JOIN `information_schema`.`TABLES`
610 ON BINARY `information_schema`.`TABLES`.`TABLE_SCHEMA`
611 = BINARY `information_schema`.`SCHEMATA`.`SCHEMA_NAME`';
613 $sql .= '
614 ' . $sql_where_schema . '
615 GROUP BY BINARY `information_schema`.`SCHEMATA`.`SCHEMA_NAME`
616 ORDER BY BINARY ' . PMA_backquote($sort_by) . ' ' . $sort_order
617 . $limit;
618 $databases = PMA_DBI_fetch_result($sql, 'SCHEMA_NAME', null, $link);
620 $mysql_error = PMA_DBI_getError($link);
621 if (! count($databases) && $GLOBALS['errno']) {
622 PMA_mysqlDie($mysql_error, $sql);
625 // display only databases also in official database list
626 // f.e. to apply hide_db and only_db
627 $drops = array_diff(array_keys($databases), (array) $GLOBALS['pma']->databases);
628 if (count($drops)) {
629 foreach ($drops as $drop) {
630 unset($databases[$drop]);
632 unset($drop);
634 unset($sql_where_schema, $sql, $drops);
635 } else {
636 foreach ($GLOBALS['pma']->databases as $database_name) {
637 // MySQL forward compatibility
638 // so pma could use this array as if every server is of version >5.0
639 $databases[$database_name]['SCHEMA_NAME'] = $database_name;
641 if ($force_stats) {
642 require_once './libraries/mysql_charsets.lib.php';
644 $databases[$database_name]['DEFAULT_COLLATION_NAME']
645 = PMA_getDbCollation($database_name);
647 // get additional info about tables
648 $databases[$database_name]['SCHEMA_TABLES'] = 0;
649 $databases[$database_name]['SCHEMA_TABLE_ROWS'] = 0;
650 $databases[$database_name]['SCHEMA_DATA_LENGTH'] = 0;
651 $databases[$database_name]['SCHEMA_MAX_DATA_LENGTH'] = 0;
652 $databases[$database_name]['SCHEMA_INDEX_LENGTH'] = 0;
653 $databases[$database_name]['SCHEMA_LENGTH'] = 0;
654 $databases[$database_name]['SCHEMA_DATA_FREE'] = 0;
656 $res = PMA_DBI_query('SHOW TABLE STATUS FROM ' . PMA_backquote($database_name) . ';');
657 while ($row = PMA_DBI_fetch_assoc($res)) {
658 $databases[$database_name]['SCHEMA_TABLES']++;
659 $databases[$database_name]['SCHEMA_TABLE_ROWS']
660 += $row['Rows'];
661 $databases[$database_name]['SCHEMA_DATA_LENGTH']
662 += $row['Data_length'];
663 $databases[$database_name]['SCHEMA_MAX_DATA_LENGTH']
664 += $row['Max_data_length'];
665 $databases[$database_name]['SCHEMA_INDEX_LENGTH']
666 += $row['Index_length'];
668 // for InnoDB, this does not contain the number of
669 // overhead bytes but the total free space
670 if ('InnoDB' != $row['Engine']) {
671 $databases[$database_name]['SCHEMA_DATA_FREE']
672 += $row['Data_free'];
674 $databases[$database_name]['SCHEMA_LENGTH']
675 += $row['Data_length'] + $row['Index_length'];
677 PMA_DBI_free_result($res);
678 unset($res);
685 * apply limit and order manually now
686 * (caused by older MySQL < 5 or $GLOBALS['cfg']['NaturalOrder'])
688 if ($apply_limit_and_order_manual) {
689 $GLOBALS['callback_sort_order'] = $sort_order;
690 $GLOBALS['callback_sort_by'] = $sort_by;
691 usort($databases, 'PMA_usort_comparison_callback');
692 unset($GLOBALS['callback_sort_order'], $GLOBALS['callback_sort_by']);
695 * now apply limit
697 if ($limit_count) {
698 $databases = array_slice($databases, $limit_offset, $limit_count);
702 return $databases;
706 * returns detailed array with all columns for given table in database,
707 * or all tables/databases
709 * @param string $database name of database
710 * @param string $table name of table to retrieve columns from
711 * @param string $column name of specific column
712 * @param mixed $link mysql link resource
713 * @return array
715 function PMA_DBI_get_columns_full($database = null, $table = null,
716 $column = null, $link = null)
718 $columns = array();
720 if (! $GLOBALS['cfg']['Server']['DisableIS']) {
721 $sql_wheres = array();
722 $array_keys = array();
724 // get columns information from information_schema
725 if (null !== $database) {
726 $sql_wheres[] = '`TABLE_SCHEMA` = \'' . PMA_sqlAddSlashes($database) . '\' ';
727 } else {
728 $array_keys[] = 'TABLE_SCHEMA';
730 if (null !== $table) {
731 $sql_wheres[] = '`TABLE_NAME` = \'' . PMA_sqlAddSlashes($table) . '\' ';
732 } else {
733 $array_keys[] = 'TABLE_NAME';
735 if (null !== $column) {
736 $sql_wheres[] = '`COLUMN_NAME` = \'' . PMA_sqlAddSlashes($column) . '\' ';
737 } else {
738 $array_keys[] = 'COLUMN_NAME';
741 // for PMA bc:
742 // `[SCHEMA_FIELD_NAME]` AS `[SHOW_FULL_COLUMNS_FIELD_NAME]`
743 $sql = '
744 SELECT *,
745 `COLUMN_NAME` AS `Field`,
746 `COLUMN_TYPE` AS `Type`,
747 `COLLATION_NAME` AS `Collation`,
748 `IS_NULLABLE` AS `Null`,
749 `COLUMN_KEY` AS `Key`,
750 `COLUMN_DEFAULT` AS `Default`,
751 `EXTRA` AS `Extra`,
752 `PRIVILEGES` AS `Privileges`,
753 `COLUMN_COMMENT` AS `Comment`
754 FROM `information_schema`.`COLUMNS`';
755 if (count($sql_wheres)) {
756 $sql .= "\n" . ' WHERE ' . implode(' AND ', $sql_wheres);
759 $columns = PMA_DBI_fetch_result($sql, $array_keys, null, $link);
760 unset($sql_wheres, $sql);
761 } else {
762 if (null === $database) {
763 foreach ($GLOBALS['pma']->databases as $database) {
764 $columns[$database] = PMA_DBI_get_columns_full($database, null,
765 null, $link);
767 return $columns;
768 } elseif (null === $table) {
769 $tables = PMA_DBI_get_tables($database);
770 foreach ($tables as $table) {
771 $columns[$table] = PMA_DBI_get_columns_full(
772 $database, $table, null, $link);
774 return $columns;
777 $sql = 'SHOW FULL COLUMNS FROM '
778 . PMA_backquote($database) . '.' . PMA_backquote($table);
779 if (null !== $column) {
780 $sql .= " LIKE '" . PMA_sqlAddSlashes($column, true) . "'";
783 $columns = PMA_DBI_fetch_result($sql, 'Field', null, $link);
785 $ordinal_position = 1;
786 foreach ($columns as $column_name => $each_column) {
788 // MySQL forward compatibility
789 // so pma could use this array as if every server is of version >5.0
790 $columns[$column_name]['COLUMN_NAME'] =& $columns[$column_name]['Field'];
791 $columns[$column_name]['COLUMN_TYPE'] =& $columns[$column_name]['Type'];
792 $columns[$column_name]['COLLATION_NAME'] =& $columns[$column_name]['Collation'];
793 $columns[$column_name]['IS_NULLABLE'] =& $columns[$column_name]['Null'];
794 $columns[$column_name]['COLUMN_KEY'] =& $columns[$column_name]['Key'];
795 $columns[$column_name]['COLUMN_DEFAULT'] =& $columns[$column_name]['Default'];
796 $columns[$column_name]['EXTRA'] =& $columns[$column_name]['Extra'];
797 $columns[$column_name]['PRIVILEGES'] =& $columns[$column_name]['Privileges'];
798 $columns[$column_name]['COLUMN_COMMENT'] =& $columns[$column_name]['Comment'];
800 $columns[$column_name]['TABLE_CATALOG'] = null;
801 $columns[$column_name]['TABLE_SCHEMA'] = $database;
802 $columns[$column_name]['TABLE_NAME'] = $table;
803 $columns[$column_name]['ORDINAL_POSITION'] = $ordinal_position;
804 $columns[$column_name]['DATA_TYPE'] =
805 substr($columns[$column_name]['COLUMN_TYPE'], 0,
806 strpos($columns[$column_name]['COLUMN_TYPE'], '('));
808 * @todo guess CHARACTER_MAXIMUM_LENGTH from COLUMN_TYPE
810 $columns[$column_name]['CHARACTER_MAXIMUM_LENGTH'] = null;
812 * @todo guess CHARACTER_OCTET_LENGTH from CHARACTER_MAXIMUM_LENGTH
814 $columns[$column_name]['CHARACTER_OCTET_LENGTH'] = null;
815 $columns[$column_name]['NUMERIC_PRECISION'] = null;
816 $columns[$column_name]['NUMERIC_SCALE'] = null;
817 $columns[$column_name]['CHARACTER_SET_NAME'] =
818 substr($columns[$column_name]['COLLATION_NAME'], 0,
819 strpos($columns[$column_name]['COLLATION_NAME'], '_'));
821 $ordinal_position++;
824 if (null !== $column) {
825 reset($columns);
826 $columns = current($columns);
830 return $columns;
834 * array PMA_DBI_get_columns(string $database, string $table, bool $full = false, mysql db link $link = null)
836 * @param string $database name of database
837 * @param string $table name of table to retrieve columns from
838 * @param boolean $full whether to return full info or only column names
839 * @param mixed $link mysql link resource
840 * @return array column names
842 function PMA_DBI_get_columns($database, $table, $full = false, $link = null)
844 $fields = PMA_DBI_fetch_result(
845 'SHOW ' . ($full ? 'FULL' : '') . ' COLUMNS
846 FROM ' . PMA_backquote($database) . '.' . PMA_backquote($table),
847 'Field', null, $link);
848 if (! is_array($fields) || count($fields) == 0) {
849 return null;
851 return $fields;
855 * returns value of given mysql server variable
857 * @param string $var mysql server variable name
858 * @param int $type PMA_DBI_GETVAR_SESSION|PMA_DBI_GETVAR_GLOBAL
859 * @param mixed $link mysql link resource|object
860 * @return mixed value for mysql server variable
862 function PMA_DBI_get_variable($var, $type = PMA_DBI_GETVAR_SESSION, $link = null)
864 if ($link === null) {
865 if (isset($GLOBALS['userlink'])) {
866 $link = $GLOBALS['userlink'];
867 } else {
868 return false;
872 switch ($type) {
873 case PMA_DBI_GETVAR_SESSION:
874 $modifier = ' SESSION';
875 break;
876 case PMA_DBI_GETVAR_GLOBAL:
877 $modifier = ' GLOBAL';
878 break;
879 default:
880 $modifier = '';
882 return PMA_DBI_fetch_value(
883 'SHOW' . $modifier . ' VARIABLES LIKE \'' . $var . '\';', 0, 1, $link);
887 * Function called just after a connection to the MySQL database server has been established
888 * It sets the connection collation, and determins the version of MySQL which is running.
890 * @param mixed $link mysql link resource|object
891 * @param boolean $is_controluser
893 function PMA_DBI_postConnect($link, $is_controluser = false)
895 if (! defined('PMA_MYSQL_INT_VERSION')) {
896 if (PMA_cacheExists('PMA_MYSQL_INT_VERSION', true)) {
897 define('PMA_MYSQL_INT_VERSION', PMA_cacheGet('PMA_MYSQL_INT_VERSION', true));
898 define('PMA_MYSQL_MAJOR_VERSION', PMA_cacheGet('PMA_MYSQL_MAJOR_VERSION', true));
899 define('PMA_MYSQL_STR_VERSION', PMA_cacheGet('PMA_MYSQL_STR_VERSION', true));
900 } else {
901 $mysql_version = PMA_DBI_fetch_value(
902 'SELECT VERSION()', 0, 0, $link);
903 if ($mysql_version) {
904 $match = explode('.', $mysql_version);
905 define('PMA_MYSQL_MAJOR_VERSION', (int)$match[0]);
906 define('PMA_MYSQL_INT_VERSION',
907 (int) sprintf('%d%02d%02d', $match[0], $match[1],
908 intval($match[2])));
909 define('PMA_MYSQL_STR_VERSION', $mysql_version);
910 unset($mysql_version, $match);
911 } else {
912 define('PMA_MYSQL_INT_VERSION', 50015);
913 define('PMA_MYSQL_MAJOR_VERSION', 5);
914 define('PMA_MYSQL_STR_VERSION', '5.00.15');
916 PMA_cacheSet('PMA_MYSQL_INT_VERSION', PMA_MYSQL_INT_VERSION, true);
917 PMA_cacheSet('PMA_MYSQL_MAJOR_VERSION', PMA_MYSQL_MAJOR_VERSION, true);
918 PMA_cacheSet('PMA_MYSQL_STR_VERSION', PMA_MYSQL_STR_VERSION, true);
920 // detect Drizzle by version number (year.month.day.?patch-version)
921 define('PMA_DRIZZLE', PMA_MYSQL_MAJOR_VERSION >= 2009);
924 // Skip charsets for Drizzle
925 if (!PMA_DRIZZLE) {
926 if (! empty($GLOBALS['collation_connection'])) {
927 PMA_DBI_query("SET CHARACTER SET 'utf8';", $link, PMA_DBI_QUERY_STORE);
928 PMA_DBI_query("SET collation_connection = '" . PMA_sqlAddSlashes($GLOBALS['collation_connection']) . "';", $link, PMA_DBI_QUERY_STORE);
929 } else {
930 PMA_DBI_query("SET NAMES 'utf8' COLLATE 'utf8_general_ci';", $link, PMA_DBI_QUERY_STORE);
936 * returns a single value from the given result or query,
937 * if the query or the result has more than one row or field
938 * the first field of the first row is returned
940 * <code>
941 * $sql = 'SELECT `name` FROM `user` WHERE `id` = 123';
942 * $user_name = PMA_DBI_fetch_value($sql);
943 * // produces
944 * // $user_name = 'John Doe'
945 * </code>
947 * @param string|mysql_result $result query or mysql result
948 * @param integer $row_number row to fetch the value from,
949 * starting at 0, with 0 beeing default
950 * @param integer|string $field field to fetch the value from,
951 * starting at 0, with 0 beeing default
952 * @param resource $link mysql link
953 * @return mixed value of first field in first row from result
954 * or false if not found
956 function PMA_DBI_fetch_value($result, $row_number = 0, $field = 0, $link = null)
958 $value = false;
960 if (is_string($result)) {
961 $result = PMA_DBI_try_query($result, $link, PMA_DBI_QUERY_STORE, false);
964 // return false if result is empty or false
965 // or requested row is larger than rows in result
966 if (PMA_DBI_num_rows($result) < ($row_number + 1)) {
967 return $value;
970 // if $field is an integer use non associative mysql fetch function
971 if (is_int($field)) {
972 $fetch_function = 'PMA_DBI_fetch_row';
973 } else {
974 $fetch_function = 'PMA_DBI_fetch_assoc';
977 // get requested row
978 for ($i = 0; $i <= $row_number; $i++) {
979 $row = $fetch_function($result);
981 PMA_DBI_free_result($result);
983 // return requested field
984 if (isset($row[$field])) {
985 $value = $row[$field];
987 unset($row);
989 return $value;
993 * returns only the first row from the result
995 * <code>
996 * $sql = 'SELECT * FROM `user` WHERE `id` = 123';
997 * $user = PMA_DBI_fetch_single_row($sql);
998 * // produces
999 * // $user = array('id' => 123, 'name' => 'John Doe')
1000 * </code>
1002 * @param string|mysql_result $result query or mysql result
1003 * @param string $type NUM|ASSOC|BOTH
1004 * returned array should either numeric
1005 * associativ or booth
1006 * @param resource $link mysql link
1007 * @return array|boolean first row from result
1008 * or false if result is empty
1010 function PMA_DBI_fetch_single_row($result, $type = 'ASSOC', $link = null)
1012 if (is_string($result)) {
1013 $result = PMA_DBI_try_query($result, $link, PMA_DBI_QUERY_STORE, false);
1016 // return null if result is empty or false
1017 if (! PMA_DBI_num_rows($result)) {
1018 return false;
1021 switch ($type) {
1022 case 'NUM' :
1023 $fetch_function = 'PMA_DBI_fetch_row';
1024 break;
1025 case 'ASSOC' :
1026 $fetch_function = 'PMA_DBI_fetch_assoc';
1027 break;
1028 case 'BOTH' :
1029 default :
1030 $fetch_function = 'PMA_DBI_fetch_array';
1031 break;
1034 $row = $fetch_function($result);
1035 PMA_DBI_free_result($result);
1036 return $row;
1040 * returns all rows in the resultset in one array
1042 * <code>
1043 * $sql = 'SELECT * FROM `user`';
1044 * $users = PMA_DBI_fetch_result($sql);
1045 * // produces
1046 * // $users[] = array('id' => 123, 'name' => 'John Doe')
1048 * $sql = 'SELECT `id`, `name` FROM `user`';
1049 * $users = PMA_DBI_fetch_result($sql, 'id');
1050 * // produces
1051 * // $users['123'] = array('id' => 123, 'name' => 'John Doe')
1053 * $sql = 'SELECT `id`, `name` FROM `user`';
1054 * $users = PMA_DBI_fetch_result($sql, 0);
1055 * // produces
1056 * // $users['123'] = array(0 => 123, 1 => 'John Doe')
1058 * $sql = 'SELECT `id`, `name` FROM `user`';
1059 * $users = PMA_DBI_fetch_result($sql, 'id', 'name');
1060 * // or
1061 * $users = PMA_DBI_fetch_result($sql, 0, 1);
1062 * // produces
1063 * // $users['123'] = 'John Doe'
1065 * $sql = 'SELECT `name` FROM `user`';
1066 * $users = PMA_DBI_fetch_result($sql);
1067 * // produces
1068 * // $users[] = 'John Doe'
1070 * $sql = 'SELECT `group`, `name` FROM `user`'
1071 * $users = PMA_DBI_fetch_result($sql, array('group', null), 'name');
1072 * // produces
1073 * // $users['admin'][] = 'John Doe'
1075 * $sql = 'SELECT `group`, `name` FROM `user`'
1076 * $users = PMA_DBI_fetch_result($sql, array('group', 'name'), 'id');
1077 * // produces
1078 * // $users['admin']['John Doe'] = '123'
1079 * </code>
1081 * @param string|mysql_result $result query or mysql result
1082 * @param string|integer $key field-name or offset
1083 * used as key for array
1084 * @param string|integer $value value-name or offset
1085 * used as value for array
1086 * @param resource $link mysql link
1087 * @param mixed $options
1088 * @return array resultrows or values indexed by $key
1090 function PMA_DBI_fetch_result($result, $key = null, $value = null,
1091 $link = null, $options = 0)
1093 $resultrows = array();
1095 if (is_string($result)) {
1096 $result = PMA_DBI_try_query($result, $link, $options, false);
1099 // return empty array if result is empty or false
1100 if (! $result) {
1101 return $resultrows;
1104 $fetch_function = 'PMA_DBI_fetch_assoc';
1106 // no nested array if only one field is in result
1107 if (null === $key && 1 === PMA_DBI_num_fields($result)) {
1108 $value = 0;
1109 $fetch_function = 'PMA_DBI_fetch_row';
1112 // if $key is an integer use non associative mysql fetch function
1113 if (is_int($key)) {
1114 $fetch_function = 'PMA_DBI_fetch_row';
1117 if (null === $key && null === $value) {
1118 while ($row = $fetch_function($result)) {
1119 $resultrows[] = $row;
1121 } elseif (null === $key) {
1122 while ($row = $fetch_function($result)) {
1123 $resultrows[] = $row[$value];
1125 } elseif (null === $value) {
1126 if (is_array($key)) {
1127 while ($row = $fetch_function($result)) {
1128 $result_target =& $resultrows;
1129 foreach ($key as $key_index) {
1130 if (null === $key_index) {
1131 $result_target =& $result_target[];
1132 continue;
1135 if (! isset($result_target[$row[$key_index]])) {
1136 $result_target[$row[$key_index]] = array();
1138 $result_target =& $result_target[$row[$key_index]];
1140 $result_target = $row;
1142 } else {
1143 while ($row = $fetch_function($result)) {
1144 $resultrows[$row[$key]] = $row;
1147 } else {
1148 if (is_array($key)) {
1149 while ($row = $fetch_function($result)) {
1150 $result_target =& $resultrows;
1151 foreach ($key as $key_index) {
1152 if (null === $key_index) {
1153 $result_target =& $result_target[];
1154 continue;
1157 if (! isset($result_target[$row[$key_index]])) {
1158 $result_target[$row[$key_index]] = array();
1160 $result_target =& $result_target[$row[$key_index]];
1162 $result_target = $row[$value];
1164 } else {
1165 while ($row = $fetch_function($result)) {
1166 $resultrows[$row[$key]] = $row[$value];
1171 PMA_DBI_free_result($result);
1172 return $resultrows;
1176 * Get supported SQL compatibility modes
1178 * @return array supported SQL compatibility modes
1180 function PMA_DBI_getCompatibilities()
1182 $compats = array('NONE');
1183 $compats[] = 'ANSI';
1184 $compats[] = 'DB2';
1185 $compats[] = 'MAXDB';
1186 $compats[] = 'MYSQL323';
1187 $compats[] = 'MYSQL40';
1188 $compats[] = 'MSSQL';
1189 $compats[] = 'ORACLE';
1190 // removed; in MySQL 5.0.33, this produces exports that
1191 // can't be read by POSTGRESQL (see our bug #1596328)
1192 //$compats[] = 'POSTGRESQL';
1193 $compats[] = 'TRADITIONAL';
1195 return $compats;
1199 * returns warnings for last query
1201 * @param resource $link mysql link resource
1202 * @return array warnings
1204 function PMA_DBI_get_warnings($link = null)
1206 if (empty($link)) {
1207 if (isset($GLOBALS['userlink'])) {
1208 $link = $GLOBALS['userlink'];
1209 } else {
1210 return array();
1214 return PMA_DBI_fetch_result('SHOW WARNINGS', null, null, $link);
1218 * returns true (int > 0) if current user is superuser
1219 * otherwise 0
1221 * @return integer $is_superuser
1223 function PMA_isSuperuser()
1225 if (PMA_cacheExists('is_superuser', true)) {
1226 return PMA_cacheGet('is_superuser', true);
1229 // with mysql extension, when connection failed we don't have
1230 // a $userlink
1231 if (isset($GLOBALS['userlink'])) {
1232 $r = (bool) PMA_DBI_try_query('SELECT COUNT(*) FROM mysql.user', $GLOBALS['userlink'], PMA_DBI_QUERY_STORE);
1233 PMA_cacheSet('is_superuser', $r, true);
1234 } else {
1235 PMA_cacheSet('is_superuser', false, true);
1238 return PMA_cacheGet('is_superuser', true);
1242 * returns an array of PROCEDURE or FUNCTION names for a db
1244 * @param string $db db name
1245 * @param string $which PROCEDURE | FUNCTION
1246 * @param resource $link mysql link
1248 * @return array the procedure names or function names
1250 function PMA_DBI_get_procedures_or_functions($db, $which, $link = null)
1252 $shows = PMA_DBI_fetch_result('SHOW ' . $which . ' STATUS;', null, null, $link);
1253 $result = array();
1254 foreach ($shows as $one_show) {
1255 if ($one_show['Db'] == $db && $one_show['Type'] == $which) {
1256 $result[] = $one_show['Name'];
1259 return($result);
1263 * returns the definition of a specific PROCEDURE, FUNCTION, EVENT or VIEW
1265 * @param string $db db name
1266 * @param string $which PROCEDURE | FUNCTION | EVENT | VIEW
1267 * @param string $name the procedure|function|event|view name
1268 * @param resource $link mysql link
1270 * @return string the definition
1272 function PMA_DBI_get_definition($db, $which, $name, $link = null)
1274 $returned_field = array(
1275 'PROCEDURE' => 'Create Procedure',
1276 'FUNCTION' => 'Create Function',
1277 'EVENT' => 'Create Event',
1278 'VIEW' => 'Create View'
1280 $query = 'SHOW CREATE ' . $which . ' ' . PMA_backquote($db) . '.' . PMA_backquote($name);
1281 return(PMA_DBI_fetch_value($query, 0, $returned_field[$which]));
1285 * returns details about the TRIGGERs for a specific table or database
1287 * @param string $db db name
1288 * @param string $table table name
1289 * @param string $delimiter the delimiter to use (may be empty)
1291 * @return array information about triggers (may be empty)
1293 function PMA_DBI_get_triggers($db, $table = '', $delimiter = '//')
1295 $result = array();
1296 if (! $GLOBALS['cfg']['Server']['DisableIS']) {
1297 // Note: in http://dev.mysql.com/doc/refman/5.0/en/faqs-triggers.html
1298 // their example uses WHERE TRIGGER_SCHEMA='dbname' so let's use this
1299 // instead of WHERE EVENT_OBJECT_SCHEMA='dbname'
1300 $query = "SELECT TRIGGER_SCHEMA, TRIGGER_NAME, EVENT_MANIPULATION, EVENT_OBJECT_TABLE, ACTION_TIMING, ACTION_STATEMENT, EVENT_OBJECT_SCHEMA, EVENT_OBJECT_TABLE, DEFINER FROM information_schema.TRIGGERS WHERE TRIGGER_SCHEMA= '" . PMA_sqlAddSlashes($db,true) . "';";
1301 if (! empty($table)) {
1302 $query .= " AND EVENT_OBJECT_TABLE = '" . PMA_sqlAddSlashes($table, true) . "';";
1304 } else {
1305 $query = "SHOW TRIGGERS FROM " . PMA_backquote(PMA_sqlAddSlashes($db,true));
1306 if (! empty($table)) {
1307 $query .= " LIKE '" . PMA_sqlAddSlashes($table, true) . "';";
1311 if ($triggers = PMA_DBI_fetch_result($query)) {
1312 foreach ($triggers as $trigger) {
1313 if ($GLOBALS['cfg']['Server']['DisableIS']) {
1314 $trigger['TRIGGER_NAME'] = $trigger['Trigger'];
1315 $trigger['ACTION_TIMING'] = $trigger['Timing'];
1316 $trigger['EVENT_MANIPULATION'] = $trigger['Event'];
1317 $trigger['EVENT_OBJECT_TABLE'] = $trigger['Table'];
1318 $trigger['ACTION_STATEMENT'] = $trigger['Statement'];
1319 $trigger['DEFINER'] = $trigger['Definer'];
1321 $one_result = array();
1322 $one_result['name'] = $trigger['TRIGGER_NAME'];
1323 $one_result['table'] = $trigger['EVENT_OBJECT_TABLE'];
1324 $one_result['action_timing'] = $trigger['ACTION_TIMING'];
1325 $one_result['event_manipulation'] = $trigger['EVENT_MANIPULATION'];
1326 $one_result['definition'] = $trigger['ACTION_STATEMENT'];
1327 $one_result['definer'] = $trigger['DEFINER'];
1329 // do not prepend the schema name; this way, importing the
1330 // definition into another schema will work
1331 $one_result['full_trigger_name'] = PMA_backquote($trigger['TRIGGER_NAME']);
1332 $one_result['drop'] = 'DROP TRIGGER IF EXISTS ' . $one_result['full_trigger_name'];
1333 $one_result['create'] = 'CREATE TRIGGER ' . $one_result['full_trigger_name'] . ' ' . $trigger['ACTION_TIMING']. ' ' . $trigger['EVENT_MANIPULATION'] . ' ON ' . PMA_backquote($trigger['EVENT_OBJECT_TABLE']) . "\n" . ' FOR EACH ROW ' . $trigger['ACTION_STATEMENT'] . "\n" . $delimiter . "\n";
1335 $result[] = $one_result;
1339 // Sort results by name
1340 $name = array();
1341 foreach ($result as $key => $value) {
1342 $name[] = $value['name'];
1344 array_multisort($name, SORT_ASC, $result);
1346 return($result);
1350 * Returns true if $db.$view_name is a view, false if not
1352 * @param string $db database name
1353 * @param string $view_name view/table name
1355 * @return bool true if $db.$view_name is a view, false if not
1357 function PMA_isView($db, $view_name)
1359 $result = PMA_DBI_fetch_result(
1360 "SELECT TABLE_NAME
1361 FROM information_schema.VIEWS
1362 WHERE TABLE_SCHEMA = '" . PMA_sqlAddSlashes($db) . "'
1363 AND TABLE_NAME = '" . PMA_sqlAddSlashes($view_name) . "'");
1365 if ($result) {
1366 return true;
1367 } else {
1368 return false;