Adapt to changes in export.
[phpmyadmin/last10db.git] / sql.php
bloba25dbef11a99b3ca33c3375b4866a6b5745d361a
1 <?php
2 /* $Id$ */
3 // vim: expandtab sw=4 ts=4 sts=4:
4 /**
5 * @todo we must handle the case if sql.php is called directly with a query
6 * what returns 0 rows - to prevent cyclic redirects or includes
7 */
9 /**
10 * Gets some core libraries
12 require_once './libraries/common.lib.php';
13 require_once './libraries/Table.class.php';
14 require_once './libraries/tbl_indexes.lib.php';
15 require_once './libraries/check_user_privileges.lib.php';
16 require_once './libraries/bookmark.lib.php';
18 /**
19 * Could be coming from a subform ("T" column expander)
21 if (isset($_REQUEST['dontlimitchars'])) {
22 $dontlimitchars = $_REQUEST['dontlimitchars'];
25 /**
26 * Defines the url to return to in case of error in a sql statement
28 // Security checkings
29 if (!empty($goto)) {
30 $is_gotofile = preg_replace('@^([^?]+).*$@s', '\\1', $goto);
31 if (!@file_exists('./' . $is_gotofile)) {
32 unset($goto);
33 } else {
34 $is_gotofile = ($is_gotofile == $goto);
36 } // end if (security checkings)
38 if (empty($goto)) {
39 $goto = (! isset($table) || ! strlen($table)) ? $cfg['DefaultTabDatabase'] : $cfg['DefaultTabTable'];
40 $is_gotofile = true;
41 } // end if
42 if (!isset($err_url)) {
43 $err_url = (!empty($back) ? $back : $goto)
44 . '?' . PMA_generate_common_url(isset($db) ? $db : '')
45 . ((strpos(' ' . $goto, 'db_details') != 1 && isset($table)) ? '&amp;table=' . urlencode($table) : '');
46 } // end if
48 // Coming from a bookmark dialog
49 if (isset($fields['query'])) {
50 $sql_query = $fields['query'];
53 // This one is just to fill $db
54 if (isset($fields['dbase'])) {
55 $db = $fields['dbase'];
58 // Default to browse if no query set an we have table
59 // (needed for browsing from DefaultTabTable)
60 if (! isset($sql_query) && isset($table) && isset($db)) {
61 require_once './libraries/bookmark.lib.php';
62 $book_sql_query = PMA_queryBookmarks($db,
63 $GLOBALS['cfg']['Bookmark'], '\'' . PMA_sqlAddslashes($table) . '\'',
64 'label');
66 if (! empty($book_sql_query)) {
67 $sql_query = $book_sql_query;
68 } else {
69 $sql_query = 'SELECT * FROM ' . PMA_backquote($table);
71 unset($book_sql_query);
73 // set $goto to what will be displayed if query returns 0 rows
74 $goto = 'tbl_properties_structure.php';
75 } else {
76 // Now we can check the parameters
77 PMA_checkParameters(array('sql_query'));
80 // instead of doing the test twice
81 $is_drop_database = preg_match('/DROP[[:space:]]+(DATABASE|SCHEMA)[[:space:]]+/i',
82 $sql_query);
84 /**
85 * Check rights in case of DROP DATABASE
87 * This test may be bypassed if $is_js_confirmed = 1 (already checked with js)
88 * but since a malicious user may pass this variable by url/form, we don't take
89 * into account this case.
91 if (!defined('PMA_CHK_DROP')
92 && !$cfg['AllowUserDropDatabase']
93 && $is_drop_database
94 && !$is_superuser) {
95 require_once './libraries/header.inc.php';
96 PMA_mysqlDie($strNoDropDatabases, '', '', $err_url);
97 } // end if
101 * Need to find the real end of rows?
104 if (isset($find_real_end) && $find_real_end) {
105 $unlim_num_rows = PMA_Table::countRecords($db, $table, true, true);
106 $pos = @((ceil($unlim_num_rows / $session_max_rows) - 1) * $session_max_rows);
109 * Avoids undefined variables
111 elseif (!isset($pos)) {
112 $pos = 0;
116 * Bookmark add
118 if (isset($store_bkm)) {
119 PMA_addBookmarks($fields, $cfg['Bookmark'], (isset($bkm_all_users) && $bkm_all_users == 'true' ? true : false));
120 PMA_sendHeaderLocation($cfg['PmaAbsoluteUri'] . $goto);
121 } // end if
125 * Gets the true sql query
127 // $sql_query has been urlencoded in the confirmation form for drop/delete
128 // queries or in the navigation bar for browsing among records
129 if (isset($btnDrop) || isset($navig)) {
130 $sql_query = urldecode($sql_query);
134 * Reformat the query
137 $GLOBALS['unparsed_sql'] = $sql_query;
138 $parsed_sql = PMA_SQP_parse($sql_query);
139 $analyzed_sql = PMA_SQP_analyze($parsed_sql);
141 // Bug #641765 - Robbat2 - 12 January 2003, 10:49PM
142 // Reverted - Robbat2 - 13 January 2003, 2:40PM
144 // lem9: for bug 780516: now that we use case insensitive preg_match
145 // or flags from the analyser, do not put back the reformatted query
146 // into $sql_query, to make this kind of query work without
147 // capitalizing keywords:
149 // CREATE TABLE SG_Persons (
150 // id int(10) unsigned NOT NULL auto_increment,
151 // first varchar(64) NOT NULL default '',
152 // PRIMARY KEY (`id`)
153 // )
155 // Note: now we probably do not need to fill and use $GLOBALS['unparsed_sql']
156 // but I let this intact for now.
158 //$sql_query = PMA_SQP_formatHtml($parsed_sql, 'query_only');
161 // check for a real SELECT ... FROM
162 $is_select = isset($analyzed_sql[0]['queryflags']['select_from']);
164 // If the query is a Select, extract the db and table names and modify
165 // $db and $table, to have correct page headers, links and left frame.
166 // db and table name may be enclosed with backquotes, db is optionnal,
167 // query may contain aliases.
169 // (TODO: if there are more than one table name in the Select:
170 // - do not extract the first table name
171 // - do not show a table name in the page header
172 // - do not display the sub-pages links)
174 if ($is_select) {
175 $prev_db = $db;
176 if (isset($analyzed_sql[0]['table_ref'][0]['table_true_name'])) {
177 $table = $analyzed_sql[0]['table_ref'][0]['table_true_name'];
179 if (isset($analyzed_sql[0]['table_ref'][0]['db'])
180 && strlen($analyzed_sql[0]['table_ref'][0]['db'])) {
181 $db = $analyzed_sql[0]['table_ref'][0]['db'];
182 } else {
183 $db = $prev_db;
185 // Nijel: don't change reload, if we already decided to reload in import
186 if (empty($reload)) {
187 $reload = ($db == $prev_db) ? 0 : 1;
192 * Sets or modifies the $goto variable if required
194 if ($goto == 'sql.php') {
195 $is_gotofile = false;
196 $goto = 'sql.php?'
197 . PMA_generate_common_url($db, $table)
198 . '&amp;pos=' . $pos
199 . '&amp;sql_query=' . urlencode($sql_query);
200 } // end if
204 * Go back to further page if table should not be dropped
206 if (isset($btnDrop) && $btnDrop == $strNo) {
207 if (!empty($back)) {
208 $goto = $back;
210 if ($is_gotofile) {
211 if (strpos(' ' . $goto, 'db_details') == 1 && isset($table) && strlen($table)) {
212 unset($table);
214 $active_page = $goto;
215 require './' . PMA_securePath($goto);
216 } else {
217 PMA_sendHeaderLocation($cfg['PmaAbsoluteUri'] . str_replace('&amp;', '&', $goto));
219 exit();
220 } // end if
224 * Displays the confirm page if required
226 * This part of the script is bypassed if $is_js_confirmed = 1 (already checked
227 * with js) because possible security issue is not so important here: at most,
228 * the confirm message isn't displayed.
230 * Also bypassed if only showing php code.or validating a SQL query
232 if (!$cfg['Confirm']
233 || (isset($is_js_confirmed) && $is_js_confirmed)
234 || isset($btnDrop)
236 // if we are coming from a "Create PHP code" or a "Without PHP Code"
237 // dialog, we won't execute the query anyway, so don't confirm
238 //|| !empty($GLOBALS['show_as_php'])
239 || isset($GLOBALS['show_as_php'])
241 || !empty($GLOBALS['validatequery'])) {
242 $do_confirm = false;
243 } else {
244 $do_confirm = isset($analyzed_sql[0]['queryflags']['need_confirm']);
247 if ($do_confirm) {
248 $stripped_sql_query = $sql_query;
249 require_once './libraries/header.inc.php';
250 if ($is_drop_database) {
251 echo '<h1 class="warning">' . $strDropDatabaseStrongWarning . '</h1>';
253 echo '<form action="sql.php" method="post">' . "\n"
254 .PMA_generate_common_hidden_inputs($db, (isset($table)?$table:''));
256 <input type="hidden" name="sql_query" value="<?php echo urlencode($sql_query); ?>" />
257 <input type="hidden" name="zero_rows" value="<?php echo isset($zero_rows) ? PMA_sanitize($zero_rows) : ''; ?>" />
258 <input type="hidden" name="goto" value="<?php echo $goto; ?>" />
259 <input type="hidden" name="back" value="<?php echo isset($back) ? PMA_sanitize($back) : ''; ?>" />
260 <input type="hidden" name="reload" value="<?php echo isset($reload) ? PMA_sanitize($reload) : 0; ?>" />
261 <input type="hidden" name="purge" value="<?php echo isset($purge) ? PMA_sanitize($purge) : ''; ?>" />
262 <input type="hidden" name="cpurge" value="<?php echo isset($cpurge) ? PMA_sanitize($cpurge) : ''; ?>" />
263 <input type="hidden" name="purgekey" value="<?php echo isset($purgekey) ? PMA_sanitize($purgekey) : ''; ?>" />
264 <input type="hidden" name="show_query" value="<?php echo isset($show_query) ? PMA_sanitize($show_query) : ''; ?>" />
265 <?php
266 echo '<fieldset class="confirmation">' . "\n"
267 .' <legend>' . $strDoYouReally . '</legend>'
268 .' <tt>' . htmlspecialchars($stripped_sql_query) . '</tt>' . "\n"
269 .'</fieldset>' . "\n"
270 .'<fieldset class="tblFooters">' . "\n";
272 <input type="submit" name="btnDrop" value="<?php echo $strYes; ?>" id="buttonYes" />
273 <input type="submit" name="btnDrop" value="<?php echo $strNo; ?>" id="buttonNo" />
274 <?php
275 echo '</fieldset>' . "\n"
276 . '</form>' . "\n";
277 } // end if $do_confirm
281 * Executes the query and displays results
283 else {
284 if (!isset($sql_query)) {
285 $sql_query = '';
287 // Defines some variables
288 // A table has to be created or renamed -> left frame should be reloaded
289 // TODO: use the parser/analyzer
291 if (empty($reload)
292 && preg_match('/^(CREATE|ALTER|DROP)\s+(VIEW|TABLE|DATABASE|SCHEMA)\s+/i', $sql_query)) {
293 $reload = 1;
295 // Gets the number of rows per page
296 if (empty($session_max_rows)) {
297 $session_max_rows = $cfg['MaxRows'];
298 } elseif ($session_max_rows != 'all') {
299 $cfg['MaxRows'] = $session_max_rows;
301 // Defines the display mode (horizontal/vertical) and header "frequency"
302 if (empty($disp_direction)) {
303 $disp_direction = $cfg['DefaultDisplay'];
305 if (empty($repeat_cells)) {
306 $repeat_cells = $cfg['RepeatCells'];
309 // SK -- Patch: $is_group added for use in calculation of total number of
310 // rows.
311 // $is_count is changed for more correct "LIMIT" clause
312 // appending in queries like
313 // "SELECT COUNT(...) FROM ... GROUP BY ..."
315 // TODO: detect all this with the parser, to avoid problems finding
316 // those strings in comments or backquoted identifiers
318 $is_explain = $is_count = $is_export = $is_delete = $is_insert = $is_affected = $is_show = $is_maint = $is_analyse = $is_group = $is_func = false;
319 if ($is_select) { // see line 141
320 $is_group = preg_match('@(GROUP[[:space:]]+BY|HAVING|SELECT[[:space:]]+DISTINCT)[[:space:]]+@i', $sql_query);
321 $is_func = !$is_group && (preg_match('@[[:space:]]+(SUM|AVG|STD|STDDEV|MIN|MAX|BIT_OR|BIT_AND)\s*\(@i', $sql_query));
322 $is_count = !$is_group && (preg_match('@^SELECT[[:space:]]+COUNT\((.*\.+)?.*\)@i', $sql_query));
323 $is_export = (preg_match('@[[:space:]]+INTO[[:space:]]+OUTFILE[[:space:]]+@i', $sql_query));
324 $is_analyse = (preg_match('@[[:space:]]+PROCEDURE[[:space:]]+ANALYSE@i', $sql_query));
325 } elseif (preg_match('@^EXPLAIN[[:space:]]+@i', $sql_query)) {
326 $is_explain = true;
327 } elseif (preg_match('@^DELETE[[:space:]]+@i', $sql_query)) {
328 $is_delete = true;
329 $is_affected = true;
330 } elseif (preg_match('@^(INSERT|LOAD[[:space:]]+DATA|REPLACE)[[:space:]]+@i', $sql_query)) {
331 $is_insert = true;
332 $is_affected = true;
333 } elseif (preg_match('@^UPDATE[[:space:]]+@i', $sql_query)) {
334 $is_affected = true;
335 } elseif (preg_match('@^SHOW[[:space:]]+@i', $sql_query)) {
336 $is_show = true;
337 } elseif (preg_match('@^(CHECK|ANALYZE|REPAIR|OPTIMIZE)[[:space:]]+TABLE[[:space:]]+@i', $sql_query)) {
338 $is_maint = true;
341 // Do append a "LIMIT" clause?
342 if (isset($pos)
343 && (!$cfg['ShowAll'] || $session_max_rows != 'all')
344 && !($is_count || $is_export || $is_func || $is_analyse)
345 && isset($analyzed_sql[0]['queryflags']['select_from'])
346 && !isset($analyzed_sql[0]['queryflags']['offset'])
347 && !preg_match('@[[:space:]]LIMIT[[:space:]0-9,-]+(;)?$@i', $sql_query)) {
348 $sql_limit_to_append = " LIMIT $pos, ".$cfg['MaxRows'] . " ";
350 $full_sql_query = $analyzed_sql[0]['section_before_limit'] . "\n" . $sql_limit_to_append . $analyzed_sql[0]['section_after_limit'];
351 // FIXME: pretty printing of this modified query
353 if (isset($display_query)) {
354 // if the analysis of the original query revealed that we found
355 // a section_after_limit, we now have to analyze $display_query
356 // to display it correctly
358 if (!empty($analyzed_sql[0]['section_after_limit']) && trim($analyzed_sql[0]['section_after_limit']) != ';') {
359 $analyzed_display_query = PMA_SQP_analyze(PMA_SQP_parse($display_query));
360 $display_query = $analyzed_display_query[0]['section_before_limit'] . "\n" . $sql_limit_to_append . $analyzed_display_query[0]['section_after_limit'];
364 } else {
365 $full_sql_query = $sql_query;
366 } // end if...else
368 if (isset($db)) {
369 PMA_DBI_select_db($db);
372 // If the query is a DELETE query with no WHERE clause, get the number of
373 // rows that will be deleted (mysql_affected_rows will always return 0 in
374 // this case)
375 // Note: testing shows that this no longer applies since MySQL 4.0.x
377 if (PMA_MYSQL_INT_VERSION < 40000) {
378 if ($is_delete
379 && preg_match('@^DELETE([[:space:]].+)?(FROM[[:space:]](.+))$@i', $sql_query, $parts)
380 && !preg_match('@[[:space:]]WHERE[[:space:]]@i', $parts[3])) {
381 $cnt_all_result = @PMA_DBI_try_query('SELECT COUNT(*) as count ' . $parts[2]);
382 if ($cnt_all_result) {
383 list($num_rows) = PMA_DBI_fetch_row($cnt_all_result);
384 PMA_DBI_free_result($cnt_all_result);
385 } else {
386 $num_rows = 0;
391 // E x e c u t e t h e q u e r y
393 // Only if we didn't ask to see the php code (mikebeck)
394 if (isset($GLOBALS['show_as_php']) || !empty($GLOBALS['validatequery'])) {
395 unset($result);
396 $num_rows = 0;
397 } else {
398 // garvin: Measure query time.
399 // TODO-Item http://sourceforge.net/tracker/index.php?func=detail&aid=571934&group_id=23067&atid=377411
400 $querytime_before = array_sum(explode(' ', microtime()));
402 $result = @PMA_DBI_try_query($full_sql_query, null, PMA_DBI_QUERY_STORE);
404 $querytime_after = array_sum(explode(' ', microtime()));
406 $GLOBALS['querytime'] = $querytime_after - $querytime_before;
408 // Displays an error message if required and stop parsing the script
409 if ($error = PMA_DBI_getError()) {
410 require_once './libraries/header.inc.php';
411 $full_err_url = (preg_match('@^(db_details|tbl_properties)@', $err_url))
412 ? $err_url . '&amp;show_query=1&amp;sql_query=' . urlencode($sql_query)
413 : $err_url;
414 PMA_mysqlDie($error, $full_sql_query, '', $full_err_url);
416 unset($error);
418 // Gets the number of rows affected/returned
419 // (This must be done immediately after the query because
420 // mysql_affected_rows() reports about the last query done)
422 if (!$is_affected) {
423 $num_rows = ($result) ? @PMA_DBI_num_rows($result) : 0;
424 } elseif (!isset($num_rows)) {
425 $num_rows = @PMA_DBI_affected_rows();
428 // Checks if the current database has changed
429 // This could happen if the user sends a query like "USE `database`;"
430 $res = PMA_DBI_query('SELECT DATABASE() AS \'db\';');
431 $row = PMA_DBI_fetch_row($res);
432 if (is_array($row) && isset($row[0]) && (strcasecmp($db, $row[0]) != 0)) {
433 $db = $row[0];
434 $reload = 1;
436 @PMA_DBI_free_result($res);
437 unset($res, $row);
439 // tmpfile remove after convert encoding appended by Y.Kawada
440 if (function_exists('PMA_kanji_file_conv')
441 && (isset($textfile) && file_exists($textfile))) {
442 unlink($textfile);
445 // Counts the total number of rows for the same 'SELECT' query without the
446 // 'LIMIT' clause that may have been programatically added
448 if (empty($sql_limit_to_append)) {
449 $unlim_num_rows = $num_rows;
450 // if we did not append a limit, set this to get a correct
451 // "Showing rows..." message
452 $GLOBALS['session_max_rows'] = 'all';
453 } elseif ($is_select) {
455 // c o u n t q u e r y
457 // If we are "just browsing", there is only one table,
458 // and no where clause (or just 'WHERE 1 '),
459 // so we do a quick count (which uses MaxExactCount)
460 // because SQL_CALC_FOUND_ROWS
461 // is not quick on large InnoDB tables
463 // but do not count again if we did it previously
464 // due to $find_real_end == true
466 if (!$is_group
467 && !isset($analyzed_sql[0]['queryflags']['union'])
468 && !isset($analyzed_sql[0]['table_ref'][1]['table_name'])
469 && (empty($analyzed_sql[0]['where_clause'])
470 || $analyzed_sql[0]['where_clause'] == '1 ')
471 && !isset($find_real_end)
474 // "j u s t b r o w s i n g"
475 $unlim_num_rows = PMA_Table::countRecords($db, $table, true);
477 } else { // n o t " j u s t b r o w s i n g "
479 if (PMA_MYSQL_INT_VERSION < 40000) {
481 // detect this case:
482 // SELECT DISTINCT x AS foo, y AS bar FROM sometable
484 if (isset($analyzed_sql[0]['queryflags']['distinct'])) {
485 $count_what = 'DISTINCT ';
486 $first_expr = true;
487 foreach ($analyzed_sql[0]['select_expr'] as $part) {
488 $count_what .= (!$first_expr ? ', ' : '') . $part['expr'];
489 $first_expr = false;
491 } else {
492 $count_what = '*';
494 // this one does not apply to VIEWs
495 $count_query = 'SELECT COUNT(' . $count_what . ') AS count';
498 // add the remaining of select expression if there is
499 // a GROUP BY or HAVING clause
500 if (PMA_MYSQL_INT_VERSION < 40000
501 && $count_what =='*'
502 && (!empty($analyzed_sql[0]['group_by_clause'])
503 || !empty($analyzed_sql[0]['having_clause']))) {
504 $count_query .= ' ,' . $analyzed_sql[0]['select_expr_clause'];
507 if (PMA_MYSQL_INT_VERSION >= 40000) {
508 // add select expression after the SQL_CALC_FOUND_ROWS
510 // for UNION, just adding SQL_CALC_FOUND_ROWS
511 // after the first SELECT works.
513 // take the left part, could be:
514 // SELECT
515 // (SELECT
516 $count_query = PMA_SQP_formatHtml($parsed_sql, 'query_only', 0, $analyzed_sql[0]['position_of_first_select'] + 1);
517 $count_query .= ' SQL_CALC_FOUND_ROWS ';
518 // add everything that was after the first SELECT
519 $count_query .= PMA_SQP_formatHtml($parsed_sql, 'query_only', $analyzed_sql[0]['position_of_first_select']+1);
520 } else { // PMA_MYSQL_INT_VERSION < 40000
522 if (!empty($analyzed_sql[0]['from_clause'])) {
523 $count_query .= ' FROM ' . $analyzed_sql[0]['from_clause'];
525 if (!empty($analyzed_sql[0]['where_clause'])) {
526 $count_query .= ' WHERE ' . $analyzed_sql[0]['where_clause'];
528 if (!empty($analyzed_sql[0]['group_by_clause'])) {
529 $count_query .= ' GROUP BY ' . $analyzed_sql[0]['group_by_clause'];
531 if (!empty($analyzed_sql[0]['having_clause'])) {
532 $count_query .= ' HAVING ' . $analyzed_sql[0]['having_clause'];
534 } // end if
536 // if using SQL_CALC_FOUND_ROWS, add a LIMIT to avoid
537 // long delays. Returned count will be complete anyway.
538 // (but a LIMIT would disrupt results in an UNION)
540 if (PMA_MYSQL_INT_VERSION >= 40000
541 && !isset($analyzed_sql[0]['queryflags']['union'])) {
542 $count_query .= ' LIMIT 1';
545 // run the count query
547 if (PMA_MYSQL_INT_VERSION < 40000) {
548 if ($cnt_all_result = PMA_DBI_try_query($count_query)) {
549 if ($is_group && $count_what == '*') {
550 $unlim_num_rows = @PMA_DBI_num_rows($cnt_all_result);
551 } else {
552 $unlim_num_rows = PMA_DBI_fetch_assoc($cnt_all_result);
553 $unlim_num_rows = $unlim_num_rows['count'];
555 PMA_DBI_free_result($cnt_all_result);
556 } else {
557 if (PMA_DBI_getError()) {
559 // there are some cases where the generated
560 // count_query (for MySQL 3) is wrong,
561 // so we get here.
562 //TODO: use a big unlimited query to get
563 // the correct number of rows (depending
564 // on a config variable?)
565 $unlim_num_rows = 0;
568 } else {
569 PMA_DBI_try_query($count_query);
570 // if (mysql_error()) {
571 // void.
572 // I tried the case
573 // (SELECT `User`, `Host`, `Db`, `Select_priv` FROM `db`)
574 // UNION (SELECT `User`, `Host`, "%" AS "Db",
575 // `Select_priv`
576 // FROM `user`) ORDER BY `User`, `Host`, `Db`;
577 // and although the generated count_query is wrong
578 // the SELECT FOUND_ROWS() work! (maybe it gets the
579 // count from the latest query that worked)
581 // another case where the count_query is wrong:
582 // SELECT COUNT(*), f1 from t1 group by f1
583 // and you click to sort on count(*)
584 // }
585 $cnt_all_result = PMA_DBI_query('SELECT FOUND_ROWS() as count;');
586 list($unlim_num_rows) = PMA_DBI_fetch_row($cnt_all_result);
587 @PMA_DBI_free_result($cnt_all_result);
589 } // end else "just browsing"
591 } else { // not $is_select
592 $unlim_num_rows = 0;
593 } // end rows total count
595 // garvin: if a table or database gets dropped, check column comments.
596 if (isset($purge) && $purge == '1') {
597 require_once './libraries/relation_cleanup.lib.php';
599 if (isset($table) && isset($db) && strlen($table) && strlen($db)) {
600 PMA_relationsCleanupTable($db, $table);
601 } elseif (isset($db) && strlen($db)) {
602 PMA_relationsCleanupDatabase($db);
603 } else {
604 // garvin: VOID. No DB/Table gets deleted.
605 } // end if relation-stuff
606 } // end if ($purge)
608 // garvin: If a column gets dropped, do relation magic.
609 if (isset($cpurge) && $cpurge == '1' && isset($purgekey)
610 && isset($db) && isset($table)
611 && strlen($db) && strlen($table) && !empty($purgekey)) {
612 require_once './libraries/relation_cleanup.lib.php';
613 PMA_relationsCleanupColumn($db, $table, $purgekey);
615 } // end if column PMA_* purge
616 } // end else "didn't ask to see php code"
618 // No rows returned -> move back to the calling page
619 if ($num_rows < 1 || $is_affected) {
620 if ($is_delete) {
621 $message = $strDeletedRows . '&nbsp;' . $num_rows;
622 } elseif ($is_insert) {
623 $message = $strInsertedRows . '&nbsp;' . $num_rows;
624 $insert_id = PMA_DBI_insert_id();
625 if ($insert_id != 0) {
626 // insert_id is id of FIRST record inserted in one insert, so if we inserted multiple rows, we had to increment this
627 $message .= '[br]'.$strInsertedRowId . '&nbsp;' . ($insert_id + $num_rows - 1);
629 } elseif ($is_affected) {
630 $message = $strAffectedRows . '&nbsp;' . $num_rows;
632 // Ok, here is an explanation for the !$is_select.
633 // The form generated by sql_query_form.lib.php
634 // and db_details.php has many submit buttons
635 // on the same form, and some confusion arises from the
636 // fact that $zero_rows is sent for every case.
637 // The $zero_rows containing $strSuccess and sent with
638 // the form should not have priority over
639 // errors like $strEmptyResultSet
640 } elseif (!empty($zero_rows) && !$is_select) {
641 $message = $zero_rows;
642 } elseif (!empty($GLOBALS['show_as_php'])) {
643 $message = $strPhp;
644 } elseif (!empty($GLOBALS['validatequery'])) {
645 $message = $strValidateSQL;
646 } else {
647 $message = $strEmptyResultSet;
650 $message .= ' ' . (isset($GLOBALS['querytime']) ? '(' . sprintf($strQueryTime, $GLOBALS['querytime']) . ')' : '');
652 if ($is_gotofile) {
653 $goto = PMA_securePath($goto);
654 // Checks for a valid target script
655 $is_db = $is_table = false;
656 include 'libraries/db_table_exists.lib.php';
657 if (strpos($goto, 'tbl_properties') === 0 && ! $is_table) {
658 if (isset($table)) {
659 unset($table);
661 $goto = 'db_details.php';
663 if (strpos($goto, 'db_details') === 0 && ! $is_db) {
664 if (isset($db)) {
665 unset($db);
667 $goto = 'main.php';
669 // Loads to target script
670 if (strpos($goto, 'db_details') === 0
671 || strpos($goto, 'tbl_properties') === 0) {
672 $js_to_run = 'functions.js';
674 if ($goto != 'main.php') {
675 require_once './libraries/header.inc.php';
677 $active_page = $goto;
678 require './' . $goto;
679 } else {
680 PMA_sendHeaderLocation($cfg['PmaAbsoluteUri'] . str_replace('&amp;', '&', $goto) . '&message=' . urlencode($message));
681 } // end else
682 exit();
683 } // end no rows returned
685 // At least one row is returned -> displays a table with results
686 else {
687 // Displays the headers
688 if (isset($show_query)) {
689 unset($show_query);
691 if (isset($printview) && $printview == '1') {
692 require_once './libraries/header_printview.inc.php';
693 } else {
694 $js_to_run = 'functions.js';
695 unset($message);
696 if (isset($table) && strlen($table)) {
697 require './libraries/tbl_properties_common.php';
698 $url_query .= '&amp;goto=tbl_properties.php&amp;back=tbl_properties.php';
699 require './libraries/tbl_properties_table_info.inc.php';
700 require './libraries/tbl_properties_links.inc.php';
701 } elseif (isset($db) && strlen($db)) {
702 require './libraries/db_details_common.inc.php';
703 require './libraries/db_details_db_info.inc.php';
704 } else {
705 require './libraries/server_common.inc.php';
706 require './libraries/server_links.inc.php';
710 if (isset($db) && strlen($db)) {
711 require_once './libraries/relation.lib.php';
712 $cfgRelation = PMA_getRelationsParam();
715 // Gets the list of fields properties
716 if (isset($result) && $result) {
717 $fields_meta = PMA_DBI_get_fields_meta($result);
718 $fields_cnt = count($fields_meta);
721 // Display previous update query (from tbl_replace)
722 if (isset($disp_query) && $cfg['ShowSQL'] == true) {
723 $tmp_sql_query = $GLOBALS['sql_query'];
724 $GLOBALS['sql_query'] = $disp_query;
725 PMA_showMessage($disp_message);
726 $GLOBALS['sql_query'] = $tmp_sql_query;
729 // Displays the results in a table
730 require_once './libraries/display_tbl.lib.php';
731 if (empty($disp_mode)) {
732 // see the "PMA_setDisplayMode()" function in
733 // libraries/display_tbl.lib.php
734 $disp_mode = 'urdr111101';
736 if (!isset($dontlimitchars)) {
737 $dontlimitchars = 0;
740 // hide edit and delete links for information_schema
741 if (PMA_MYSQL_INT_VERSION >= 50002 && isset($db) && $db == 'information_schema') {
742 $disp_mode = 'nnnn110111';
745 PMA_displayTable($result, $disp_mode, $analyzed_sql);
746 PMA_DBI_free_result($result);
748 // BEGIN INDEX CHECK See if indexes should be checked.
749 if (isset($query_type) && $query_type == 'check_tbl' && isset($selected) && is_array($selected)) {
750 foreach ($selected AS $idx => $tbl_name) {
751 $indexes = $indexes_info = $indexes_data = array();
752 $tbl_ret_keys = PMA_get_indexes(urldecode($tbl_name), $err_url_0);
754 PMA_extract_indexes($tbl_ret_keys, $indexes, $indexes_info, $indexes_data);
756 $idx_collection = PMA_show_indexes(urldecode($tbl_name), $indexes, $indexes_info, $indexes_data, false);
757 $check = PMA_check_indexes($idx_collection);
758 if (!empty($check)) {
760 <table border="0" cellpadding="2" cellspacing="0">
761 <tr>
762 <td class="tblHeaders" colspan="7"><?php printf($strIndexWarningTable, urldecode($tbl_name)); ?></td>
763 </tr>
764 <?php echo $check; ?>
765 </table>
766 <?php
769 } // End INDEX CHECK
771 // Bookmark Support if required
772 if ($disp_mode[7] == '1'
773 && (isset($cfg['Bookmark']) && $cfg['Bookmark']['db'] && $cfg['Bookmark']['table'] && empty($id_bookmark))
774 && !empty($sql_query)) {
775 echo "\n";
777 $goto = 'sql.php?'
778 . PMA_generate_common_url($db, $table)
779 . '&amp;pos=' . $pos
780 . '&amp;session_max_rows=' . $session_max_rows
781 . '&amp;disp_direction=' . $disp_direction
782 . '&amp;repeat_cells=' . $repeat_cells
783 . '&amp;dontlimitchars=' . $dontlimitchars
784 . '&amp;sql_query=' . urlencode($sql_query)
785 . '&amp;id_bookmark=1';
788 <form action="sql.php" method="post" onsubmit="return emptyFormElements(this, 'fields[label]');">
789 <?php echo PMA_generate_common_hidden_inputs(); ?>
790 <input type="hidden" name="goto" value="<?php echo $goto; ?>" />
791 <input type="hidden" name="fields[dbase]" value="<?php echo htmlspecialchars($db); ?>" />
792 <input type="hidden" name="fields[user]" value="<?php echo $cfg['Bookmark']['user']; ?>" />
793 <input type="hidden" name="fields[query]" value="<?php echo urlencode(isset($complete_query) ? $complete_query : $sql_query); ?>" />
794 <fieldset>
795 <legend><?php
796 echo ($cfg['PropertiesIconic'] ? '<img class="icon" src="' . $pmaThemeImage . 'b_bookmark.png" width="16" height="16" alt="' . $strBookmarkThis . '" />' : '')
797 . $strBookmarkThis;
799 </legend>
801 <div class="formelement">
802 <label for="fields_label_"><?php echo $strBookmarkLabel; ?>:</label>
803 <input type="text" id="fields_label_" name="fields[label]" value="" />
804 </div>
806 <div class="formelement">
807 <input type="checkbox" name="bkm_all_users" id="bkm_all_users" value="true" />
808 <label for="bkm_all_users"><?php echo $strBookmarkAllUsers; ?></label>
809 </div>
811 <div class="clearfloat"></div>
812 </fieldset>
813 <fieldset class="tblFooters">
814 <input type="submit" name="store_bkm" value="<?php echo $strBookmarkThis; ?>" />
815 </fieldset>
816 </form>
817 <?php
818 } // end bookmark support
820 // Do print the page if required
821 if (isset($printview) && $printview == '1') {
823 <script type="text/javascript" language="javascript">
824 //<![CDATA[
825 // Do print the page
826 window.onload = function()
828 if (typeof(window.print) != 'undefined') {
829 window.print();
832 //]]>
833 </script>
834 <?php
835 } // end print case
836 } // end rows returned
838 } // end executes the query
841 * Displays the footer
843 require_once './libraries/footer.inc.php';