2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * @todo we must handle the case if sql.php is called directly with a query
5 * that returns 0 rows - to prevent cyclic redirects or includes
10 * Gets some core libraries
12 require_once './libraries/common.inc.php';
13 require_once './libraries/Table.class.php';
14 require_once './libraries/check_user_privileges.lib.php';
15 require_once './libraries/bookmark.lib.php';
17 $GLOBALS['js_include'][] = 'jquery/jquery-ui-1.8.custom.js';
18 $GLOBALS['js_include'][] = 'tbl_change.js';
20 if (isset($_SESSION['profiling'])) {
21 $GLOBALS['js_include'][] = 'highcharts/highcharts.js';
22 /* Files required for chart exporting */
23 $GLOBALS['js_include'][] = 'highcharts/exporting.js';
24 /* < IE 9 doesn't support canvas natively */
25 if (PMA_USR_BROWSER_AGENT
== 'IE' && PMA_USR_BROWSER_VER
< 9) {
26 $GLOBALS['js_include'][] = 'canvg/flashcanvas.js';
28 $GLOBALS['js_include'][] = 'canvg/canvg.js';
32 * Defines the url to return to in case of error in a sql statement
36 $is_gotofile = preg_replace('@^([^?]+).*$@s', '\\1', $goto);
37 if (! @file_exists
('./' . $is_gotofile)) {
40 $is_gotofile = ($is_gotofile == $goto);
43 $goto = (! strlen($table)) ?
$cfg['DefaultTabDatabase'] : $cfg['DefaultTabTable'];
47 if (! isset($err_url)) {
48 $err_url = (!empty($back) ?
$back : $goto)
49 . '?' . PMA_generate_common_url($db)
50 . ((strpos(' ' . $goto, 'db_') != 1 && strlen($table)) ?
'&table=' . urlencode($table) : '');
53 // Coming from a bookmark dialog
54 if (isset($fields['query'])) {
55 $sql_query = $fields['query'];
58 // This one is just to fill $db
59 if (isset($fields['dbase'])) {
60 $db = $fields['dbase'];
64 * During grid edit, if we have a relational field, show the dropdown for it
66 * Logic taken from libraries/display_tbl_lib.php
68 * This doesn't seem to be the right place to do this, but I can't think of any
69 * better place either.
71 if (isset($_REQUEST['get_relational_values']) && $_REQUEST['get_relational_values'] == true) {
72 require_once 'libraries/relation.lib.php';
74 $column = $_REQUEST['column'];
75 $foreigners = PMA_getForeigners($db, $table, $column);
77 $display_field = PMA_getDisplayField($foreigners[$column]['foreign_db'], $foreigners[$column]['foreign_table']);
79 $foreignData = PMA_getForeignData($foreigners, $column, false, '', '');
81 if ($_SESSION['tmp_user_values']['relational_display'] == 'D'
82 && (isset($display_field) && strlen($display_field)
83 && (isset($_REQUEST['relation_key_or_display_column']) && $_REQUEST['relation_key_or_display_column']))) {
84 $curr_value = $_REQUEST['relation_key_or_display_column'];
86 $curr_value = $_REQUEST['curr_value'];
88 if ($foreignData['disp_row'] == null) {
89 //Handle the case when number of values is more than $cfg['ForeignKeyMaxLimit']
96 $dropdown = '<span class="curr_value">' . htmlspecialchars($_REQUEST['curr_value']) . '</span> <a href="browse_foreigners.php' . PMA_generate_common_url($_url_params) . '"'
97 . ' target="_blank" class="browse_foreign" '
98 .'>' . __('Browse foreign values') . '</a>';
100 $dropdown = PMA_foreignDropdown($foreignData['disp_row'], $foreignData['foreign_field'], $foreignData['foreign_display'], $curr_value, $cfg['ForeignKeyMaxLimit']);
101 $dropdown = '<select>' . $dropdown . '</select>';
104 $extra_data['dropdown'] = $dropdown;
105 PMA_ajaxResponse(NULL, true, $extra_data);
109 * Just like above, find possible values for enum fields during grid edit.
111 * Logic taken from libraries/display_tbl_lib.php
113 if (isset($_REQUEST['get_enum_values']) && $_REQUEST['get_enum_values'] == true) {
114 $field_info_query = 'SHOW FIELDS FROM `' . $db . '`.`' . $table . '` LIKE \'' . $_REQUEST['column'] . '\' ;';
116 $field_info_result = PMA_DBI_fetch_result($field_info_query, null, null, null, PMA_DBI_QUERY_STORE
);
118 $search = array('enum', '(', ')', "'");
120 $values = explode(',', str_replace($search, '', $field_info_result[0]['Type']));
122 $dropdown = '<option value=""> </option>';
123 foreach ($values as $value) {
124 $dropdown .= '<option value="' . htmlspecialchars($value) . '"';
125 if ($value == $_REQUEST['curr_value']) {
126 $dropdown .= ' selected="selected"';
128 $dropdown .= '>' . $value . '</option>';
131 $dropdown = '<select>' . $dropdown . '</select>';
133 $extra_data['dropdown'] = $dropdown;
134 PMA_ajaxResponse(NULL, true, $extra_data);
138 * Find possible values for set fields during grid edit.
140 if (isset($_REQUEST['get_set_values']) && $_REQUEST['get_set_values'] == true) {
141 $field_info_query = 'SHOW FIELDS FROM `' . $db . '`.`' . $table . '` LIKE \'' . $_REQUEST['column'] . '\' ;';
143 $field_info_result = PMA_DBI_fetch_result($field_info_query, null, null, null, PMA_DBI_QUERY_STORE
);
145 $selected_values = explode(',', $_REQUEST['curr_value']);
147 $search = array('set', '(', ')', "'");
148 $values = explode(',', str_replace($search, '', $field_info_result[0]['Type']));
151 foreach ($values as $value) {
152 $select .= '<option value="' . htmlspecialchars($value) . '"';
153 if (in_array($value, $selected_values, true)) {
154 $select .= ' selected="selected"';
156 $select .= '>' . $value . '</option>';
159 $select_size = (sizeof($values) > 10) ?
10 : sizeof($values);
160 $select = '<select multiple="multiple" size="' . $select_size . '">' . $select . '</select>';
162 $extra_data['select'] = $select;
163 PMA_ajaxResponse(NULL, true, $extra_data);
167 * Check ajax request to set the column order
169 if (isset($_REQUEST['set_col_prefs']) && $_REQUEST['set_col_prefs'] == true) {
170 $pmatable = new PMA_Table($table, $db);
174 if (isset($_REQUEST['col_order'])) {
175 $col_order = explode(',', $_REQUEST['col_order']);
176 $retval = $pmatable->setUiProp(PMA_Table
::PROP_COLUMN_ORDER
, $col_order, $_REQUEST['table_create_time']);
177 if ($retval !== true) {
178 PMA_ajaxResponse($retval->getString(), false);
183 // set column visibility
184 if (isset($_REQUEST['col_visib'])) {
185 $col_visib = explode(',', $_REQUEST['col_visib']);
186 $retval = $pmatable->setUiProp(PMA_Table
::PROP_COLUMN_VISIB
, $col_visib, $_REQUEST['table_create_time']);
187 if ($retval !== true) {
188 PMA_ajaxResponse($retval->getString(), false);
192 PMA_ajaxResponse(NULL, ($retval == true));
195 // Default to browse if no query set and we have table
196 // (needed for browsing from DefaultTabTable)
197 if (empty($sql_query) && strlen($table) && strlen($db)) {
198 require_once './libraries/bookmark.lib.php';
199 $book_sql_query = PMA_Bookmark_get($db, '\'' . PMA_sqlAddSlashes($table) . '\'',
200 'label', false, true);
202 if (! empty($book_sql_query)) {
203 $GLOBALS['using_bookmark_message'] = PMA_message
::notice(__('Using bookmark "%s" as default browse query.'));
204 $GLOBALS['using_bookmark_message']->addParam($table);
205 $GLOBALS['using_bookmark_message']->addMessage(PMA_showDocu('faq6_22'));
206 $sql_query = $book_sql_query;
208 $sql_query = 'SELECT * FROM ' . PMA_backquote($table);
210 unset($book_sql_query);
212 // set $goto to what will be displayed if query returns 0 rows
213 $goto = 'tbl_structure.php';
215 // Now we can check the parameters
216 PMA_checkParameters(array('sql_query'));
219 // instead of doing the test twice
220 $is_drop_database = preg_match('/DROP[[:space:]]+(DATABASE|SCHEMA)[[:space:]]+/i',
224 * Check rights in case of DROP DATABASE
226 * This test may be bypassed if $is_js_confirmed = 1 (already checked with js)
227 * but since a malicious user may pass this variable by url/form, we don't take
228 * into account this case.
230 if (!defined('PMA_CHK_DROP')
231 && !$cfg['AllowUserDropDatabase']
234 require_once './libraries/header.inc.php';
235 PMA_mysqlDie(__('"DROP DATABASE" statements are disabled.'), '', '', $err_url);
238 require_once './libraries/display_tbl.lib.php';
239 PMA_displayTable_checkConfigParams();
242 * Need to find the real end of rows?
244 if (isset($find_real_end) && $find_real_end) {
245 $unlim_num_rows = PMA_Table
::countRecords($db, $table, $force_exact = true);
246 $_SESSION['tmp_user_values']['pos'] = @((ceil($unlim_num_rows / $_SESSION['tmp_user_values']['max_rows']) - 1) * $_SESSION['tmp_user_values']['max_rows']);
253 if (isset($store_bkm)) {
254 PMA_Bookmark_save($fields, (isset($bkm_all_users) && $bkm_all_users == 'true' ?
true : false));
255 // go back to sql.php to redisplay query; do not use & in this case:
256 PMA_sendHeaderLocation($cfg['PmaAbsoluteUri'] . $goto . '&label=' . $fields['label']);
260 * Parse and analyze the query
262 require_once './libraries/parse_analyze.lib.php';
265 * Sets or modifies the $goto variable if required
267 if ($goto == 'sql.php') {
268 $is_gotofile = false;
270 . PMA_generate_common_url($db, $table)
271 . '&sql_query=' . urlencode($sql_query);
276 * Go back to further page if table should not be dropped
278 if (isset($btnDrop) && $btnDrop == __('No')) {
283 if (strpos($goto, 'db_') === 0 && strlen($table)) {
286 $active_page = $goto;
287 require './' . PMA_securePath($goto);
289 PMA_sendHeaderLocation($cfg['PmaAbsoluteUri'] . str_replace('&', '&', $goto));
296 * Displays the confirm page if required
298 * This part of the script is bypassed if $is_js_confirmed = 1 (already checked
299 * with js) because possible security issue is not so important here: at most,
300 * the confirm message isn't displayed.
302 * Also bypassed if only showing php code.or validating a SQL query
304 if (! $cfg['Confirm'] ||
isset($_REQUEST['is_js_confirmed']) ||
isset($btnDrop)
305 // if we are coming from a "Create PHP code" or a "Without PHP Code"
306 // dialog, we won't execute the query anyway, so don't confirm
307 ||
isset($GLOBALS['show_as_php'])
308 ||
!empty($GLOBALS['validatequery'])) {
311 $do_confirm = isset($analyzed_sql[0]['queryflags']['need_confirm']);
315 $stripped_sql_query = $sql_query;
316 require_once './libraries/header.inc.php';
317 if ($is_drop_database) {
318 echo '<h1 class="error">' . __('You are about to DESTROY a complete database!') . '</h1>';
320 echo '<form action="sql.php" method="post">' . "\n"
321 .PMA_generate_common_hidden_inputs($db, $table);
323 <input type
="hidden" name
="sql_query" value
="<?php echo htmlspecialchars($sql_query); ?>" />
324 <input type
="hidden" name
="message_to_show" value
="<?php echo isset($message_to_show) ? PMA_sanitize($message_to_show, true) : ''; ?>" />
325 <input type
="hidden" name
="goto" value
="<?php echo $goto; ?>" />
326 <input type
="hidden" name
="back" value
="<?php echo isset($back) ? PMA_sanitize($back, true) : ''; ?>" />
327 <input type
="hidden" name
="reload" value
="<?php echo isset($reload) ? PMA_sanitize($reload, true) : 0; ?>" />
328 <input type
="hidden" name
="purge" value
="<?php echo isset($purge) ? PMA_sanitize($purge, true) : ''; ?>" />
329 <input type
="hidden" name
="dropped_column" value
="<?php echo isset($dropped_column) ? PMA_sanitize($dropped_column, true) : ''; ?>" />
330 <input type
="hidden" name
="show_query" value
="<?php echo isset($show_query) ? PMA_sanitize($show_query, true) : ''; ?>" />
332 echo '<fieldset class="confirmation">' . "\n"
333 .' <legend>' . __('Do you really want to ') . '</legend>'
334 .' <tt>' . htmlspecialchars($stripped_sql_query) . '</tt>' . "\n"
335 .'</fieldset>' . "\n"
336 .'<fieldset class="tblFooters">' . "\n";
338 <input type
="submit" name
="btnDrop" value
="<?php echo __('Yes'); ?>" id
="buttonYes" />
339 <input type
="submit" name
="btnDrop" value
="<?php echo __('No'); ?>" id
="buttonNo" />
341 echo '</fieldset>' . "\n"
345 * Displays the footer and exit
347 require './libraries/footer.inc.php';
348 } // end if $do_confirm
351 // Defines some variables
352 // A table has to be created, renamed, dropped -> navi frame should be reloaded
354 * @todo use the parser/analyzer
358 && preg_match('/^(CREATE|ALTER|DROP)\s+(VIEW|TABLE|DATABASE|SCHEMA)\s+/i', $sql_query)) {
362 // SK -- Patch: $is_group added for use in calculation of total number of
364 // $is_count is changed for more correct "LIMIT" clause
365 // appending in queries like
366 // "SELECT COUNT(...) FROM ... GROUP BY ..."
369 * @todo detect all this with the parser, to avoid problems finding
370 * those strings in comments or backquoted identifiers
373 $is_explain = $is_count = $is_export = $is_delete = $is_insert = $is_affected = $is_show = $is_maint = $is_analyse = $is_group = $is_func = $is_replace = false;
374 if ($is_select) { // see line 141
375 $is_group = preg_match('@(GROUP[[:space:]]+BY|HAVING|SELECT[[:space:]]+DISTINCT)[[:space:]]+@i', $sql_query);
376 $is_func = !$is_group && (preg_match('@[[:space:]]+(SUM|AVG|STD|STDDEV|MIN|MAX|BIT_OR|BIT_AND)\s*\(@i', $sql_query));
377 $is_count = !$is_group && (preg_match('@^SELECT[[:space:]]+COUNT\((.*\.+)?.*\)@i', $sql_query));
378 $is_export = (preg_match('@[[:space:]]+INTO[[:space:]]+OUTFILE[[:space:]]+@i', $sql_query));
379 $is_analyse = (preg_match('@[[:space:]]+PROCEDURE[[:space:]]+ANALYSE@i', $sql_query));
380 } elseif (preg_match('@^EXPLAIN[[:space:]]+@i', $sql_query)) {
382 } elseif (preg_match('@^DELETE[[:space:]]+@i', $sql_query)) {
385 } elseif (preg_match('@^(INSERT|LOAD[[:space:]]+DATA|REPLACE)[[:space:]]+@i', $sql_query)) {
388 if (preg_match('@^(REPLACE)[[:space:]]+@i', $sql_query)) {
391 } elseif (preg_match('@^UPDATE[[:space:]]+@i', $sql_query)) {
393 } elseif (preg_match('@^[[:space:]]*SHOW[[:space:]]+@i', $sql_query)) {
395 } elseif (preg_match('@^(CHECK|ANALYZE|REPAIR|OPTIMIZE)[[:space:]]+TABLE[[:space:]]+@i', $sql_query)) {
399 // assign default full_sql_query
400 $full_sql_query = $sql_query;
402 // Handle remembered sorting order, only for single table query
403 if ($GLOBALS['cfg']['RememberSorting']
404 && ! ($is_count ||
$is_export ||
$is_func ||
$is_analyse)
405 && count($analyzed_sql[0]['select_expr']) == 0
406 && isset($analyzed_sql[0]['queryflags']['select_from'])
407 && count($analyzed_sql[0]['table_ref']) == 1
409 $pmatable = new PMA_Table($table, $db);
410 if (empty($analyzed_sql[0]['order_by_clause'])) {
411 $sorted_col = $pmatable->getUiProp(PMA_Table
::PROP_SORTED_COLUMN
);
413 // retrieve the remembered sorting order for current table
414 $sql_order_to_append = ' ORDER BY ' . $sorted_col . ' ';
415 $full_sql_query = $analyzed_sql[0]['section_before_limit'] . $sql_order_to_append . $analyzed_sql[0]['section_after_limit'];
417 // update the $analyzed_sql
418 $analyzed_sql[0]['section_before_limit'] .= $sql_order_to_append;
419 $analyzed_sql[0]['order_by_clause'] = $sorted_col;
422 // store the remembered table into session
423 $pmatable->setUiProp(PMA_Table
::PROP_SORTED_COLUMN
, $analyzed_sql[0]['order_by_clause']);
427 // Do append a "LIMIT" clause?
428 if ((! $cfg['ShowAll'] ||
$_SESSION['tmp_user_values']['max_rows'] != 'all')
429 && ! ($is_count ||
$is_export ||
$is_func ||
$is_analyse)
430 && isset($analyzed_sql[0]['queryflags']['select_from'])
431 && ! isset($analyzed_sql[0]['queryflags']['offset'])
432 && empty($analyzed_sql[0]['limit_clause'])
434 $sql_limit_to_append = ' LIMIT ' . $_SESSION['tmp_user_values']['pos'] . ', ' . $_SESSION['tmp_user_values']['max_rows'] . " ";
436 $full_sql_query = $analyzed_sql[0]['section_before_limit'] . "\n" . $sql_limit_to_append . $analyzed_sql[0]['section_after_limit'];
438 * @todo pretty printing of this modified query
440 if (isset($display_query)) {
441 // if the analysis of the original query revealed that we found
442 // a section_after_limit, we now have to analyze $display_query
443 // to display it correctly
445 if (!empty($analyzed_sql[0]['section_after_limit']) && trim($analyzed_sql[0]['section_after_limit']) != ';') {
446 $analyzed_display_query = PMA_SQP_analyze(PMA_SQP_parse($display_query));
447 $display_query = $analyzed_display_query[0]['section_before_limit'] . "\n" . $sql_limit_to_append . $analyzed_display_query[0]['section_after_limit'];
454 PMA_DBI_select_db($db);
457 // E x e c u t e t h e q u e r y
459 // Only if we didn't ask to see the php code (mikebeck)
460 if (isset($GLOBALS['show_as_php']) ||
!empty($GLOBALS['validatequery'])) {
464 if (isset($_SESSION['profiling']) && PMA_profilingSupported()) {
465 PMA_DBI_query('SET PROFILING=1;');
468 // Measure query time.
469 $querytime_before = array_sum(explode(' ', microtime()));
471 $result = @PMA_DBI_try_query
($full_sql_query, null, PMA_DBI_QUERY_STORE
);
473 // If a stored procedure was called, there may be more results that are
474 // queued up and waiting to be flushed from the buffer. So let's do that.
476 if (! PMA_DBI_more_results()) {
479 PMA_DBI_next_result();
482 $querytime_after = array_sum(explode(' ', microtime()));
484 $GLOBALS['querytime'] = $querytime_after - $querytime_before;
486 // Displays an error message if required and stop parsing the script
487 if ($error = PMA_DBI_getError()) {
489 if (strpos($goto, 'db_') === 0 && strlen($table)) {
492 $active_page = $goto;
493 $message = PMA_Message
::rawError($error);
495 if ($GLOBALS['is_ajax_request'] == true) {
496 PMA_ajaxResponse($message, false);
502 require './' . PMA_securePath($goto);
504 $full_err_url = (preg_match('@^(db|tbl)_@', $err_url))
505 ?
$err_url . '&show_query=1&sql_query=' . urlencode($sql_query)
507 PMA_mysqlDie($error, $full_sql_query, '', $full_err_url);
513 // Gets the number of rows affected/returned
514 // (This must be done immediately after the query because
515 // mysql_affected_rows() reports about the last query done)
518 $num_rows = ($result) ? @PMA_DBI_num_rows
($result) : 0;
519 } elseif (! isset($num_rows)) {
520 $num_rows = @PMA_DBI_affected_rows
();
523 // Grabs the profiling results
524 if (isset($_SESSION['profiling']) && PMA_profilingSupported()) {
525 $profiling_results = PMA_DBI_fetch_result('SHOW PROFILE;');
528 // Checks if the current database has changed
529 // This could happen if the user sends a query like "USE `database`;"
531 * commented out auto-switching to active database - really required?
532 * bug #1814718 win: table list disappears (mixed case db names)
533 * https://sourceforge.net/support/tracker.php?aid=1814718
534 * @todo RELEASE test and comit or rollback before release
535 $current_db = PMA_DBI_fetch_value('SELECT DATABASE()');
536 if ($db !== $current_db) {
543 // tmpfile remove after convert encoding appended by Y.Kawada
544 if (function_exists('PMA_kanji_file_conv')
545 && (isset($textfile) && file_exists($textfile))) {
549 // Counts the total number of rows for the same 'SELECT' query without the
550 // 'LIMIT' clause that may have been programatically added
552 if (empty($sql_limit_to_append)) {
553 $unlim_num_rows = $num_rows;
554 // if we did not append a limit, set this to get a correct
555 // "Showing rows..." message
556 //$_SESSION['tmp_user_values']['max_rows'] = 'all';
557 } elseif ($is_select) {
559 // c o u n t q u e r y
561 // If we are "just browsing", there is only one table,
562 // and no WHERE clause (or just 'WHERE 1 '),
563 // we do a quick count (which uses MaxExactCount) because
564 // SQL_CALC_FOUND_ROWS is not quick on large InnoDB tables
566 // However, do not count again if we did it previously
567 // due to $find_real_end == true
570 && ! isset($analyzed_sql[0]['queryflags']['union'])
571 && ! isset($analyzed_sql[0]['table_ref'][1]['table_name'])
572 && (empty($analyzed_sql[0]['where_clause'])
573 ||
$analyzed_sql[0]['where_clause'] == '1 ')
574 && ! isset($find_real_end)
577 // "j u s t b r o w s i n g"
578 $unlim_num_rows = PMA_Table
::countRecords($db, $table);
580 } else { // n o t " j u s t b r o w s i n g "
582 // add select expression after the SQL_CALC_FOUND_ROWS
584 // for UNION, just adding SQL_CALC_FOUND_ROWS
585 // after the first SELECT works.
587 // take the left part, could be:
590 $count_query = PMA_SQP_formatHtml($parsed_sql, 'query_only', 0, $analyzed_sql[0]['position_of_first_select'] +
1);
591 $count_query .= ' SQL_CALC_FOUND_ROWS ';
592 // add everything that was after the first SELECT
593 $count_query .= PMA_SQP_formatHtml($parsed_sql, 'query_only', $analyzed_sql[0]['position_of_first_select']+
1);
594 // ensure there is no semicolon at the end of the
595 // count query because we'll probably add
596 // a LIMIT 1 clause after it
597 $count_query = rtrim($count_query);
598 $count_query = rtrim($count_query, ';');
600 // if using SQL_CALC_FOUND_ROWS, add a LIMIT to avoid
601 // long delays. Returned count will be complete anyway.
602 // (but a LIMIT would disrupt results in an UNION)
604 if (! isset($analyzed_sql[0]['queryflags']['union'])) {
605 $count_query .= ' LIMIT 1';
608 // run the count query
610 PMA_DBI_try_query($count_query);
611 // if (mysql_error()) {
614 // (SELECT `User`, `Host`, `Db`, `Select_priv` FROM `db`)
615 // UNION (SELECT `User`, `Host`, "%" AS "Db",
617 // FROM `user`) ORDER BY `User`, `Host`, `Db`;
618 // and although the generated count_query is wrong
619 // the SELECT FOUND_ROWS() work! (maybe it gets the
620 // count from the latest query that worked)
622 // another case where the count_query is wrong:
623 // SELECT COUNT(*), f1 from t1 group by f1
624 // and you click to sort on count(*)
626 $unlim_num_rows = PMA_DBI_fetch_value('SELECT FOUND_ROWS()');
627 } // end else "just browsing"
629 } else { // not $is_select
631 } // end rows total count
633 // if a table or database gets dropped, check column comments.
634 if (isset($purge) && $purge == '1') {
638 require_once './libraries/relation_cleanup.lib.php';
640 if (strlen($table) && strlen($db)) {
641 PMA_relationsCleanupTable($db, $table);
642 } elseif (strlen($db)) {
643 PMA_relationsCleanupDatabase($db);
645 // VOID. No DB/Table gets deleted.
646 } // end if relation-stuff
649 // If a column gets dropped, do relation magic.
650 if (isset($dropped_column) && strlen($db) && strlen($table) && !empty($dropped_column)) {
651 require_once './libraries/relation_cleanup.lib.php';
652 PMA_relationsCleanupColumn($db, $table, $dropped_column);
654 } // end if column was dropped
655 } // end else "didn't ask to see php code"
657 // No rows returned -> move back to the calling page
658 if (0 == $num_rows ||
$is_affected) {
660 $message = PMA_Message
::deleted_rows($num_rows);
661 } elseif ($is_insert) {
663 /* For replace we get DELETED + INSERTED row count, so we have to call it affected */
664 $message = PMA_Message
::affected_rows($num_rows);
666 $message = PMA_Message
::inserted_rows($num_rows);
668 $insert_id = PMA_DBI_insert_id();
669 if ($insert_id != 0) {
670 // insert_id is id of FIRST record inserted in one insert, so if we inserted multiple rows, we had to increment this
671 $message->addMessage('[br]');
672 // need to use a temporary because the Message class
673 // currently supports adding parameters only to the first
675 $_inserted = PMA_Message
::notice(__('Inserted row id: %1$d'));
676 $_inserted->addParam($insert_id +
$num_rows - 1);
677 $message->addMessage($_inserted);
679 } elseif ($is_affected) {
680 $message = PMA_Message
::affected_rows($num_rows);
682 // Ok, here is an explanation for the !$is_select.
683 // The form generated by sql_query_form.lib.php
684 // and db_sql.php has many submit buttons
685 // on the same form, and some confusion arises from the
686 // fact that $message_to_show is sent for every case.
687 // The $message_to_show containing a success message and sent with
688 // the form should not have priority over errors
689 } elseif (!empty($message_to_show) && !$is_select) {
690 $message = PMA_Message
::rawSuccess(htmlspecialchars($message_to_show));
691 } elseif (!empty($GLOBALS['show_as_php'])) {
692 $message = PMA_Message
::success(__('Showing as PHP code'));
693 } elseif (isset($GLOBALS['show_as_php'])) {
694 /* User disable showing as PHP, query is only displayed */
695 $message = PMA_Message
::notice(__('Showing SQL query'));
696 } elseif (!empty($GLOBALS['validatequery'])) {
697 $message = PMA_Message
::notice(__('Validated SQL'));
699 $message = PMA_Message
::success(__('MySQL returned an empty result set (i.e. zero rows).'));
702 if (isset($GLOBALS['querytime'])) {
703 $_querytime = PMA_Message
::notice(__('Query took %01.4f sec'));
704 $_querytime->addParam($GLOBALS['querytime']);
705 $message->addMessage('(');
706 $message->addMessage($_querytime);
707 $message->addMessage(')');
710 if ($GLOBALS['is_ajax_request'] == true) {
711 if ($cfg['ShowSQL']) {
712 $extra_data['sql_query'] = PMA_showMessage($message, $GLOBALS['sql_query'], 'success');
714 if (isset($GLOBALS['reload']) && $GLOBALS['reload'] == 1) {
715 $extra_data['reload'] = 1;
716 $extra_data['db'] = $GLOBALS['db'];
718 PMA_ajaxResponse($message, $message->isSuccess(), (isset($extra_data) ?
$extra_data : ''));
722 $goto = PMA_securePath($goto);
723 // Checks for a valid target script
724 $is_db = $is_table = false;
725 if (isset($_REQUEST['purge']) && $_REQUEST['purge'] == '1') {
727 unset($url_params['table']);
729 include 'libraries/db_table_exists.lib.php';
731 if (strpos($goto, 'tbl_') === 0 && ! $is_table) {
732 if (strlen($table)) {
735 $goto = 'db_sql.php';
737 if (strpos($goto, 'db_') === 0 && ! $is_db) {
743 // Loads to target script
744 if ($goto != 'main.php') {
745 require_once './libraries/header.inc.php';
747 $active_page = $goto;
748 require './' . $goto;
750 // avoid a redirect loop when last record was deleted
751 if (0 == $num_rows && 'sql.php' == $cfg['DefaultTabTable']) {
752 $goto = str_replace('sql.php','tbl_structure.php',$goto);
754 PMA_sendHeaderLocation($cfg['PmaAbsoluteUri'] . str_replace('&', '&', $goto) . '&message=' . urlencode($message));
757 } // end no rows returned
759 // At least one row is returned -> displays a table with results
761 //If we are retrieving the full value of a truncated field or the original
762 // value of a transformed field, show it here and exit
763 if ($GLOBALS['grid_edit'] == true && $GLOBALS['cfg']['AjaxEnable']) {
764 $row = PMA_DBI_fetch_row($result);
765 $extra_data = array();
766 $extra_data['value'] = $row[0];
767 PMA_ajaxResponse(NULL, true, $extra_data);
770 // Displays the headers
771 if (isset($show_query)) {
774 if (isset($printview) && $printview == '1') {
775 require_once './libraries/header_printview.inc.php';
778 $GLOBALS['js_include'][] = 'functions.js';
779 $GLOBALS['js_include'][] = 'makegrid.js';
780 $GLOBALS['js_include'][] = 'sql.js';
784 if (! $GLOBALS['is_ajax_request'] ||
! $GLOBALS['cfg']['AjaxEnable']) {
785 if (strlen($table)) {
786 require './libraries/tbl_common.php';
787 $url_query .= '&goto=tbl_sql.php&back=tbl_sql.php';
788 require './libraries/tbl_info.inc.php';
789 require './libraries/tbl_links.inc.php';
790 } elseif (strlen($db)) {
791 require './libraries/db_common.inc.php';
792 require './libraries/db_info.inc.php';
794 require './libraries/server_common.inc.php';
795 require './libraries/server_links.inc.php';
798 require_once './libraries/header.inc.php';
799 //we don't need to buffer the output in PMA_showMessage here.
800 //set a global variable and check against it in the function
801 $GLOBALS['buffer_message'] = false;
806 $cfgRelation = PMA_getRelationsParam();
809 // Gets the list of fields properties
810 if (isset($result) && $result) {
811 $fields_meta = PMA_DBI_get_fields_meta($result);
812 $fields_cnt = count($fields_meta);
815 if (! $GLOBALS['is_ajax_request']) {
816 //begin the sqlqueryresults div here. container div
817 echo '<div id="sqlqueryresults"';
818 if ($GLOBALS['cfg']['AjaxEnable']) {
819 echo ' class="ajax"';
824 // Display previous update query (from tbl_replace)
825 if (isset($disp_query) && $cfg['ShowSQL'] == true) {
826 PMA_showMessage($disp_message, $disp_query, 'success');
829 if (isset($profiling_results)) {
830 // pma_token/url_query needed for chart export
832 <script type
="text/javascript">
833 pma_token
= '<?php echo $_SESSION[' PMA_token
']; ?>';
834 url_query
= '<?php echo isset($url_query)?$url_query:PMA_generate_common_url($db);?>';
835 $
(document
).ready(makeProfilingChart
);
838 echo '<fieldset><legend>' . __('Profiling') . '</legend>' . "\n";
839 echo '<div style="float: left;">';
840 echo '<table>' . "\n";
842 echo ' <th>' . __('Status') . PMA_showMySQLDocu('general-thread-states','general-thread-states') . '</th>' . "\n";
843 echo ' <th>' . __('Time') . '</th>' . "\n";
844 echo ' </tr>' . "\n";
846 $chart_json = Array();
847 foreach ($profiling_results as $one_result) {
849 echo '<td>' . ucwords($one_result['Status']) . '</td>' . "\n";
850 echo '<td align="right">' . (PMA_formatNumber($one_result['Duration'],3,1)) . 's</td>' . "\n";
851 $chart_json[ucwords($one_result['Status'])] = $one_result['Duration'];
854 echo '</table>' . "\n";
856 //require_once './libraries/chart.lib.php';
857 echo '<div id="profilingchart" style="display:none;">';
858 //PMA_chart_profiling($profiling_results);
859 echo json_encode($chart_json);
861 echo '</fieldset>' . "\n";
864 // Displays the results in a table
865 if (empty($disp_mode)) {
866 // see the "PMA_setDisplayMode()" function in
867 // libraries/display_tbl.lib.php
868 $disp_mode = 'urdr111101';
871 // hide edit and delete links for information_schema
872 if ($db == 'information_schema') {
873 $disp_mode = 'nnnn110111';
877 $message = PMA_message
::success(__('Bookmark %s created'));
878 $message->addParam($label);
882 PMA_displayTable($result, $disp_mode, $analyzed_sql);
883 PMA_DBI_free_result($result);
885 // BEGIN INDEX CHECK See if indexes should be checked.
886 if (isset($query_type) && $query_type == 'check_tbl' && isset($selected) && is_array($selected)) {
887 foreach ($selected as $idx => $tbl_name) {
888 $check = PMA_Index
::findDuplicates($tbl_name, $db);
889 if (! empty($check)) {
890 printf(__('Problems with indexes of table `%s`'), $tbl_name);
896 // Bookmark support if required
897 if ($disp_mode[7] == '1'
898 && (! empty($cfg['Bookmark']) && empty($id_bookmark))
899 && !empty($sql_query)) {
903 . PMA_generate_common_url($db, $table)
904 . '&sql_query=' . urlencode($sql_query)
905 . '&id_bookmark=1';
908 <form action
="sql.php" method
="post" onsubmit
="return emptyFormElements(this, 'fields[label]');">
909 <?php
echo PMA_generate_common_hidden_inputs(); ?
>
910 <input type
="hidden" name
="goto" value
="<?php echo $goto; ?>" />
911 <input type
="hidden" name
="fields[dbase]" value
="<?php echo htmlspecialchars($db); ?>" />
912 <input type
="hidden" name
="fields[user]" value
="<?php echo $cfg['Bookmark']['user']; ?>" />
913 <input type
="hidden" name
="fields[query]" value
="<?php echo urlencode(isset($complete_query) ? $complete_query : $sql_query); ?>" />
916 echo PMA_getIcon('b_bookmark.png', __('Bookmark this SQL query'));
920 <div
class="formelement">
921 <label
for="fields_label_"><?php
echo __('Label'); ?
>:</label
>
922 <input type
="text" id
="fields_label_" name
="fields[label]" value
="" />
925 <div
class="formelement">
926 <input type
="checkbox" name
="bkm_all_users" id
="bkm_all_users" value
="true" />
927 <label
for="bkm_all_users"><?php
echo __('Let every user access this bookmark'); ?
></label
>
930 <div
class="clearfloat"></div
>
932 <fieldset
class="tblFooters">
933 <input type
="submit" name
="store_bkm" value
="<?php echo __('Bookmark this SQL query'); ?>" />
937 } // end bookmark support
939 // Do print the page if required
940 if (isset($printview) && $printview == '1') {
942 <script type
="text/javascript">
945 window
.onload
= function()
947 if (typeof(window
.print) != 'undefined') {
956 if ($GLOBALS['is_ajax_request'] != true) {
957 echo '</div>'; // end sqlqueryresults div
959 } // end rows returned
962 * Displays the footer
964 if (! isset($_REQUEST['table_maintenance'])) {
965 require './libraries/footer.inc.php';