Add phpdoc header comment.
[phpmyadmin/last10db.git] / sql.php
blob18410c31165fecb354c5e4e8b46931476b1bfdb6
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 * Parse and analyze the query
136 require_once('./libraries/parse_analyze.lib.php');
139 * Sets or modifies the $goto variable if required
141 if ($goto == 'sql.php') {
142 $is_gotofile = false;
143 $goto = 'sql.php?'
144 . PMA_generate_common_url($db, $table)
145 . '&amp;pos=' . $pos
146 . '&amp;sql_query=' . urlencode($sql_query);
147 } // end if
151 * Go back to further page if table should not be dropped
153 if (isset($btnDrop) && $btnDrop == $strNo) {
154 if (!empty($back)) {
155 $goto = $back;
157 if ($is_gotofile) {
158 if (strpos(' ' . $goto, 'db_details') == 1 && isset($table) && strlen($table)) {
159 unset($table);
161 $active_page = $goto;
162 require './' . PMA_securePath($goto);
163 } else {
164 PMA_sendHeaderLocation($cfg['PmaAbsoluteUri'] . str_replace('&amp;', '&', $goto));
166 exit();
167 } // end if
171 * Displays the confirm page if required
173 * This part of the script is bypassed if $is_js_confirmed = 1 (already checked
174 * with js) because possible security issue is not so important here: at most,
175 * the confirm message isn't displayed.
177 * Also bypassed if only showing php code.or validating a SQL query
179 if (!$cfg['Confirm']
180 || (isset($is_js_confirmed) && $is_js_confirmed)
181 || isset($btnDrop)
183 // if we are coming from a "Create PHP code" or a "Without PHP Code"
184 // dialog, we won't execute the query anyway, so don't confirm
185 //|| !empty($GLOBALS['show_as_php'])
186 || isset($GLOBALS['show_as_php'])
188 || !empty($GLOBALS['validatequery'])) {
189 $do_confirm = false;
190 } else {
191 $do_confirm = isset($analyzed_sql[0]['queryflags']['need_confirm']);
194 if ($do_confirm) {
195 $stripped_sql_query = $sql_query;
196 require_once './libraries/header.inc.php';
197 if ($is_drop_database) {
198 echo '<h1 class="warning">' . $strDropDatabaseStrongWarning . '</h1>';
200 echo '<form action="sql.php" method="post">' . "\n"
201 .PMA_generate_common_hidden_inputs($db, (isset($table)?$table:''));
203 <input type="hidden" name="sql_query" value="<?php echo urlencode($sql_query); ?>" />
204 <input type="hidden" name="zero_rows" value="<?php echo isset($zero_rows) ? PMA_sanitize($zero_rows) : ''; ?>" />
205 <input type="hidden" name="goto" value="<?php echo $goto; ?>" />
206 <input type="hidden" name="back" value="<?php echo isset($back) ? PMA_sanitize($back) : ''; ?>" />
207 <input type="hidden" name="reload" value="<?php echo isset($reload) ? PMA_sanitize($reload) : 0; ?>" />
208 <input type="hidden" name="purge" value="<?php echo isset($purge) ? PMA_sanitize($purge) : ''; ?>" />
209 <input type="hidden" name="cpurge" value="<?php echo isset($cpurge) ? PMA_sanitize($cpurge) : ''; ?>" />
210 <input type="hidden" name="purgekey" value="<?php echo isset($purgekey) ? PMA_sanitize($purgekey) : ''; ?>" />
211 <input type="hidden" name="show_query" value="<?php echo isset($show_query) ? PMA_sanitize($show_query) : ''; ?>" />
212 <?php
213 echo '<fieldset class="confirmation">' . "\n"
214 .' <legend>' . $strDoYouReally . '</legend>'
215 .' <tt>' . htmlspecialchars($stripped_sql_query) . '</tt>' . "\n"
216 .'</fieldset>' . "\n"
217 .'<fieldset class="tblFooters">' . "\n";
219 <input type="submit" name="btnDrop" value="<?php echo $strYes; ?>" id="buttonYes" />
220 <input type="submit" name="btnDrop" value="<?php echo $strNo; ?>" id="buttonNo" />
221 <?php
222 echo '</fieldset>' . "\n"
223 . '</form>' . "\n";
224 } // end if $do_confirm
228 * Executes the query and displays results
230 else {
231 if (!isset($sql_query)) {
232 $sql_query = '';
234 // Defines some variables
235 // A table has to be created or renamed -> left frame should be reloaded
236 // TODO: use the parser/analyzer
238 if (empty($reload)
239 && preg_match('/^(CREATE|ALTER|DROP)\s+(VIEW|TABLE|DATABASE|SCHEMA)\s+/i', $sql_query)) {
240 $reload = 1;
242 // Gets the number of rows per page
243 if (empty($session_max_rows)) {
244 $session_max_rows = $cfg['MaxRows'];
245 } elseif ($session_max_rows != 'all') {
246 $cfg['MaxRows'] = $session_max_rows;
248 // Defines the display mode (horizontal/vertical) and header "frequency"
249 if (empty($disp_direction)) {
250 $disp_direction = $cfg['DefaultDisplay'];
252 if (empty($repeat_cells)) {
253 $repeat_cells = $cfg['RepeatCells'];
256 // SK -- Patch: $is_group added for use in calculation of total number of
257 // rows.
258 // $is_count is changed for more correct "LIMIT" clause
259 // appending in queries like
260 // "SELECT COUNT(...) FROM ... GROUP BY ..."
262 // TODO: detect all this with the parser, to avoid problems finding
263 // those strings in comments or backquoted identifiers
265 $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;
266 if ($is_select) { // see line 141
267 $is_group = preg_match('@(GROUP[[:space:]]+BY|HAVING|SELECT[[:space:]]+DISTINCT)[[:space:]]+@i', $sql_query);
268 $is_func = !$is_group && (preg_match('@[[:space:]]+(SUM|AVG|STD|STDDEV|MIN|MAX|BIT_OR|BIT_AND)\s*\(@i', $sql_query));
269 $is_count = !$is_group && (preg_match('@^SELECT[[:space:]]+COUNT\((.*\.+)?.*\)@i', $sql_query));
270 $is_export = (preg_match('@[[:space:]]+INTO[[:space:]]+OUTFILE[[:space:]]+@i', $sql_query));
271 $is_analyse = (preg_match('@[[:space:]]+PROCEDURE[[:space:]]+ANALYSE@i', $sql_query));
272 } elseif (preg_match('@^EXPLAIN[[:space:]]+@i', $sql_query)) {
273 $is_explain = true;
274 } elseif (preg_match('@^DELETE[[:space:]]+@i', $sql_query)) {
275 $is_delete = true;
276 $is_affected = true;
277 } elseif (preg_match('@^(INSERT|LOAD[[:space:]]+DATA|REPLACE)[[:space:]]+@i', $sql_query)) {
278 $is_insert = true;
279 $is_affected = true;
280 if (preg_match('@^(REPLACE)[[:space:]]+@i', $sql_query)) {
281 $is_replace = true;
283 } elseif (preg_match('@^UPDATE[[:space:]]+@i', $sql_query)) {
284 $is_affected = true;
285 } elseif (preg_match('@^SHOW[[:space:]]+@i', $sql_query)) {
286 $is_show = true;
287 } elseif (preg_match('@^(CHECK|ANALYZE|REPAIR|OPTIMIZE)[[:space:]]+TABLE[[:space:]]+@i', $sql_query)) {
288 $is_maint = true;
291 // Do append a "LIMIT" clause?
292 if (isset($pos)
293 && (!$cfg['ShowAll'] || $session_max_rows != 'all')
294 && !($is_count || $is_export || $is_func || $is_analyse)
295 && isset($analyzed_sql[0]['queryflags']['select_from'])
296 && !isset($analyzed_sql[0]['queryflags']['offset'])
297 && !preg_match('@[[:space:]]LIMIT[[:space:]0-9,-]+(;)?$@i', $sql_query)) {
298 $sql_limit_to_append = " LIMIT $pos, ".$cfg['MaxRows'] . " ";
300 $full_sql_query = $analyzed_sql[0]['section_before_limit'] . "\n" . $sql_limit_to_append . $analyzed_sql[0]['section_after_limit'];
301 // FIXME: pretty printing of this modified query
303 if (isset($display_query)) {
304 // if the analysis of the original query revealed that we found
305 // a section_after_limit, we now have to analyze $display_query
306 // to display it correctly
308 if (!empty($analyzed_sql[0]['section_after_limit']) && trim($analyzed_sql[0]['section_after_limit']) != ';') {
309 $analyzed_display_query = PMA_SQP_analyze(PMA_SQP_parse($display_query));
310 $display_query = $analyzed_display_query[0]['section_before_limit'] . "\n" . $sql_limit_to_append . $analyzed_display_query[0]['section_after_limit'];
314 } else {
315 $full_sql_query = $sql_query;
316 } // end if...else
318 if (isset($db)) {
319 PMA_DBI_select_db($db);
322 // If the query is a DELETE query with no WHERE clause, get the number of
323 // rows that will be deleted (mysql_affected_rows will always return 0 in
324 // this case)
325 // Note: testing shows that this no longer applies since MySQL 4.0.x
327 if (PMA_MYSQL_INT_VERSION < 40000) {
328 if ($is_delete
329 && preg_match('@^DELETE([[:space:]].+)?(FROM[[:space:]](.+))$@i', $sql_query, $parts)
330 && !preg_match('@[[:space:]]WHERE[[:space:]]@i', $parts[3])) {
331 $cnt_all_result = @PMA_DBI_try_query('SELECT COUNT(*) as count ' . $parts[2]);
332 if ($cnt_all_result) {
333 list($num_rows) = PMA_DBI_fetch_row($cnt_all_result);
334 PMA_DBI_free_result($cnt_all_result);
335 } else {
336 $num_rows = 0;
341 // E x e c u t e t h e q u e r y
343 // Only if we didn't ask to see the php code (mikebeck)
344 if (isset($GLOBALS['show_as_php']) || !empty($GLOBALS['validatequery'])) {
345 unset($result);
346 $num_rows = 0;
347 } else {
348 // garvin: Measure query time.
349 // TODO-Item http://sourceforge.net/tracker/index.php?func=detail&aid=571934&group_id=23067&atid=377411
350 $querytime_before = array_sum(explode(' ', microtime()));
352 $result = @PMA_DBI_try_query($full_sql_query, null, PMA_DBI_QUERY_STORE);
354 $querytime_after = array_sum(explode(' ', microtime()));
356 $GLOBALS['querytime'] = $querytime_after - $querytime_before;
358 // Displays an error message if required and stop parsing the script
359 if ($error = PMA_DBI_getError()) {
360 require_once './libraries/header.inc.php';
361 $full_err_url = (preg_match('@^(db_details|tbl_properties)@', $err_url))
362 ? $err_url . '&amp;show_query=1&amp;sql_query=' . urlencode($sql_query)
363 : $err_url;
364 PMA_mysqlDie($error, $full_sql_query, '', $full_err_url);
366 unset($error);
368 // Gets the number of rows affected/returned
369 // (This must be done immediately after the query because
370 // mysql_affected_rows() reports about the last query done)
372 if (!$is_affected) {
373 $num_rows = ($result) ? @PMA_DBI_num_rows($result) : 0;
374 } elseif (!isset($num_rows)) {
375 $num_rows = @PMA_DBI_affected_rows();
378 // Checks if the current database has changed
379 // This could happen if the user sends a query like "USE `database`;"
380 $res = PMA_DBI_query('SELECT DATABASE() AS \'db\';');
381 $row = PMA_DBI_fetch_row($res);
382 if (isset($db) && is_array($row) && isset($row[0]) && (strcasecmp($db, $row[0]) != 0)) {
383 $db = $row[0];
384 $reload = 1;
386 @PMA_DBI_free_result($res);
387 unset($res, $row);
389 // tmpfile remove after convert encoding appended by Y.Kawada
390 if (function_exists('PMA_kanji_file_conv')
391 && (isset($textfile) && file_exists($textfile))) {
392 unlink($textfile);
395 // Counts the total number of rows for the same 'SELECT' query without the
396 // 'LIMIT' clause that may have been programatically added
398 if (empty($sql_limit_to_append)) {
399 $unlim_num_rows = $num_rows;
400 // if we did not append a limit, set this to get a correct
401 // "Showing rows..." message
402 $GLOBALS['session_max_rows'] = 'all';
403 } elseif ($is_select) {
405 // c o u n t q u e r y
407 // If we are "just browsing", there is only one table,
408 // and no where clause (or just 'WHERE 1 '),
409 // so we do a quick count (which uses MaxExactCount)
410 // because SQL_CALC_FOUND_ROWS
411 // is not quick on large InnoDB tables
413 // but do not count again if we did it previously
414 // due to $find_real_end == true
416 if (!$is_group
417 && !isset($analyzed_sql[0]['queryflags']['union'])
418 && !isset($analyzed_sql[0]['table_ref'][1]['table_name'])
419 && (empty($analyzed_sql[0]['where_clause'])
420 || $analyzed_sql[0]['where_clause'] == '1 ')
421 && !isset($find_real_end)
424 // "j u s t b r o w s i n g"
425 $unlim_num_rows = PMA_Table::countRecords($db, $table, true);
427 } else { // n o t " j u s t b r o w s i n g "
429 if (PMA_MYSQL_INT_VERSION < 40000) {
431 // detect this case:
432 // SELECT DISTINCT x AS foo, y AS bar FROM sometable
434 if (isset($analyzed_sql[0]['queryflags']['distinct'])) {
435 $count_what = 'DISTINCT ';
436 $first_expr = true;
437 foreach ($analyzed_sql[0]['select_expr'] as $part) {
438 $count_what .= (!$first_expr ? ', ' : '') . $part['expr'];
439 $first_expr = false;
441 } else {
442 $count_what = '*';
444 // this one does not apply to VIEWs
445 $count_query = 'SELECT COUNT(' . $count_what . ') AS count';
448 // add the remaining of select expression if there is
449 // a GROUP BY or HAVING clause
450 if (PMA_MYSQL_INT_VERSION < 40000
451 && $count_what =='*'
452 && (!empty($analyzed_sql[0]['group_by_clause'])
453 || !empty($analyzed_sql[0]['having_clause']))) {
454 $count_query .= ' ,' . $analyzed_sql[0]['select_expr_clause'];
457 if (PMA_MYSQL_INT_VERSION >= 40000) {
458 // add select expression after the SQL_CALC_FOUND_ROWS
460 // for UNION, just adding SQL_CALC_FOUND_ROWS
461 // after the first SELECT works.
463 // take the left part, could be:
464 // SELECT
465 // (SELECT
466 $count_query = PMA_SQP_formatHtml($parsed_sql, 'query_only', 0, $analyzed_sql[0]['position_of_first_select'] + 1);
467 $count_query .= ' SQL_CALC_FOUND_ROWS ';
468 // add everything that was after the first SELECT
469 $count_query .= PMA_SQP_formatHtml($parsed_sql, 'query_only', $analyzed_sql[0]['position_of_first_select']+1);
470 // ensure there is no semicolon at the end of the
471 // count query because we'll probably add
472 // a LIMIT 1 clause after it
473 $count_query = rtrim($count_query);
474 $count_query = rtrim($count_query, ';');
475 } else { // PMA_MYSQL_INT_VERSION < 40000
477 if (!empty($analyzed_sql[0]['from_clause'])) {
478 $count_query .= ' FROM ' . $analyzed_sql[0]['from_clause'];
480 if (!empty($analyzed_sql[0]['where_clause'])) {
481 $count_query .= ' WHERE ' . $analyzed_sql[0]['where_clause'];
483 if (!empty($analyzed_sql[0]['group_by_clause'])) {
484 $count_query .= ' GROUP BY ' . $analyzed_sql[0]['group_by_clause'];
486 if (!empty($analyzed_sql[0]['having_clause'])) {
487 $count_query .= ' HAVING ' . $analyzed_sql[0]['having_clause'];
489 } // end if
491 // if using SQL_CALC_FOUND_ROWS, add a LIMIT to avoid
492 // long delays. Returned count will be complete anyway.
493 // (but a LIMIT would disrupt results in an UNION)
495 if (PMA_MYSQL_INT_VERSION >= 40000
496 && !isset($analyzed_sql[0]['queryflags']['union'])) {
497 $count_query .= ' LIMIT 1';
500 // run the count query
502 if (PMA_MYSQL_INT_VERSION < 40000) {
503 if ($cnt_all_result = PMA_DBI_try_query($count_query)) {
504 if ($is_group && $count_what == '*') {
505 $unlim_num_rows = @PMA_DBI_num_rows($cnt_all_result);
506 } else {
507 $unlim_num_rows = PMA_DBI_fetch_assoc($cnt_all_result);
508 $unlim_num_rows = $unlim_num_rows['count'];
510 PMA_DBI_free_result($cnt_all_result);
511 } else {
512 if (PMA_DBI_getError()) {
514 // there are some cases where the generated
515 // count_query (for MySQL 3) is wrong,
516 // so we get here.
517 //TODO: use a big unlimited query to get
518 // the correct number of rows (depending
519 // on a config variable?)
520 $unlim_num_rows = 0;
523 } else {
524 PMA_DBI_try_query($count_query);
525 // if (mysql_error()) {
526 // void.
527 // I tried the case
528 // (SELECT `User`, `Host`, `Db`, `Select_priv` FROM `db`)
529 // UNION (SELECT `User`, `Host`, "%" AS "Db",
530 // `Select_priv`
531 // FROM `user`) ORDER BY `User`, `Host`, `Db`;
532 // and although the generated count_query is wrong
533 // the SELECT FOUND_ROWS() work! (maybe it gets the
534 // count from the latest query that worked)
536 // another case where the count_query is wrong:
537 // SELECT COUNT(*), f1 from t1 group by f1
538 // and you click to sort on count(*)
539 // }
540 $cnt_all_result = PMA_DBI_query('SELECT FOUND_ROWS() as count;');
541 list($unlim_num_rows) = PMA_DBI_fetch_row($cnt_all_result);
542 @PMA_DBI_free_result($cnt_all_result);
544 } // end else "just browsing"
546 } else { // not $is_select
547 $unlim_num_rows = 0;
548 } // end rows total count
550 // garvin: if a table or database gets dropped, check column comments.
551 if (isset($purge) && $purge == '1') {
552 require_once './libraries/relation_cleanup.lib.php';
554 if (isset($table) && isset($db) && strlen($table) && strlen($db)) {
555 PMA_relationsCleanupTable($db, $table);
556 } elseif (isset($db) && strlen($db)) {
557 PMA_relationsCleanupDatabase($db);
558 } else {
559 // garvin: VOID. No DB/Table gets deleted.
560 } // end if relation-stuff
561 } // end if ($purge)
563 // garvin: If a column gets dropped, do relation magic.
564 if (isset($cpurge) && $cpurge == '1' && isset($purgekey)
565 && isset($db) && isset($table)
566 && strlen($db) && strlen($table) && !empty($purgekey)) {
567 require_once './libraries/relation_cleanup.lib.php';
568 PMA_relationsCleanupColumn($db, $table, $purgekey);
570 } // end if column PMA_* purge
571 } // end else "didn't ask to see php code"
573 // No rows returned -> move back to the calling page
574 if ($num_rows < 1 || $is_affected) {
575 if ($is_delete) {
576 $message = $strDeletedRows . '&nbsp;' . $num_rows;
577 } elseif ($is_insert) {
578 if ($is_replace) {
579 /* For replace we get DELETED + INSERTED row count, so we have to call it affected */
580 $message = $strAffectedRows . '&nbsp;' . $num_rows;
581 } else {
582 $message = $strInsertedRows . '&nbsp;' . $num_rows;
584 $insert_id = PMA_DBI_insert_id();
585 if ($insert_id != 0) {
586 // insert_id is id of FIRST record inserted in one insert, so if we inserted multiple rows, we had to increment this
587 $message .= '[br]'.$strInsertedRowId . '&nbsp;' . ($insert_id + $num_rows - 1);
589 } elseif ($is_affected) {
590 $message = $strAffectedRows . '&nbsp;' . $num_rows;
592 // Ok, here is an explanation for the !$is_select.
593 // The form generated by sql_query_form.lib.php
594 // and db_details.php has many submit buttons
595 // on the same form, and some confusion arises from the
596 // fact that $zero_rows is sent for every case.
597 // The $zero_rows containing $strSuccess and sent with
598 // the form should not have priority over
599 // errors like $strEmptyResultSet
600 } elseif (!empty($zero_rows) && !$is_select) {
601 $message = $zero_rows;
602 } elseif (!empty($GLOBALS['show_as_php'])) {
603 $message = $strPhp;
604 } elseif (!empty($GLOBALS['validatequery'])) {
605 $message = $strValidateSQL;
606 } else {
607 $message = $strEmptyResultSet;
610 $message .= ' ' . (isset($GLOBALS['querytime']) ? '(' . sprintf($strQueryTime, $GLOBALS['querytime']) . ')' : '');
612 if ($is_gotofile) {
613 $goto = PMA_securePath($goto);
614 // Checks for a valid target script
615 $is_db = $is_table = false;
616 include 'libraries/db_table_exists.lib.php';
617 if (strpos($goto, 'tbl_properties') === 0 && ! $is_table) {
618 if (isset($table)) {
619 unset($table);
621 $goto = 'db_details.php';
623 if (strpos($goto, 'db_details') === 0 && ! $is_db) {
624 if (isset($db)) {
625 unset($db);
627 $goto = 'main.php';
629 // Loads to target script
630 if (strpos($goto, 'db_details') === 0
631 || strpos($goto, 'tbl_properties') === 0) {
632 $js_to_run = 'functions.js';
634 if ($goto != 'main.php') {
635 require_once './libraries/header.inc.php';
637 $active_page = $goto;
638 require './' . $goto;
639 } else {
640 PMA_sendHeaderLocation($cfg['PmaAbsoluteUri'] . str_replace('&amp;', '&', $goto) . '&message=' . urlencode($message));
641 } // end else
642 exit();
643 } // end no rows returned
645 // At least one row is returned -> displays a table with results
646 else {
647 // Displays the headers
648 if (isset($show_query)) {
649 unset($show_query);
651 if (isset($printview) && $printview == '1') {
652 require_once './libraries/header_printview.inc.php';
653 } else {
654 $js_to_run = 'functions.js';
655 unset($message);
656 if (isset($table) && strlen($table)) {
657 require './libraries/tbl_properties_common.php';
658 $url_query .= '&amp;goto=tbl_properties.php&amp;back=tbl_properties.php';
659 require './libraries/tbl_properties_table_info.inc.php';
660 require './libraries/tbl_properties_links.inc.php';
661 } elseif (isset($db) && strlen($db)) {
662 require './libraries/db_details_common.inc.php';
663 require './libraries/db_details_db_info.inc.php';
664 } else {
665 require './libraries/server_common.inc.php';
666 require './libraries/server_links.inc.php';
670 if (isset($db) && strlen($db)) {
671 require_once './libraries/relation.lib.php';
672 $cfgRelation = PMA_getRelationsParam();
675 // Gets the list of fields properties
676 if (isset($result) && $result) {
677 $fields_meta = PMA_DBI_get_fields_meta($result);
678 $fields_cnt = count($fields_meta);
681 // Display previous update query (from tbl_replace)
682 if (isset($disp_query) && $cfg['ShowSQL'] == true) {
683 $tmp_sql_query = $GLOBALS['sql_query'];
684 $GLOBALS['sql_query'] = $disp_query;
685 PMA_showMessage($disp_message);
686 $GLOBALS['sql_query'] = $tmp_sql_query;
689 // Displays the results in a table
690 require_once './libraries/display_tbl.lib.php';
691 if (empty($disp_mode)) {
692 // see the "PMA_setDisplayMode()" function in
693 // libraries/display_tbl.lib.php
694 $disp_mode = 'urdr111101';
696 if (!isset($dontlimitchars)) {
697 $dontlimitchars = 0;
700 // hide edit and delete links for information_schema
701 if (PMA_MYSQL_INT_VERSION >= 50002 && isset($db) && $db == 'information_schema') {
702 $disp_mode = 'nnnn110111';
705 PMA_displayTable($result, $disp_mode, $analyzed_sql);
706 PMA_DBI_free_result($result);
708 // BEGIN INDEX CHECK See if indexes should be checked.
709 if (isset($query_type) && $query_type == 'check_tbl' && isset($selected) && is_array($selected)) {
710 foreach ($selected AS $idx => $tbl_name) {
711 $indexes = $indexes_info = $indexes_data = array();
712 $tbl_ret_keys = PMA_get_indexes(urldecode($tbl_name), $err_url_0);
714 PMA_extract_indexes($tbl_ret_keys, $indexes, $indexes_info, $indexes_data);
716 $idx_collection = PMA_show_indexes(urldecode($tbl_name), $indexes, $indexes_info, $indexes_data, false);
717 $check = PMA_check_indexes($idx_collection);
718 if (!empty($check)) {
720 <table border="0" cellpadding="2" cellspacing="0">
721 <tr>
722 <td class="tblHeaders" colspan="7"><?php printf($strIndexWarningTable, urldecode($tbl_name)); ?></td>
723 </tr>
724 <?php echo $check; ?>
725 </table>
726 <?php
729 } // End INDEX CHECK
731 // Bookmark Support if required
732 if ($disp_mode[7] == '1'
733 && (isset($cfg['Bookmark']) && $cfg['Bookmark']['db'] && $cfg['Bookmark']['table'] && empty($id_bookmark))
734 && !empty($sql_query)) {
735 echo "\n";
737 $goto = 'sql.php?'
738 . PMA_generate_common_url($db, $table)
739 . '&amp;pos=' . $pos
740 . '&amp;session_max_rows=' . $session_max_rows
741 . '&amp;disp_direction=' . $disp_direction
742 . '&amp;repeat_cells=' . $repeat_cells
743 . '&amp;dontlimitchars=' . $dontlimitchars
744 . '&amp;sql_query=' . urlencode($sql_query)
745 . '&amp;id_bookmark=1';
748 <form action="sql.php" method="post" onsubmit="return emptyFormElements(this, 'fields[label]');">
749 <?php echo PMA_generate_common_hidden_inputs(); ?>
750 <input type="hidden" name="goto" value="<?php echo $goto; ?>" />
751 <input type="hidden" name="fields[dbase]" value="<?php echo htmlspecialchars($db); ?>" />
752 <input type="hidden" name="fields[user]" value="<?php echo $cfg['Bookmark']['user']; ?>" />
753 <input type="hidden" name="fields[query]" value="<?php echo urlencode(isset($complete_query) ? $complete_query : $sql_query); ?>" />
754 <fieldset>
755 <legend><?php
756 echo ($cfg['PropertiesIconic'] ? '<img class="icon" src="' . $pmaThemeImage . 'b_bookmark.png" width="16" height="16" alt="' . $strBookmarkThis . '" />' : '')
757 . $strBookmarkThis;
759 </legend>
761 <div class="formelement">
762 <label for="fields_label_"><?php echo $strBookmarkLabel; ?>:</label>
763 <input type="text" id="fields_label_" name="fields[label]" value="" />
764 </div>
766 <div class="formelement">
767 <input type="checkbox" name="bkm_all_users" id="bkm_all_users" value="true" />
768 <label for="bkm_all_users"><?php echo $strBookmarkAllUsers; ?></label>
769 </div>
771 <div class="clearfloat"></div>
772 </fieldset>
773 <fieldset class="tblFooters">
774 <input type="submit" name="store_bkm" value="<?php echo $strBookmarkThis; ?>" />
775 </fieldset>
776 </form>
777 <?php
778 } // end bookmark support
780 // Do print the page if required
781 if (isset($printview) && $printview == '1') {
783 <script type="text/javascript" language="javascript">
784 //<![CDATA[
785 // Do print the page
786 window.onload = function()
788 if (typeof(window.print) != 'undefined') {
789 window.print();
792 //]]>
793 </script>
794 <?php
795 } // end print case
796 } // end rows returned
798 } // end executes the query
801 * Displays the footer
803 require_once './libraries/footer.inc.php';