sorry, wrong version checked in
[phpmyadmin/arisferyanto.git] / sql.php
blob93c6b17cea341b2eec984d6081c4013237474e12
1 <?php
2 /* $Id$ */
3 // vim: expandtab sw=4 ts=4 sts=4:
5 /**
6 * Gets some core libraries
7 */
8 require_once('./libraries/common.lib.php');
9 require_once('./libraries/tbl_indexes.lib.php');
10 require_once('./libraries/check_user_privileges.lib.php');
11 require_once('./libraries/bookmark.lib.php');
13 /**
14 * Defines the url to return to in case of error in a sql statement
16 // Security checkings
17 if (!empty($goto)) {
18 $is_gotofile = preg_replace('@^([^?]+).*$@s', '\\1', $goto);
19 if (!@file_exists('./' . $is_gotofile)) {
20 unset($goto);
21 } else {
22 $is_gotofile = ($is_gotofile == $goto);
24 } // end if (security checkings)
26 if (empty($goto)) {
27 $goto = (empty($table)) ? $cfg['DefaultTabDatabase'] : $cfg['DefaultTabTable'];
28 $is_gotofile = TRUE;
29 } // end if
30 if (!isset($err_url)) {
31 $err_url = (!empty($back) ? $back : $goto)
32 . '?' . PMA_generate_common_url(isset($db) ? $db : '')
33 . ((strpos(' ' . $goto, 'db_details') != 1 && isset($table)) ? '&amp;table=' . urlencode($table) : '');
34 } // end if
36 // Coming from a bookmark dialog
37 if (isset($fields['query'])) {
38 $sql_query = $fields['query'];
41 // This one is just to fill $db
42 if (isset($fields['dbase'])) {
43 $db = $fields['dbase'];
46 // Now we can check the parameters
47 PMA_checkParameters(array('sql_query'));
49 // instead of doing the test twice
50 $is_drop_database = preg_match('@DROP[[:space:]]+DATABASE[[:space:]]+@i', $sql_query);
52 /**
53 * Check rights in case of DROP DATABASE
55 * This test may be bypassed if $is_js_confirmed = 1 (already checked with js)
56 * but since a malicious user may pass this variable by url/form, we don't take
57 * into account this case.
59 if (!defined('PMA_CHK_DROP')
60 && !$cfg['AllowUserDropDatabase']
61 && $is_drop_database
62 && !$is_superuser) {
63 require_once('./header.inc.php');
64 PMA_mysqlDie($strNoDropDatabases, '', '', $err_url);
65 } // end if
68 /**
69 * Need to find the real end of rows?
72 if (isset($find_real_end) && $find_real_end) {
73 $unlim_num_rows = PMA_countRecords($db, $table, TRUE, TRUE);
74 $pos = @((ceil($unlim_num_rows / $session_max_rows) - 1) * $session_max_rows);
77 /**
78 * Bookmark add
80 if (isset($store_bkm)) {
81 PMA_addBookmarks($fields, $cfg['Bookmark'], (isset($bkm_all_users) && $bkm_all_users == 'true' ? true : false));
82 PMA_sendHeaderLocation($cfg['PmaAbsoluteUri'] . $goto);
83 } // end if
86 /**
87 * Gets the true sql query
89 // $sql_query has been urlencoded in the confirmation form for drop/delete
90 // queries or in the navigation bar for browsing among records
91 if (isset($btnDrop) || isset($navig)) {
92 $sql_query = urldecode($sql_query);
95 /**
96 * Reformat the query
99 $GLOBALS['unparsed_sql'] = $sql_query;
100 $parsed_sql = PMA_SQP_parse($sql_query);
101 $analyzed_sql = PMA_SQP_analyze($parsed_sql);
103 // Bug #641765 - Robbat2 - 12 January 2003, 10:49PM
104 // Reverted - Robbat2 - 13 January 2003, 2:40PM
106 // lem9: for bug 780516: now that we use case insensitive preg_match
107 // or flags from the analyser, do not put back the reformatted query
108 // into $sql_query, to make this kind of query work without
109 // capitalizing keywords:
111 // CREATE TABLE SG_Persons (
112 // id int(10) unsigned NOT NULL auto_increment,
113 // first varchar(64) NOT NULL default '',
114 // PRIMARY KEY (`id`)
115 // )
117 // Note: now we probably do not need to fill and use $GLOBALS['unparsed_sql']
118 // but I let this intact for now.
120 //$sql_query = PMA_SQP_formatHtml($parsed_sql, 'query_only');
123 // check for a real SELECT ... FROM
124 $is_select = isset($analyzed_sql[0]['queryflags']['select_from']);
126 // If the query is a Select, extract the db and table names and modify
127 // $db and $table, to have correct page headers, links and left frame.
128 // db and table name may be enclosed with backquotes, db is optionnal,
129 // query may contain aliases.
131 // (TODO: if there are more than one table name in the Select:
132 // - do not extract the first table name
133 // - do not show a table name in the page header
134 // - do not display the sub-pages links)
136 if ($is_select) {
137 $prev_db = $db;
138 if (isset($analyzed_sql[0]['table_ref'][0]['table_true_name'])) {
139 $table = $analyzed_sql[0]['table_ref'][0]['table_true_name'];
141 if (isset($analyzed_sql[0]['table_ref'][0]['db'])
142 && !empty($analyzed_sql[0]['table_ref'][0]['db'])) {
143 $db = $analyzed_sql[0]['table_ref'][0]['db'];
145 else {
146 $db = $prev_db;
148 // Nijel: don't change reload, if we already decided to reload in import
149 if ( empty( $reload ) ) {
150 $reload = ($db == $prev_db) ? 0 : 1;
155 * Sets or modifies the $goto variable if required
157 if ($goto == 'sql.php') {
158 $is_gotofile = FALSE;
159 $goto = 'sql.php?'
160 . PMA_generate_common_url($db, $table)
161 . '&amp;pos=' . $pos
162 . '&amp;sql_query=' . urlencode($sql_query);
163 } // end if
167 * Go back to further page if table should not be dropped
169 if (isset($btnDrop) && $btnDrop == $strNo) {
170 if (!empty($back)) {
171 $goto = $back;
173 if ($is_gotofile) {
174 if (strpos(' ' . $goto, 'db_details') == 1 && !empty($table)) {
175 unset($table);
177 $active_page = $goto;
178 require('./' . PMA_securePath($goto));
179 } else {
180 PMA_sendHeaderLocation($cfg['PmaAbsoluteUri'] . str_replace('&amp;', '&', $goto));
182 exit();
183 } // end if
187 * Displays the confirm page if required
189 * This part of the script is bypassed if $is_js_confirmed = 1 (already checked
190 * with js) because possible security issue is not so important here: at most,
191 * the confirm message isn't displayed.
193 * Also bypassed if only showing php code.or validating a SQL query
195 if (!$cfg['Confirm']
196 || (isset($is_js_confirmed) && $is_js_confirmed)
197 || isset($btnDrop)
199 // if we are coming from a "Create PHP code" or a "Without PHP Code"
200 // dialog, we won't execute the query anyway, so don't confirm
201 //|| !empty($GLOBALS['show_as_php'])
202 || isset($GLOBALS['show_as_php'])
204 || !empty($GLOBALS['validatequery'])) {
205 $do_confirm = FALSE;
206 } else {
207 $do_confirm = isset($analyzed_sql[0]['queryflags']['need_confirm']);
210 if ( $do_confirm ) {
211 $stripped_sql_query = $sql_query;
212 require_once('./header.inc.php');
213 if ( $is_drop_database) {
214 echo '<h1 class="warning">' . $strDropDatabaseStrongWarning . '</h1>';
216 echo '<form action="sql.php" method="post">' . "\n"
217 .PMA_generate_common_hidden_inputs($db, (isset($table)?$table:''));
219 <input type="hidden" name="sql_query" value="<?php echo urlencode($sql_query); ?>" />
220 <input type="hidden" name="zero_rows" value="<?php echo isset($zero_rows) ? PMA_sanitize($zero_rows) : ''; ?>" />
221 <input type="hidden" name="goto" value="<?php echo $goto; ?>" />
222 <input type="hidden" name="back" value="<?php echo isset($back) ? PMA_sanitize($back) : ''; ?>" />
223 <input type="hidden" name="reload" value="<?php echo isset($reload) ? PMA_sanitize($reload) : 0; ?>" />
224 <input type="hidden" name="purge" value="<?php echo isset($purge) ? PMA_sanitize($purge) : ''; ?>" />
225 <input type="hidden" name="cpurge" value="<?php echo isset($cpurge) ? PMA_sanitize($cpurge) : ''; ?>" />
226 <input type="hidden" name="purgekey" value="<?php echo isset($purgekey) ? PMA_sanitize($purgekey) : ''; ?>" />
227 <input type="hidden" name="show_query" value="<?php echo isset($show_query) ? PMA_sanitize($show_query) : ''; ?>" />
228 <?php
229 echo '<fieldset class="confirmation">' . "\n"
230 .' <legend>' . $strDoYouReally . '</legend>'
231 .' <tt>' . htmlspecialchars( $stripped_sql_query ) . '</tt>' . "\n"
232 .'</fieldset>' . "\n"
233 .'<fieldset class="tblFooters">' . "\n";
235 <input type="submit" name="btnDrop" value="<?php echo $strYes; ?>" id="buttonYes" />
236 <input type="submit" name="btnDrop" value="<?php echo $strNo; ?>" id="buttonNo" />
237 <?php
238 echo '</fieldset>' . "\n"
239 . '</form>' . "\n";
240 } // end if $do_confirm
244 * Executes the query and displays results
246 else {
247 if (!isset($sql_query)) {
248 $sql_query = '';
250 // Defines some variables
251 // A table has to be created or renamed -> left frame should be reloaded
252 // TODO: use the parser/analyzer
254 if ( empty( $reload )
255 && (preg_match('@^CREATE (VIEW|TABLE)\s+@i', $sql_query)
256 || preg_match('@^ALTER TABLE\s+.*RENAME@i', $sql_query))) {
257 $reload = 1;
259 // Gets the number of rows per page
260 if (empty($session_max_rows)) {
261 $session_max_rows = $cfg['MaxRows'];
262 } else if ($session_max_rows != 'all') {
263 $cfg['MaxRows'] = $session_max_rows;
265 // Defines the display mode (horizontal/vertical) and header "frequency"
266 if (empty($disp_direction)) {
267 $disp_direction = $cfg['DefaultDisplay'];
269 if (empty($repeat_cells)) {
270 $repeat_cells = $cfg['RepeatCells'];
273 // SK -- Patch: $is_group added for use in calculation of total number of
274 // rows.
275 // $is_count is changed for more correct "LIMIT" clause
276 // appending in queries like
277 // "SELECT COUNT(...) FROM ... GROUP BY ..."
279 // TODO: detect all this with the parser, to avoid problems finding
280 // those strings in comments or backquoted identifiers
282 $is_explain = $is_count = $is_export = $is_delete = $is_insert = $is_affected = $is_show = $is_maint = $is_analyse = $is_group = $is_func = FALSE;
283 if ($is_select) { // see line 141
284 $is_group = preg_match('@(GROUP[[:space:]]+BY|HAVING|SELECT[[:space:]]+DISTINCT)[[:space:]]+@i', $sql_query);
285 $is_func = !$is_group && (preg_match('@[[:space:]]+(SUM|AVG|STD|STDDEV|MIN|MAX|BIT_OR|BIT_AND)\s*\(@i', $sql_query));
286 $is_count = !$is_group && (preg_match('@^SELECT[[:space:]]+COUNT\((.*\.+)?.*\)@i', $sql_query));
287 $is_export = (preg_match('@[[:space:]]+INTO[[:space:]]+OUTFILE[[:space:]]+@i', $sql_query));
288 $is_analyse = (preg_match('@[[:space:]]+PROCEDURE[[:space:]]+ANALYSE@i', $sql_query));
289 } else if (preg_match('@^EXPLAIN[[:space:]]+@i', $sql_query)) {
290 $is_explain = TRUE;
291 } else if (preg_match('@^DELETE[[:space:]]+@i', $sql_query)) {
292 $is_delete = TRUE;
293 $is_affected = TRUE;
294 } else if (preg_match('@^(INSERT|LOAD[[:space:]]+DATA|REPLACE)[[:space:]]+@i', $sql_query)) {
295 $is_insert = TRUE;
296 $is_affected = TRUE;
297 } else if (preg_match('@^UPDATE[[:space:]]+@i', $sql_query)) {
298 $is_affected = TRUE;
299 } else if (preg_match('@^SHOW[[:space:]]+@i', $sql_query)) {
300 $is_show = TRUE;
301 } else if (preg_match('@^(CHECK|ANALYZE|REPAIR|OPTIMIZE)[[:space:]]+TABLE[[:space:]]+@i', $sql_query)) {
302 $is_maint = TRUE;
305 // Do append a "LIMIT" clause?
306 if (isset($pos)
307 && (!$cfg['ShowAll'] || $session_max_rows != 'all')
308 && !($is_count || $is_export || $is_func || $is_analyse)
309 && isset($analyzed_sql[0]['queryflags']['select_from'])
310 && !isset($analyzed_sql[0]['queryflags']['offset'])
311 && !preg_match('@[[:space:]]LIMIT[[:space:]0-9,-]+$@i', $sql_query)) {
312 $sql_limit_to_append = " LIMIT $pos, ".$cfg['MaxRows'] . " ";
314 $full_sql_query = $analyzed_sql[0]['section_before_limit'] . "\n" . $sql_limit_to_append . $analyzed_sql[0]['section_after_limit'];
315 // FIXME: pretty printing of this modified query
317 if (isset($display_query)) {
318 // if the analysis of the original query revealed that we found
319 // a section_after_limit, we now have to analyze $display_query
320 // to display it correctly
322 if (!empty($analyzed_sql[0]['section_after_limit'])) {
323 $analyzed_display_query = PMA_SQP_analyze(PMA_SQP_parse($display_query));
324 $display_query = $analyzed_display_query[0]['section_before_limit'] . "\n" . $sql_limit_to_append . $analyzed_display_query[0]['section_after_limit'];
328 } else {
329 $full_sql_query = $sql_query;
330 } // end if...else
332 if (isset($db)) {
333 PMA_DBI_select_db($db);
336 // If the query is a DELETE query with no WHERE clause, get the number of
337 // rows that will be deleted (mysql_affected_rows will always return 0 in
338 // this case)
340 if ($is_delete
341 && preg_match('@^DELETE([[:space:]].+)?(FROM[[:space:]](.+))$@i', $sql_query, $parts)
342 && !preg_match('@[[:space:]]WHERE[[:space:]]@i', $parts[3])) {
343 $cnt_all_result = @PMA_DBI_try_query('SELECT COUNT(*) as count ' . $parts[2]);
344 if ($cnt_all_result) {
345 list($num_rows) = PMA_DBI_fetch_row($cnt_all_result);
346 PMA_DBI_free_result($cnt_all_result);
347 } else {
348 $num_rows = 0;
352 // E x e c u t e t h e q u e r y
354 // Only if we didn't ask to see the php code (mikebeck)
355 if (isset($GLOBALS['show_as_php']) || !empty($GLOBALS['validatequery'])) {
356 unset($result);
357 $num_rows = 0;
359 else {
360 // garvin: Measure query time. TODO-Item http://sourceforge.net/tracker/index.php?func=detail&aid=571934&group_id=23067&atid=377411
361 list($usec, $sec) = explode(' ',microtime());
362 $querytime_before = ((float)$usec + (float)$sec);
364 $result = @PMA_DBI_try_query($full_sql_query, NULL, PMA_DBI_QUERY_STORE);
366 list($usec, $sec) = explode(' ',microtime());
367 $querytime_after = ((float)$usec + (float)$sec);
369 $GLOBALS['querytime'] = $querytime_after - $querytime_before;
371 // Displays an error message if required and stop parsing the script
372 if ($error = PMA_DBI_getError()) {
373 require_once('./header.inc.php');
374 $full_err_url = (preg_match('@^(db_details|tbl_properties)@', $err_url))
375 ? $err_url . '&amp;show_query=1&amp;sql_query=' . urlencode($sql_query)
376 : $err_url;
377 PMA_mysqlDie($error, $full_sql_query, '', $full_err_url);
379 unset($error);
381 // Gets the number of rows affected/returned
382 // (This must be done immediately after the query because
383 // mysql_affected_rows() reports about the last query done)
385 if (!$is_affected) {
386 $num_rows = ($result) ? @PMA_DBI_num_rows($result) : 0;
387 } else if (!isset($num_rows)) {
388 $num_rows = @PMA_DBI_affected_rows();
391 // Checks if the current database has changed
392 // This could happen if the user sends a query like "USE `database`;"
393 $res = PMA_DBI_query('SELECT DATABASE() AS \'db\';');
394 $row = PMA_DBI_fetch_row($res);
395 if (is_array($row) && isset($row[0]) && (strcasecmp($db,$row[0]) != 0)) {
396 $db = $row[0];
397 $reload = 1;
399 @PMA_DBI_free_result($res);
400 unset($res, $row);
402 // tmpfile remove after convert encoding appended by Y.Kawada
403 if (function_exists('PMA_kanji_file_conv')
404 && (isset($textfile) && file_exists($textfile))) {
405 unlink($textfile);
408 // Counts the total number of rows for the same 'SELECT' query without the
409 // 'LIMIT' clause that may have been programatically added
411 if (empty($sql_limit_to_append)) {
412 $unlim_num_rows = $num_rows;
413 // if we did not append a limit, set this to get a correct
414 // "Showing rows..." message
415 $GLOBALS['session_max_rows'] = 'all';
417 else if ($is_select) {
419 // c o u n t q u e r y
421 // If we are "just browsing", there is only one table,
422 // and no where clause (or just 'WHERE 1 '),
423 // so we do a quick count (which uses MaxExactCount)
424 // because SQL_CALC_FOUND_ROWS
425 // is not quick on large InnoDB tables
427 // but do not count again if we did it previously
428 // due to $find_real_end == TRUE
430 if (!$is_group
431 && !isset($analyzed_sql[0]['queryflags']['union'])
432 && !isset($analyzed_sql[0]['table_ref'][1]['table_name'])
433 && (empty($analyzed_sql[0]['where_clause'])
434 || $analyzed_sql[0]['where_clause'] == '1 ')
435 && !isset($find_real_end)
438 // "j u s t b r o w s i n g"
439 $unlim_num_rows = PMA_countRecords($db, $table, TRUE);
441 } else { // n o t " j u s t b r o w s i n g "
443 if (PMA_MYSQL_INT_VERSION < 40000) {
445 // detect this case:
446 // SELECT DISTINCT x AS foo, y AS bar FROM sometable
448 if (isset($analyzed_sql[0]['queryflags']['distinct'])) {
449 $count_what = 'DISTINCT ';
450 $first_expr = TRUE;
451 foreach($analyzed_sql[0]['select_expr'] as $part) {
452 $count_what .= (!$first_expr ? ', ' : '') . $part['expr'];
453 $first_expr = FALSE;
455 } else {
456 $count_what = '*';
458 $count_query = 'SELECT COUNT(' . $count_what . ') AS count';
461 // add the remaining of select expression if there is
462 // a GROUP BY or HAVING clause
463 if (PMA_MYSQL_INT_VERSION < 40000
464 && $count_what =='*'
465 && (!empty($analyzed_sql[0]['group_by_clause'])
466 || !empty($analyzed_sql[0]['having_clause']))) {
467 $count_query .= ' ,' . $analyzed_sql[0]['select_expr_clause'];
470 if (PMA_MYSQL_INT_VERSION >= 40000) {
471 // add select expression after the SQL_CALC_FOUND_ROWS
473 // for UNION, just adding SQL_CALC_FOUND_ROWS
474 // after the first SELECT works.
476 // take the left part, could be:
477 // SELECT
478 // (SELECT
479 $count_query = PMA_SQP_formatHtml($parsed_sql, 'query_only', 0, $analyzed_sql[0]['position_of_first_select'] + 1);
480 $count_query .= ' SQL_CALC_FOUND_ROWS ';
481 // add everything that was after the first SELECT
482 $count_query .= PMA_SQP_formatHtml($parsed_sql, 'query_only', $analyzed_sql[0]['position_of_first_select']+1);
483 } else { // PMA_MYSQL_INT_VERSION < 40000
485 if (!empty($analyzed_sql[0]['from_clause'])) {
486 $count_query .= ' FROM ' . $analyzed_sql[0]['from_clause'];
488 if (!empty($analyzed_sql[0]['where_clause'])) {
489 $count_query .= ' WHERE ' . $analyzed_sql[0]['where_clause'];
491 if (!empty($analyzed_sql[0]['group_by_clause'])) {
492 $count_query .= ' GROUP BY ' . $analyzed_sql[0]['group_by_clause'];
494 if (!empty($analyzed_sql[0]['having_clause'])) {
495 $count_query .= ' HAVING ' . $analyzed_sql[0]['having_clause'];
497 } // end if
499 // if using SQL_CALC_FOUND_ROWS, add a LIMIT to avoid
500 // long delays. Returned count will be complete anyway.
501 // (but a LIMIT would disrupt results in an UNION)
503 if (PMA_MYSQL_INT_VERSION >= 40000
504 && !isset($analyzed_sql[0]['queryflags']['union'])) {
505 $count_query .= ' LIMIT 1';
508 // run the count query
510 if (PMA_MYSQL_INT_VERSION < 40000) {
511 if ($cnt_all_result = PMA_DBI_try_query($count_query)) {
512 if ($is_group && $count_what == '*') {
513 $unlim_num_rows = @PMA_DBI_num_rows($cnt_all_result);
514 } else {
515 $unlim_num_rows = PMA_DBI_fetch_assoc($cnt_all_result);
516 $unlim_num_rows = $unlim_num_rows['count'];
518 PMA_DBI_free_result($cnt_all_result);
519 } else {
520 if (PMA_DBI_getError()) {
522 // there are some cases where the generated
523 // count_query (for MySQL 3) is wrong,
524 // so we get here.
525 //TODO: use a big unlimited query to get
526 // the correct number of rows (depending
527 // on a config variable?)
528 $unlim_num_rows = 0;
531 } else {
532 PMA_DBI_try_query($count_query);
533 // if (mysql_error()) {
534 // void.
535 // I tried the case
536 // (SELECT `User`, `Host`, `Db`, `Select_priv` FROM `db`)
537 // UNION (SELECT `User`, `Host`, "%" AS "Db",
538 // `Select_priv`
539 // FROM `user`) ORDER BY `User`, `Host`, `Db`;
540 // and although the generated count_query is wrong
541 // the SELECT FOUND_ROWS() work! (maybe it gets the
542 // count from the latest query that worked)
544 // another case where the count_query is wrong:
545 // SELECT COUNT( * ), f1 from t1 group by f1
546 // and you click to sort on count( * )
547 // }
548 $cnt_all_result = PMA_DBI_query('SELECT FOUND_ROWS() as count;');
549 list($unlim_num_rows) = PMA_DBI_fetch_row($cnt_all_result);
550 @PMA_DBI_free_result($cnt_all_result);
552 } // end else "just browsing"
554 } else { // not $is_select
555 $unlim_num_rows = 0;
556 } // end rows total count
558 // garvin: if a table or database gets dropped, check column comments.
559 if (isset($purge) && $purge == '1') {
560 require_once('./libraries/relation_cleanup.lib.php');
562 if (isset($table) && isset($db) && !empty($table) && !empty($db)) {
563 PMA_relationsCleanupTable($db, $table);
564 } elseif (isset($db) && !empty($db)) {
565 PMA_relationsCleanupDatabase($db);
566 } else {
567 // garvin: VOID. No DB/Table gets deleted.
568 } // end if relation-stuff
569 } // end if ($purge)
571 // garvin: If a column gets dropped, do relation magic.
572 if (isset($cpurge) && $cpurge == '1' && isset($purgekey)
573 && isset($db) && isset($table)
574 && !empty($db) && !empty($table) && !empty($purgekey)) {
575 require_once('./libraries/relation_cleanup.lib.php');
576 PMA_relationsCleanupColumn($db, $table, $purgekey);
578 } // end if column PMA_* purge
579 } // end else "didn't ask to see php code"
581 // No rows returned -> move back to the calling page
582 if ($num_rows < 1 || $is_affected) {
583 if ($is_delete) {
584 $message = $strDeletedRows . '&nbsp;' . $num_rows;
585 } else if ($is_insert) {
586 $message = $strInsertedRows . '&nbsp;' . $num_rows;
587 $insert_id = PMA_DBI_insert_id();
588 if ($insert_id != 0) {
589 // insert_id is id of FIRST record inserted in one insert, so if we inserted multiple rows, we had to increment this
590 $message .= '[br]'.$strInsertedRowId . '&nbsp;' . ($insert_id + $num_rows - 1);
592 } else if ($is_affected) {
593 $message = $strAffectedRows . '&nbsp;' . $num_rows;
595 // Ok, here is an explanation for the !$is_select.
596 // The form generated by sql_query_form.lib.php
597 // and db_details.php has many submit buttons
598 // on the same form, and some confusion arises from the
599 // fact that $zero_rows is sent for every case.
600 // The $zero_rows containing $strSuccess and sent with
601 // the form should not have priority over
602 // errors like $strEmptyResultSet
603 } else if (!empty($zero_rows) && !$is_select) {
604 $message = $zero_rows;
605 } else if (!empty($GLOBALS['show_as_php'])) {
606 $message = $strPhp;
607 } else if (!empty($GLOBALS['validatequery'])) {
608 $message = $strValidateSQL;
609 } else {
610 $message = $strEmptyResultSet;
613 $message .= ' ' . (isset($GLOBALS['querytime']) ? '(' . sprintf($strQueryTime, $GLOBALS['querytime']) . ')' : '');
615 if ($is_gotofile) {
616 $goto = PMA_securePath($goto);
617 // Checks for a valid target script
618 if (isset($table) && $table == '') {
619 unset($table);
621 if (isset($db) && $db == '') {
622 unset($db);
624 $is_db = $is_table = FALSE;
625 if (strpos(' ' . $goto, 'tbl_properties') == 1) {
626 if (!isset($table)) {
627 $goto = 'db_details.php';
628 } else {
629 $is_table = @PMA_DBI_query('SHOW TABLES LIKE \'' . PMA_sqlAddslashes($table, TRUE) . '\';', NULL, PMA_DBI_QUERY_STORE);
630 if (!($is_table && @PMA_DBI_num_rows($is_table))) {
631 $goto = 'db_details.php';
632 unset($table);
634 @PMA_DBI_free_result($is_table);
635 } // end if... else...
637 if (strpos(' ' . $goto, 'db_details') == 1) {
638 if (isset($table)) {
639 unset($table);
641 if (!isset($db)) {
642 $goto = 'main.php';
643 } else {
644 $is_db = @PMA_DBI_select_db($db);
645 if (!$is_db) {
646 $goto = 'main.php';
647 unset($db);
649 } // end if... else...
651 // Loads to target script
652 if (strpos(' ' . $goto, 'db_details') == 1
653 || strpos(' ' . $goto, 'tbl_properties') == 1) {
654 $js_to_run = 'functions.js';
656 if ($goto != 'main.php') {
657 require_once('./header.inc.php');
659 $active_page = $goto;
660 require('./' . $goto);
661 } // end if file_exist
662 else {
663 PMA_sendHeaderLocation($cfg['PmaAbsoluteUri'] . str_replace('&amp;', '&', $goto) . '&message=' . urlencode($message));
664 } // end else
665 exit();
666 } // end no rows returned
668 // At least one row is returned -> displays a table with results
669 else {
670 // Displays the headers
671 if (isset($show_query)) {
672 unset($show_query);
674 if (isset($printview) && $printview == '1') {
675 require_once('./header_printview.inc.php');
676 } else {
677 $js_to_run = 'functions.js';
678 unset($message);
679 if (!empty($table)) {
680 require('./tbl_properties_common.php');
681 $url_query .= '&amp;goto=tbl_properties.php&amp;back=tbl_properties.php';
682 require('./tbl_properties_table_info.php');
683 require('./tbl_properties_links.php');
685 elseif (!empty($db)) {
686 require('./db_details_common.php');
687 require('./db_details_db_info.php');
689 else {
690 require('./server_common.inc.php');
691 require('./server_links.inc.php');
695 if (!empty($db)) {
696 require_once('./libraries/relation.lib.php');
697 $cfgRelation = PMA_getRelationsParam();
700 // Gets the list of fields properties
701 if (isset($result) && $result) {
702 $fields_meta = PMA_DBI_get_fields_meta($result);
703 $fields_cnt = count($fields_meta);
706 // Display previous update query (from tbl_replace)
707 if (isset($disp_query) && $cfg['ShowSQL'] == TRUE) {
708 $tmp_sql_query = $GLOBALS['sql_query'];
709 $GLOBALS['sql_query'] = $disp_query;
710 PMA_showMessage($disp_message);
711 $GLOBALS['sql_query'] = $tmp_sql_query;
714 // Displays the results in a table
715 require_once('./libraries/display_tbl.lib.php');
716 if (empty($disp_mode)) {
717 // see the "PMA_setDisplayMode()" function in
718 // libraries/display_tbl.lib.php
719 $disp_mode = 'urdr111101';
721 if (!isset($dontlimitchars)) {
722 $dontlimitchars = 0;
725 PMA_displayTable($result, $disp_mode, $analyzed_sql);
726 PMA_DBI_free_result($result);
728 // BEGIN INDEX CHECK See if indexes should be checked.
729 if (isset($query_type) && $query_type == 'check_tbl' && isset($selected) && is_array($selected)) {
730 foreach($selected AS $idx => $tbl_name) {
731 $indexes = $indexes_info = $indexes_data = array();
732 $tbl_ret_keys = PMA_get_indexes(urldecode($tbl_name), $err_url_0);
734 PMA_extract_indexes($tbl_ret_keys, $indexes, $indexes_info, $indexes_data);
736 $idx_collection = PMA_show_indexes(urldecode($tbl_name), $indexes, $indexes_info, $indexes_data, false);
737 $check = PMA_check_indexes($idx_collection);
738 if (!empty($check)) {
740 <table border="0" cellpadding="2" cellspacing="0">
741 <tr>
742 <td class="tblHeaders" colspan="7"><?php printf($strIndexWarningTable, urldecode($tbl_name)); ?></td>
743 </tr>
744 <?php echo $check; ?>
745 </table>
746 <?php
749 } // End INDEX CHECK
751 if ($disp_mode[6] == '1' || $disp_mode[9] == '1') {
752 echo "\n";
753 echo '<hr />' . "\n";
755 // Displays "Insert a new row" link if required
756 if ($disp_mode[6] == '1') {
757 $lnk_goto = 'sql.php?'
758 . PMA_generate_common_url($db, $table)
759 . '&amp;pos=' . $pos
760 . '&amp;session_max_rows=' . $session_max_rows
761 . '&amp;disp_direction=' . $disp_direction
762 . '&amp;repeat_cells=' . $repeat_cells
763 . '&amp;dontlimitchars=' . $dontlimitchars
764 . '&amp;sql_query=' . urlencode($sql_query);
765 $url_query = '?'
766 . PMA_generate_common_url($db, $table)
767 . '&amp;pos=' . $pos
768 . '&amp;session_max_rows=' . $session_max_rows
769 . '&amp;disp_direction=' . $disp_direction
770 . '&amp;repeat_cells=' . $repeat_cells
771 . '&amp;dontlimitchars=' . $dontlimitchars
772 . '&amp;sql_query=' . urlencode($sql_query)
773 . '&amp;goto=' . urlencode($lnk_goto);
775 echo ' <!-- Insert a new row -->' . "\n";
776 echo PMA_linkOrButton(
777 'tbl_change.php' . $url_query,
778 ($cfg['PropertiesIconic'] ? '<img class="icon" src="' . $pmaThemeImage . 'b_insrow.png" height="16" width="16" alt="' . $strInsertNewRow . '"/>' : '') . $strInsertNewRow,
779 '', TRUE, TRUE, '') . "\n";
781 if ($disp_mode[9] == '1') {
782 echo '&nbsp;&nbsp;';
784 echo "\n";
785 } // end insert new row
787 // Displays "printable view" link if required
788 if ($disp_mode[9] == '1') {
789 $url_query = '?'
790 . PMA_generate_common_url($db, $table)
791 . '&amp;pos=' . $pos
792 . '&amp;session_max_rows=' . $session_max_rows
793 . '&amp;disp_direction=' . $disp_direction
794 . '&amp;repeat_cells=' . $repeat_cells
795 . '&amp;printview=1'
796 . '&amp;sql_query=' . urlencode($sql_query);
797 echo ' <!-- Print view -->' . "\n";
798 echo PMA_linkOrButton(
799 'sql.php' . $url_query . ((isset($dontlimitchars) && $dontlimitchars == '1') ? '&amp;dontlimitchars=1' : ''),
800 ($cfg['PropertiesIconic'] ? '<img class="icon" src="' . $pmaThemeImage . 'b_print.png" height="16" width="16" alt="' . $strPrintView . '"/>' : '') . $strPrintView,
801 '', TRUE, TRUE, 'print_view') . "\n";
803 if (!$dontlimitchars) {
804 echo ' &nbsp;&nbsp;' . "\n";
805 echo PMA_linkOrButton(
806 'sql.php' . $url_query . '&amp;dontlimitchars=1',
807 ($cfg['PropertiesIconic'] ? '<img class="icon" src="' . $pmaThemeImage . 'b_print.png" height="16" width="16" alt="' . $strPrintViewFull . '"/>' : '') . $strPrintViewFull,
808 '', TRUE, TRUE, 'print_view') . "\n";
810 } // end displays "printable view"
812 echo "\n";
815 // Export link
816 // (the url_query has extra parameters that won't be used to export)
817 // (the single_table parameter is used in display_export.lib.php
818 // to hide the SQL and the structure export dialogs)
819 if (isset($analyzed_sql[0]) && $analyzed_sql[0]['querytype'] == 'SELECT' && !isset($printview)) {
820 if (isset($analyzed_sql[0]['table_ref'][0]['table_true_name']) && !isset($analyzed_sql[0]['table_ref'][1]['table_true_name'])) {
821 $single_table = '&amp;single_table=true';
822 } else {
823 $single_table = '';
825 echo ' <!-- Export -->' . "\n";
826 echo ' &nbsp;&nbsp;' . "\n";
827 echo PMA_linkOrButton(
828 'tbl_properties_export.php' . $url_query . '&amp;unlim_num_rows=' . $unlim_num_rows . $single_table,
829 ($cfg['PropertiesIconic'] ? '<img class="icon" src="' . $pmaThemeImage . 'b_tblexport.png" height="16" width="16" alt="' . $strExport . '" />' : '') . $strExport,
830 '', TRUE, TRUE, '') . "\n";
833 // Bookmark Support if required
834 if ($disp_mode[7] == '1'
835 && ( isset( $cfg['Bookmark'] ) && $cfg['Bookmark']['db'] && $cfg['Bookmark']['table'] && empty($id_bookmark))
836 && !empty($sql_query)) {
837 echo "\n";
839 $goto = 'sql.php?'
840 . PMA_generate_common_url($db, $table)
841 . '&amp;pos=' . $pos
842 . '&amp;session_max_rows=' . $session_max_rows
843 . '&amp;disp_direction=' . $disp_direction
844 . '&amp;repeat_cells=' . $repeat_cells
845 . '&amp;dontlimitchars=' . $dontlimitchars
846 . '&amp;sql_query=' . urlencode($sql_query)
847 . '&amp;id_bookmark=1';
849 if ($disp_mode[3] == '1') {
850 echo ' <i>' . $strOr . '</i>';
851 } else {
852 echo '<br /><br />';
855 <form action="sql.php" method="post" onsubmit="return emptyFormElements(this, 'fields[label]');">
856 <?php echo PMA_generate_common_hidden_inputs(); ?>
857 <input type="hidden" name="goto" value="<?php echo $goto; ?>" />
858 <input type="hidden" name="fields[dbase]" value="<?php echo htmlspecialchars($db); ?>" />
859 <input type="hidden" name="fields[user]" value="<?php echo $cfg['Bookmark']['user']; ?>" />
860 <input type="hidden" name="fields[query]" value="<?php echo urlencode(isset($complete_query) ? $complete_query : $sql_query); ?>" />
861 <fieldset>
862 <legend><?php
863 echo ($cfg['PropertiesIconic'] ? '<img class="icon" src="' . $pmaThemeImage . 'b_bookmark.png" width="16" height="16" alt="' . $strBookmarkThis . '" />' : '')
864 . $strBookmarkThis;
866 </legend>
868 <div class="formelement">
869 <label for="fields_label_"><?php echo $strBookmarkLabel; ?>:</label>
870 <input type="text" id="fields_label_" name="fields[label]" value="" />
871 </div>
873 <div class="formelement">
874 <input type="checkbox" name="bkm_all_users" id="bkm_all_users" value="true" />
875 <label for="bkm_all_users"><?php echo $strBookmarkAllUsers; ?></label>
876 </div>
877 </fieldset>
878 <fieldset class="tblFooters">
879 <input type="submit" name="store_bkm" value="<?php echo $strBookmarkThis; ?>" />
880 </fieldset>
881 </form>
882 <?php
883 } // end bookmark support
885 // Do print the page if required
886 if (isset($printview) && $printview == '1') {
887 echo "\n";
889 <script type="text/javascript" language="javascript">
890 <!--
891 // Do print the page
892 if (typeof(window.print) != 'undefined') {
893 window.print();
895 //-->
896 </script>
897 <?php
898 } // end print case
899 } // end rows returned
901 } // end executes the query
902 echo "\n\n";
905 * Displays the footer
907 require_once('./footer.inc.php');