Documentation TYPO3 par Ameos

sql.php

00001 <?php
00002 /* $Id: sql.php,v 2.53 2005/08/12 01:06:18 lem9 Exp $ */
00003 // vim: expandtab sw=4 ts=4 sts=4:
00004 
00008 require_once('./libraries/grab_globals.lib.php');
00009 require_once('./libraries/common.lib.php');
00010 require_once('./libraries/tbl_indexes.lib.php');
00011 
00015 // Security checkings
00016 if (!empty($goto)) {
00017     $is_gotofile     = preg_replace('@^([^?]+).*$@s', '\\1', $goto);
00018     if (!@file_exists('./' . $is_gotofile)) {
00019         unset($goto);
00020     } else {
00021         $is_gotofile = ($is_gotofile == $goto);
00022     }
00023 } // end if (security checkings)
00024 
00025 if (empty($goto)) {
00026     $goto         = (empty($table)) ? $cfg['DefaultTabDatabase'] : $cfg['DefaultTabTable'];
00027     $is_gotofile  = TRUE;
00028 } // end if
00029 if (!isset($err_url)) {
00030     $err_url = (!empty($back) ? $back : $goto)
00031              . '?' . PMA_generate_common_url(isset($db) ? $db : '')
00032              . ((strpos(' ' . $goto, 'db_details') != 1 && isset($table)) ? '&amp;table=' . urlencode($table) : '');
00033 } // end if
00034 
00035 // Coming from a bookmark dialog
00036 if (isset($fields['query'])) {
00037     $sql_query = $fields['query'];
00038 }
00039 
00040 // This one is just to fill $db
00041 if (isset($fields['dbase'])) {
00042     $db = $fields['dbase'];
00043 }
00044 
00045 // Now we can check the parameters
00046 PMA_checkParameters(array('sql_query', 'db'));
00047 
00048 // instead of doing the test twice
00049 $is_drop_database = preg_match('@DROP[[:space:]]+DATABASE[[:space:]]+@i', $sql_query);
00050 
00058 if (!defined('PMA_CHK_DROP')
00059     && !$cfg['AllowUserDropDatabase']
00060     && $is_drop_database) {
00061     // Checks if the user is a Superuser
00062     // TODO: set a global variable with this information
00063     // loic1: optimized query
00064     if (!($result = PMA_DBI_select_db('mysql'))) {
00065         require_once('./header.inc.php');
00066         PMA_mysqlDie($strNoDropDatabases, '', '', $err_url);
00067     } // end if
00068 } // end if
00069 
00070 
00075 if (isset($find_real_end) && $find_real_end) {
00076     $unlim_num_rows = PMA_countRecords($db, $table, TRUE, TRUE);
00077     $pos = @((ceil($unlim_num_rows / $session_max_rows) - 1) * $session_max_rows);
00078 }
00079 
00083 if (isset($store_bkm)) {
00084     require_once('./libraries/bookmark.lib.php');
00085     PMA_addBookmarks($fields, $cfg['Bookmark'], (isset($bkm_all_users) && $bkm_all_users == 'true' ? true : false));
00086     PMA_sendHeaderLocation($cfg['PmaAbsoluteUri'] . $goto);
00087 } // end if
00088 
00089 
00093 // $sql_query has been urlencoded in the confirmation form for drop/delete
00094 // queries or in the navigation bar for browsing among records
00095 if (isset($btnDrop) || isset($navig)) {
00096     $sql_query = urldecode($sql_query);
00097 }
00098 
00103 $GLOBALS['unparsed_sql'] = $sql_query;
00104 $parsed_sql = PMA_SQP_parse($sql_query);
00105 $analyzed_sql = PMA_SQP_analyze($parsed_sql);
00106 // Bug #641765 - Robbat2 - 12 January 2003, 10:49PM
00107 // Reverted - Robbat2 - 13 January 2003, 2:40PM
00108 
00109 // lem9: for bug 780516: now that we use case insensitive preg_match
00110 // or flags from the analyser, do not put back the reformatted query
00111 // into $sql_query, to make this kind of query work without
00112 // capitalizing keywords:
00113 //
00114 // CREATE TABLE SG_Persons (
00115 //  id int(10) unsigned NOT NULL auto_increment,
00116 //  first varchar(64) NOT NULL default '',
00117 //  PRIMARY KEY  (`id`)
00118 // )
00119 //
00120 // Note: now we probably do not need to fill and use $GLOBALS['unparsed_sql']
00121 // but I let this intact for now.
00122 //
00123 //$sql_query = PMA_SQP_formatHtml($parsed_sql, 'query_only');
00124 
00125 
00126 // check for a real SELECT ... FROM
00127 $is_select = isset($analyzed_sql[0]['queryflags']['select_from']);
00128 
00129 // If the query is a Select, extract the db and table names and modify
00130 // $db and $table, to have correct page headers, links and left frame.
00131 // db and table name may be enclosed with backquotes, db is optionnal,
00132 // query may contain aliases.
00133 
00134 // (TODO: if there are more than one table name in the Select:
00135 // - do not extract the first table name
00136 // - do not show a table name in the page header
00137 // - do not display the sub-pages links)
00138 
00139 if ($is_select) {
00140     $prev_db = $db;
00141     if (isset($analyzed_sql[0]['table_ref'][0]['table_true_name'])) {
00142         $table = $analyzed_sql[0]['table_ref'][0]['table_true_name'];
00143     }
00144     if (isset($analyzed_sql[0]['table_ref'][0]['db'])
00145        && !empty($analyzed_sql[0]['table_ref'][0]['db'])) {
00146         $db    = $analyzed_sql[0]['table_ref'][0]['db'];
00147     }
00148     else {
00149         $db = $prev_db;
00150     }
00151     // Nijel don't change reload, if we already decided to reload in read_dump
00152     if (!isset($reload) || $reload == 0) {
00153         $reload  = ($db == $prev_db) ? 0 : 1;
00154     }
00155 }
00156 
00160 if ($goto == 'sql.php') {
00161     $is_gotofile = FALSE;
00162     $goto = 'sql.php?'
00163           . PMA_generate_common_url($db, $table)
00164           . '&amp;pos=' . $pos
00165           . '&amp;sql_query=' . urlencode($sql_query);
00166 } // end if
00167 
00168 
00172 if (isset($btnDrop) && $btnDrop == $strNo) {
00173     if (!empty($back)) {
00174         $goto = $back;
00175     }
00176     if ($is_gotofile) {
00177         if (strpos(' ' . $goto, 'db_details') == 1 && !empty($table)) {
00178             unset($table);
00179         }
00180         $active_page = $goto;
00181         require('./' . PMA_securePath($goto));
00182     } else {
00183         PMA_sendHeaderLocation($cfg['PmaAbsoluteUri'] . str_replace('&amp;', '&', $goto));
00184     }
00185     exit();
00186 } // end if
00187 
00188 
00198 if (!$cfg['Confirm']
00199     || (isset($is_js_confirmed) && $is_js_confirmed)
00200     || isset($btnDrop)
00201 
00202     // if we are coming from a "Create PHP code" or a "Without PHP Code"
00203     // dialog, we won't execute the query anyway, so don't confirm
00204     //|| !empty($GLOBALS['show_as_php'])
00205     || isset($GLOBALS['show_as_php'])
00206 
00207     || !empty($GLOBALS['validatequery'])) {
00208     $do_confirm = FALSE;
00209 } else {
00210     $do_confirm = isset($analyzed_sql[0]['queryflags']['need_confirm']);
00211 }
00212 
00213 if ($do_confirm) {
00214     $stripped_sql_query = $sql_query;
00215     require_once('./header.inc.php');
00216     echo '<table border="0" cellpadding="3" cellspacing="0">' . "\n";
00217     if ($is_drop_database) {
00218         echo '    <tr>' . "\n"
00219            . '        <td class="tblHeadError">' . "\n";
00220         if($cfg['ErrorIconic']){
00221             echo '        <img src="' .$pmaThemeImage .'s_warn.png" border="0" hspace="2" vspace="2" align="left" />';
00222         }
00223         echo $strDropDatabaseStrongWarning . '&nbsp;<br />' . "\n";
00224     } else {
00225         echo '    <tr>' . "\n"
00226            . '        <td class="tblHeadError">' . "\n";
00227         if($cfg['ErrorIconic']){
00228             echo '        <img src="' .$pmaThemeImage .'s_really.png" border="0" hspace="2" align="middle" />';
00229         }
00230     }
00231     echo $strDoYouReally . "\n"
00232        . '        </td>' . "\n"
00233        . '    </tr>' . "\n"
00234        . '    <tr>' . "\n"
00235        . '        <td class="tblError">' . "\n"
00236        . '            <tt>' . htmlspecialchars($stripped_sql_query) . '</tt>&nbsp;?<br/>' . "\n"
00237        . '        </td>' . "\n"
00238        . '    </tr>' . "\n"
00239        . '    <form action="sql.php" method="post">' . "\n"
00240        . '    <tr>' . "\n"
00241        . '        <td align="right">' . "\n"
00242     ?>
00243     <?php echo PMA_generate_common_hidden_inputs($db, (isset($table)?$table:'')); ?>
00244     <input type="hidden" name="sql_query" value="<?php echo urlencode($sql_query); ?>" />
00245     <input type="hidden" name="zero_rows" value="<?php echo isset($zero_rows) ? PMA_sanitize($zero_rows) : ''; ?>" />
00246     <input type="hidden" name="goto" value="<?php echo $goto; ?>" />
00247     <input type="hidden" name="back" value="<?php echo isset($back) ? PMA_sanitize($back) : ''; ?>" />
00248     <input type="hidden" name="reload" value="<?php echo isset($reload) ? PMA_sanitize($reload) : 0; ?>" />
00249     <input type="hidden" name="purge" value="<?php echo isset($purge) ? PMA_sanitize($purge) : ''; ?>" />
00250     <input type="hidden" name="cpurge" value="<?php echo isset($cpurge) ? PMA_sanitize($cpurge) : ''; ?>" />
00251     <input type="hidden" name="purgekey" value="<?php echo isset($purgekey) ? PMA_sanitize($purgekey) : ''; ?>" />
00252     <input type="hidden" name="show_query" value="<?php echo isset($show_query) ? PMA_sanitize($show_query) : ''; ?>" />
00253     <input type="submit" name="btnDrop" value="<?php echo $strYes; ?>" id="buttonYes" />
00254     <input type="submit" name="btnDrop" value="<?php echo $strNo; ?>" id="buttonNo" />
00255     <?php
00256     echo '        </td>' . "\n"
00257        . '    </tr>' . "\n"
00258        . '    </form>' . "\n"
00259        . '</table>';
00260     echo "\n";
00261 } // end if
00262 
00263 
00267 else {
00268     if (!isset($sql_query)) {
00269         $sql_query = '';
00270     }
00271     // Defines some variables
00272     // A table has to be created or renamed -> left frame should be reloaded
00273     // TODO: use the parser/analyzer
00274 
00275     if ((!isset($reload) || $reload == 0)
00276         && (preg_match('@^CREATE TABLE[[:space:]]+(.*)@i', $sql_query)
00277          || preg_match('@^ALTER TABLE[[:space:]]+(.*)RENAME@i', $sql_query)
00278          || preg_match('@^TRUNCATE@i', $sql_query))) {
00279         $reload           = 1;
00280     }
00281     // Gets the number of rows per page
00282     if (empty($session_max_rows)) {
00283         $session_max_rows = $cfg['MaxRows'];
00284     } else if ($session_max_rows != 'all') {
00285         $cfg['MaxRows']   = $session_max_rows;
00286     }
00287     // Defines the display mode (horizontal/vertical) and header "frequency"
00288     if (empty($disp_direction)) {
00289         $disp_direction   = $cfg['DefaultDisplay'];
00290     }
00291     if (empty($repeat_cells)) {
00292         $repeat_cells     = $cfg['RepeatCells'];
00293     }
00294 
00295     // SK -- Patch: $is_group added for use in calculation of total number of
00296     //              rows.
00297     //              $is_count is changed for more correct "LIMIT" clause
00298     //              appending in queries like
00299     //                "SELECT COUNT(...) FROM ... GROUP BY ..."
00300 
00301     // TODO: detect all this with the parser, to avoid problems finding
00302     // those strings in comments or backquoted identifiers
00303 
00304     $is_explain = $is_count = $is_export = $is_delete = $is_insert = $is_affected = $is_show = $is_maint = $is_analyse = $is_group = $is_func = FALSE;
00305     if ($is_select) { // see line 141
00306         $is_group = preg_match('@(GROUP[[:space:]]+BY|HAVING|SELECT[[:space:]]+DISTINCT)[[:space:]]+@i', $sql_query);
00307         $is_func =  !$is_group && (preg_match('@[[:space:]]+(SUM|AVG|STD|STDDEV|MIN|MAX|BIT_OR|BIT_AND)\s*\(@i', $sql_query));
00308         $is_count = !$is_group && (preg_match('@^SELECT[[:space:]]+COUNT\((.*\.+)?.*\)@i', $sql_query));
00309         $is_export   = (preg_match('@[[:space:]]+INTO[[:space:]]+OUTFILE[[:space:]]+@i', $sql_query));
00310         $is_analyse  = (preg_match('@[[:space:]]+PROCEDURE[[:space:]]+ANALYSE@i', $sql_query));
00311     } else if (preg_match('@^EXPLAIN[[:space:]]+@i', $sql_query)) {
00312         $is_explain  = TRUE;
00313     } else if (preg_match('@^DELETE[[:space:]]+@i', $sql_query)) {
00314         $is_delete   = TRUE;
00315         $is_affected = TRUE;
00316     } else if (preg_match('@^(INSERT|LOAD[[:space:]]+DATA|REPLACE)[[:space:]]+@i', $sql_query)) {
00317         $is_insert   = TRUE;
00318         $is_affected = TRUE;
00319     } else if (preg_match('@^UPDATE[[:space:]]+@i', $sql_query)) {
00320         $is_affected = TRUE;
00321     } else if (preg_match('@^SHOW[[:space:]]+@i', $sql_query)) {
00322         $is_show     = TRUE;
00323     } else if (preg_match('@^(CHECK|ANALYZE|REPAIR|OPTIMIZE)[[:space:]]+TABLE[[:space:]]+@i', $sql_query)) {
00324         $is_maint    = TRUE;
00325     }
00326 
00327     // Do append a "LIMIT" clause?
00328     if (isset($pos)
00329         && (!$cfg['ShowAll'] || $session_max_rows != 'all')
00330         && !($is_count || $is_export || $is_func || $is_analyse)
00331         && isset($analyzed_sql[0]['queryflags']['select_from'])
00332         && !isset($analyzed_sql[0]['queryflags']['offset'])
00333         && !preg_match('@[[:space:]]LIMIT[[:space:]0-9,-]+$@i', $sql_query)) {
00334         $sql_limit_to_append = " LIMIT $pos, ".$cfg['MaxRows'] . " ";
00335 
00336 //        if (preg_match('@(.*)([[:space:]](PROCEDURE[[:space:]](.*)|FOR[[:space:]]+UPDATE|LOCK[[:space:]]+IN[[:space:]]+SHARE[[:space:]]+MODE))$@i', $sql_query, $regs)) {
00337 //            $full_sql_query  = $regs[1] . $sql_limit_to_append . $regs[2];
00338 //        } else {
00339 //            $full_sql_query  = $sql_query . $sql_limit_to_append;
00340 //        }
00341 
00342         $full_sql_query  = $analyzed_sql[0]['section_before_limit'] . $sql_limit_to_append . $analyzed_sql[0]['section_after_limit']; 
00343         // FIXME: pretty printing of this modified query
00344 
00345         if (isset($display_query)) {
00346 //            if (preg_match('@((.|\n)*)(([[:space:]](PROCEDURE[[:space:]](.*)|FOR[[:space:]]+UPDATE|LOCK[[:space:]]+IN[[:space:]]+SHARE[[:space:]]+MODE))|;)[[:space:]]*$@i', $display_query, $regs)) {
00347 //                $display_query  = $regs[1] . $sql_limit_to_append . $regs[3];
00348 //            } else {
00349 //                $display_query  = $display_query . $sql_limit_to_append;
00350 //            }
00351 
00352             // if the analysis of the original query revealed that we found 
00353             // a section_after_limit, we now have to analyze $display_query
00354             // to display it correctly
00355 
00356             if (!empty($analyzed_sql[0]['section_after_limit'])) {
00357                 $analyzed_display_query = PMA_SQP_analyze(PMA_SQP_parse($display_query));
00358                 $display_query  = $analyzed_display_query[0]['section_before_limit'] . $sql_limit_to_append . $analyzed_display_query[0]['section_after_limit']; 
00359             }
00360         }
00361 
00362     } else {
00363         $full_sql_query      = $sql_query;
00364     } // end if...else
00365 
00366     PMA_DBI_select_db($db);
00367 
00368     // If the query is a DELETE query with no WHERE clause, get the number of
00369     // rows that will be deleted (mysql_affected_rows will always return 0 in
00370     // this case)
00371 
00372     if ($is_delete
00373         && preg_match('@^DELETE([[:space:]].+)?(FROM[[:space:]](.+))$@i', $sql_query, $parts)
00374         && !preg_match('@[[:space:]]WHERE[[:space:]]@i', $parts[3])) {
00375         $cnt_all_result = @PMA_DBI_try_query('SELECT COUNT(*) as count ' .  $parts[2]);
00376         if ($cnt_all_result) {
00377             list($num_rows) = PMA_DBI_fetch_row($cnt_all_result);
00378             PMA_DBI_free_result($cnt_all_result);
00379         } else {
00380             $num_rows   = 0;
00381         }
00382     }
00383 
00384     //  E x e c u t e    t h e    q u e r y
00385 
00386     // Only if we didn't ask to see the php code (mikebeck)
00387     if (isset($GLOBALS['show_as_php']) || !empty($GLOBALS['validatequery'])) {
00388         unset($result);
00389         $num_rows = 0;
00390     }
00391     else {
00392         // garvin: Measure query time. TODO-Item http://sourceforge.net/tracker/index.php?func=detail&aid=571934&group_id=23067&atid=377411
00393         list($usec, $sec) = explode(' ',microtime());
00394         $querytime_before = ((float)$usec + (float)$sec);
00395 
00396         $result   = @PMA_DBI_try_query($full_sql_query, NULL, PMA_DBI_QUERY_STORE);
00397 
00398         list($usec, $sec) = explode(' ',microtime());
00399         $querytime_after = ((float)$usec + (float)$sec);
00400 
00401         $GLOBALS['querytime'] = $querytime_after - $querytime_before;
00402 
00403         // Displays an error message if required and stop parsing the script
00404         if ($error        = PMA_DBI_getError()) {
00405             require_once('./header.inc.php');
00406             $full_err_url = (preg_match('@^(db_details|tbl_properties)@', $err_url))
00407                           ? $err_url . '&amp;show_query=1&amp;sql_query=' . urlencode($sql_query)
00408                           : $err_url;
00409             PMA_mysqlDie($error, $full_sql_query, '', $full_err_url);
00410         }
00411         unset($error);
00412 
00413         // Gets the number of rows affected/returned
00414         // (This must be done immediately after the query because
00415         // mysql_affected_rows() reports about the last query done)
00416 
00417         if (!$is_affected) {
00418             $num_rows = ($result) ? @PMA_DBI_num_rows($result) : 0;
00419         } else if (!isset($num_rows)) {
00420             $num_rows = @PMA_DBI_affected_rows();
00421         }
00422 
00423         // Checks if the current database has changed
00424         // This could happen if the user sends a query like "USE `database`;"
00425         $res = PMA_DBI_query('SELECT DATABASE() AS \'db\';');
00426         $row = PMA_DBI_fetch_row($res);
00427         if (is_array($row) && isset($row[0]) && (strcasecmp($db,$row[0]) != 0)) {
00428             $db     = $row[0];
00429             $reload = 1;
00430         }
00431         @PMA_DBI_free_result($res);
00432         unset($res, $row);
00433 
00434         // tmpfile remove after convert encoding appended by Y.Kawada
00435         if (function_exists('PMA_kanji_file_conv')
00436             && (isset($textfile) && file_exists($textfile))) {
00437             unlink($textfile);
00438         }
00439 
00440         // Counts the total number of rows for the same 'SELECT' query without the
00441         // 'LIMIT' clause that may have been programatically added
00442 
00443         if (empty($sql_limit_to_append)) {
00444             $unlim_num_rows         = $num_rows;
00445             // if we did not append a limit, set this to get a correct
00446             // "Showing rows..." message
00447             $GLOBALS['session_max_rows'] = 'all';
00448         }
00449         else if ($is_select) {
00450 
00451                 //    c o u n t    q u e r y
00452 
00453                 // If we are "just browsing", there is only one table,
00454                 // and no where clause (or just 'WHERE 1 '),
00455                 // so we do a quick count (which uses MaxExactCount)
00456                 // because SQL_CALC_FOUND_ROWS
00457                 // is not quick on large InnoDB tables
00458 
00459                 // but do not count again if we did it previously
00460                 // due to $find_real_end == TRUE
00461 
00462                 if (!$is_group
00463                  && !isset($analyzed_sql[0]['queryflags']['union'])
00464                  && !isset($analyzed_sql[0]['table_ref'][1]['table_name'])
00465                  && (empty($analyzed_sql[0]['where_clause'])
00466                    || $analyzed_sql[0]['where_clause'] == '1 ')
00467                  && !isset($find_real_end)
00468                  ) {
00469 
00470                     // "j u s t   b r o w s i n g"
00471                     $unlim_num_rows = PMA_countRecords($db, $table, TRUE);
00472 
00473                 } else { // n o t   " j u s t   b r o w s i n g "
00474 
00475                     if (PMA_MYSQL_INT_VERSION < 40000) {
00476 
00477                         // detect this case:
00478                         // SELECT DISTINCT x AS foo, y AS bar FROM sometable
00479 
00480                         if (isset($analyzed_sql[0]['queryflags']['distinct'])) {
00481                             $count_what = 'DISTINCT ';
00482                             $first_expr = TRUE;
00483                             foreach($analyzed_sql[0]['select_expr'] as $part) {
00484                                 $count_what .= (!$first_expr ? ', ' : '') . $part['expr'];
00485                                 $first_expr = FALSE;
00486                             }
00487                          } else {
00488                              $count_what = '*';
00489                          }
00490                         $count_query = 'SELECT COUNT(' . $count_what . ') AS count';
00491                     }
00492 
00493                     // add the remaining of select expression if there is
00494                     // a GROUP BY or HAVING clause
00495                     if (PMA_MYSQL_INT_VERSION < 40000
00496                      && $count_what =='*'
00497                      && (!empty($analyzed_sql[0]['group_by_clause'])
00498                         || !empty($analyzed_sql[0]['having_clause']))) {
00499                         $count_query .= ' ,' . $analyzed_sql[0]['select_expr_clause'];
00500                     }
00501 
00502                     if (PMA_MYSQL_INT_VERSION >= 40000) {
00503                          // add select expression after the SQL_CALC_FOUND_ROWS
00504 
00505                             // for UNION, just adding SQL_CALC_FOUND_ROWS
00506                             // after the first SELECT works.
00507 
00508                             // take the left part, could be:
00509                             // SELECT
00510                             // (SELECT
00511                             $count_query = PMA_SQP_formatHtml($parsed_sql, 'query_only', 0, $analyzed_sql[0]['position_of_first_select'] + 1);
00512                             $count_query .= ' SQL_CALC_FOUND_ROWS ';
00513                             // add everything that was after the first SELECT
00514                             $count_query .= PMA_SQP_formatHtml($parsed_sql, 'query_only', $analyzed_sql[0]['position_of_first_select']+1);
00515                     } else { // PMA_MYSQL_INT_VERSION < 40000
00516 
00517                         if (!empty($analyzed_sql[0]['from_clause'])) {
00518                             $count_query .= ' FROM ' . $analyzed_sql[0]['from_clause'];
00519                         }
00520                         if (!empty($analyzed_sql[0]['where_clause'])) {
00521                             $count_query .= ' WHERE ' . $analyzed_sql[0]['where_clause'];
00522                         }
00523                         if (!empty($analyzed_sql[0]['group_by_clause'])) {
00524                             $count_query .= ' GROUP BY ' . $analyzed_sql[0]['group_by_clause'];
00525                         }
00526                         if (!empty($analyzed_sql[0]['having_clause'])) {
00527                             $count_query .= ' HAVING ' . $analyzed_sql[0]['having_clause'];
00528                         }
00529                     } // end if
00530 
00531                     // if using SQL_CALC_FOUND_ROWS, add a LIMIT to avoid
00532                     // long delays. Returned count will be complete anyway.
00533                     // (but a LIMIT would disrupt results in an UNION)
00534 
00535                     if (PMA_MYSQL_INT_VERSION >= 40000
00536                     && !isset($analyzed_sql[0]['queryflags']['union'])) {
00537                         $count_query .= ' LIMIT 1';
00538                     }
00539 
00540                     // run the count query
00541 
00542                     if (PMA_MYSQL_INT_VERSION < 40000) {
00543                         if ($cnt_all_result = PMA_DBI_try_query($count_query)) {
00544                             if ($is_group && $count_what == '*') {
00545                                 $unlim_num_rows = @PMA_DBI_num_rows($cnt_all_result);
00546                             } else {
00547                                 $unlim_num_rows = PMA_DBI_fetch_assoc($cnt_all_result);
00548                                 $unlim_num_rows = $unlim_num_rows['count'];
00549                             }
00550                             PMA_DBI_free_result($cnt_all_result);
00551                         } else {
00552                             if (PMA_DBI_getError()) {
00553 
00554                                 // there are some cases where the generated
00555                                 // count_query (for MySQL 3) is wrong,
00556                                 // so we get here.
00557                                 //TODO: use a big unlimited query to get
00558                                 // the correct number of rows (depending
00559                                 // on a config variable?)
00560                                 $unlim_num_rows = 0;
00561                             }
00562                         }
00563                     } else {
00564                         PMA_DBI_try_query($count_query);
00565                         // if (mysql_error()) {
00566                         // void.
00567                         // I tried the case
00568                         // (SELECT `User`, `Host`, `Db`, `Select_priv` FROM `db`)
00569                         // UNION (SELECT `User`, `Host`, "%" AS "Db",
00570                         // `Select_priv`
00571                         // FROM `user`) ORDER BY `User`, `Host`, `Db`;
00572                         // and although the generated count_query is wrong
00573                         // the SELECT FOUND_ROWS() work! (maybe it gets the
00574                         // count from the latest query that worked)
00575                         //
00576                         // another case where the count_query is wrong:
00577                         // SELECT COUNT( * ), f1 from t1 group by f1
00578                         // and you click to sort on count( * )
00579                         // }
00580                         $cnt_all_result       = PMA_DBI_query('SELECT FOUND_ROWS() as count;');
00581                         list($unlim_num_rows) = PMA_DBI_fetch_row($cnt_all_result);
00582                         @PMA_DBI_free_result($cnt_all_result);
00583                     }
00584             } // end else "just browsing"
00585 
00586         } else { // not $is_select
00587              $unlim_num_rows         = 0;
00588         } // end rows total count
00589 
00590         // garvin: if a table or database gets dropped, check column comments.
00591         if (isset($purge) && $purge == '1') {
00592             require_once('./libraries/relation_cleanup.lib.php');
00593 
00594             if (isset($table) && isset($db) && !empty($table) && !empty($db)) {
00595                 PMA_relationsCleanupTable($db, $table);
00596             } elseif (isset($db) && !empty($db)) {
00597                 PMA_relationsCleanupDatabase($db);
00598             } else {
00599                 // garvin: VOID. No DB/Table gets deleted.
00600             } // end if relation-stuff
00601          } // end if ($purge)
00602 
00603         // garvin: If a column gets dropped, do relation magic.
00604         if (isset($cpurge) && $cpurge == '1' && isset($purgekey)
00605             && isset($db) && isset($table)
00606             && !empty($db) && !empty($table) && !empty($purgekey)) {
00607             require_once('./libraries/relation_cleanup.lib.php');
00608             PMA_relationsCleanupColumn($db, $table, $purgekey);
00609 
00610         } // end if column PMA_* purge
00611     } // end else "didn't ask to see php code"
00612 
00613     // No rows returned -> move back to the calling page
00614     if ($num_rows < 1 || $is_affected) {
00615         if ($is_delete) {
00616             $message = $strDeletedRows . '&nbsp;' . $num_rows;
00617         } else if ($is_insert) {
00618             $message = $strInsertedRows . '&nbsp;' . $num_rows;
00619             $insert_id = PMA_DBI_insert_id();
00620             if ($insert_id != 0) {
00621                 // insert_id is id of FIRST record inserted in one insert, so if we inserted multiple rows, we had to increment this
00622                 $message .= '[br]'.$strInsertedRowId . '&nbsp;' . ($insert_id + $num_rows - 1);
00623             }
00624         } else if ($is_affected) {
00625             $message = $strAffectedRows . '&nbsp;' . $num_rows;
00626 
00627             // Ok, here is an explanation for the !$is_select.
00628             // The form generated
00629             // by tbl_query_box.php and db_details.php has many submit buttons
00630             // on the same form, and some confusion arises from the
00631             // fact that $zero_rows is sent for every case. 
00632             // The $zero_rows containing $strSuccess and sent with
00633             // the form should not have priority over 
00634             // errors like $strEmptyResultSet
00635         } else if (!empty($zero_rows) && !$is_select) {
00636             $message = $zero_rows;
00637         } else if (!empty($GLOBALS['show_as_php'])) {
00638             $message = $strPhp;
00639         } else if (!empty($GLOBALS['validatequery'])) {
00640             $message = $strValidateSQL;
00641         } else {
00642             $message = $strEmptyResultSet;
00643         }
00644 
00645         $message .= ' ' . (isset($GLOBALS['querytime']) ? '(' . sprintf($strQueryTime, $GLOBALS['querytime']) . ')' : '');
00646 
00647         if ($is_gotofile) {
00648             $goto = PMA_securePath($goto);
00649             // Checks for a valid target script
00650             if (isset($table) && $table == '') {
00651                 unset($table);
00652             }
00653             if (isset($db) && $db == '') {
00654                 unset($db);
00655             }
00656             $is_db = $is_table = FALSE;
00657             if (strpos(' ' . $goto, 'tbl_properties') == 1) {
00658                 if (!isset($table)) {
00659                     $goto     = 'db_details.php';
00660                 } else {
00661                     $is_table = @PMA_DBI_query('SHOW TABLES LIKE \'' . PMA_sqlAddslashes($table, TRUE) . '\';', NULL, PMA_DBI_QUERY_STORE);
00662                     if (!($is_table && @PMA_DBI_num_rows($is_table))) {
00663                         $goto = 'db_details.php';
00664                         unset($table);
00665                     }
00666                     @PMA_DBI_free_result($is_table);
00667                 } // end if... else...
00668             }
00669             if (strpos(' ' . $goto, 'db_details') == 1) {
00670                 if (isset($table)) {
00671                     unset($table);
00672                 }
00673                 if (!isset($db)) {
00674                     $goto     = 'main.php';
00675                 } else {
00676                     $is_db    = @PMA_DBI_select_db($db);
00677                     if (!$is_db) {
00678                         $goto = 'main.php';
00679                         unset($db);
00680                     }
00681                 } // end if... else...
00682             }
00683             // Loads to target script
00684             if (strpos(' ' . $goto, 'db_details') == 1
00685                 || strpos(' ' . $goto, 'tbl_properties') == 1) {
00686                 $js_to_run = 'functions.js';
00687             }
00688             if ($goto != 'main.php') {
00689                 require_once('./header.inc.php');
00690             }
00691             $active_page = $goto;
00692             require('./' . $goto);
00693         } // end if file_exist
00694         else {
00695             PMA_sendHeaderLocation($cfg['PmaAbsoluteUri'] . str_replace('&amp;', '&', $goto) . '&message=' . urlencode($message));
00696         } // end else
00697         exit();
00698     } // end no rows returned
00699 
00700     // At least one row is returned -> displays a table with results
00701     else {
00702         // Displays the headers
00703         if (isset($show_query)) {
00704             unset($show_query);
00705         }
00706         if (isset($printview) && $printview == '1') {
00707             require_once('./header_printview.inc.php');
00708         } else {
00709             $js_to_run = 'functions.js';
00710             unset($message);
00711             if (!empty($table)) {
00712                 require('./tbl_properties_common.php');
00713                 $url_query .= '&amp;goto=tbl_properties.php&amp;back=tbl_properties.php';
00714                 require('./tbl_properties_table_info.php');
00715                 require('./tbl_properties_links.php');
00716             }
00717             else {
00718                 require('./db_details_common.php');
00719                 require('./db_details_db_info.php');
00720             }
00721         }
00722 
00723         require_once('./libraries/relation.lib.php');
00724         $cfgRelation = PMA_getRelationsParam();
00725 
00726         // Gets the list of fields properties
00727         if (isset($result) && $result) {
00728             $fields_meta = PMA_DBI_get_fields_meta($result);
00729             $fields_cnt  = count($fields_meta);
00730         }
00731 
00732         // Display previous update query (from tbl_replace)
00733         if (isset($disp_query) && $cfg['ShowSQL'] == TRUE) {
00734             $tmp_sql_query = $GLOBALS['sql_query'];
00735             $GLOBALS['sql_query'] = $disp_query;
00736             PMA_showMessage($disp_message);
00737             $GLOBALS['sql_query'] = $tmp_sql_query;
00738         }
00739 
00740         // Displays the results in a table
00741         require_once('./libraries/display_tbl.lib.php');
00742         if (empty($disp_mode)) {
00743             // see the "PMA_setDisplayMode()" function in
00744             // libraries/display_tbl.lib.php
00745             $disp_mode = 'urdr111101';
00746         }
00747         if (!isset($dontlimitchars)) {
00748             $dontlimitchars = 0;
00749         }
00750 
00751         PMA_displayTable($result, $disp_mode, $analyzed_sql);
00752         PMA_DBI_free_result($result);
00753 
00754         // BEGIN INDEX CHECK See if indexes should be checked.
00755         if (isset($query_type) && $query_type == 'check_tbl' && isset($selected) && is_array($selected)) {
00756             foreach($selected AS $idx => $tbl_name) {
00757                 $indexes        = $indexes_info = $indexes_data = array();
00758                 $tbl_ret_keys   = PMA_get_indexes(urldecode($tbl_name), $err_url_0);
00759 
00760                 PMA_extract_indexes($tbl_ret_keys, $indexes, $indexes_info, $indexes_data);
00761 
00762                 $idx_collection = PMA_show_indexes(urldecode($tbl_name), $indexes, $indexes_info, $indexes_data, false);
00763                 $check          = PMA_check_indexes($idx_collection);
00764                 if (!empty($check)) {
00765                 ?>
00766 <table border="0" cellpadding="2" cellspacing="0">
00767     <tr>
00768         <td class="tblHeaders" colspan="7"><?php printf($strIndexWarningTable, urldecode($tbl_name)); ?></td>
00769     </tr>
00770     <?php echo $check; ?>
00771 </table>
00772                 <?php
00773                 }
00774             }
00775         } // End INDEX CHECK
00776 
00777         if ($disp_mode[6] == '1' || $disp_mode[9] == '1') {
00778             echo "\n";
00779             echo '<hr />' . "\n";
00780 
00781             // Displays "Insert a new row" link if required
00782             if ($disp_mode[6] == '1') {
00783                 $lnk_goto  = 'sql.php?'
00784                            . PMA_generate_common_url($db, $table)
00785                            . '&amp;pos=' . $pos
00786                            . '&amp;session_max_rows=' . $session_max_rows
00787                            . '&amp;disp_direction=' . $disp_direction
00788                            . '&amp;repeat_cells=' . $repeat_cells
00789                            . '&amp;dontlimitchars=' . $dontlimitchars
00790                            . '&amp;sql_query=' . urlencode($sql_query);
00791                 $url_query = '?'
00792                            . PMA_generate_common_url($db, $table)
00793                            . '&amp;pos=' . $pos
00794                            . '&amp;session_max_rows=' . $session_max_rows
00795                            . '&amp;disp_direction=' . $disp_direction
00796                            . '&amp;repeat_cells=' . $repeat_cells
00797                            . '&amp;dontlimitchars=' . $dontlimitchars
00798                            . '&amp;sql_query=' . urlencode($sql_query)
00799                            . '&amp;goto=' . urlencode($lnk_goto);
00800 
00801                 echo '    <!-- Insert a new row -->' . "\n";
00802                 echo PMA_linkOrButton(
00803                     'tbl_change.php' . $url_query,
00804                     ($cfg['PropertiesIconic'] ? '<img src="' . $pmaThemeImage . 'b_insrow.png" border="0" height="16" width="16" align="middle" hspace="2" alt="' . $strInsertNewRow . '"/>' : '') . $strInsertNewRow,
00805                     '', TRUE, TRUE, '') . "\n";
00806 
00807                 if ($disp_mode[9] == '1') {
00808                     echo '&nbsp;&nbsp;';
00809                 }
00810                 echo "\n";
00811             } // end insert new row
00812 
00813             // Displays "printable view" link if required
00814             if ($disp_mode[9] == '1') {
00815                 $url_query = '?'
00816                            . PMA_generate_common_url($db, $table)
00817                            . '&amp;pos=' . $pos
00818                            . '&amp;session_max_rows=' . $session_max_rows
00819                            . '&amp;disp_direction=' . $disp_direction
00820                            . '&amp;repeat_cells=' . $repeat_cells
00821                            . '&amp;printview=1'
00822                            . '&amp;sql_query=' . urlencode($sql_query);
00823                 echo '    <!-- Print view -->' . "\n";
00824                 echo PMA_linkOrButton(
00825                     'sql.php' . $url_query . ((isset($dontlimitchars) && $dontlimitchars == '1') ? '&amp;dontlimitchars=1' : ''),
00826                     ($cfg['PropertiesIconic'] ? '<img src="' . $pmaThemeImage . 'b_print.png" border="0" height="16" width="16" align="middle" hspace="2" alt="' . $strPrintView . '"/>' : '') . $strPrintView,
00827                     '', TRUE, TRUE, 'print_view') . "\n";
00828 
00829                 if (!$dontlimitchars) {
00830                     echo   '    &nbsp;&nbsp;' . "\n";
00831                     echo PMA_linkOrButton(
00832                         'sql.php' . $url_query . '&amp;dontlimitchars=1',
00833                         ($cfg['PropertiesIconic'] ? '<img src="' . $pmaThemeImage . 'b_print.png" border="0" height="16" width="16" align="middle" hspace="2" alt="' . $strPrintViewFull . '"/>' : '') . $strPrintViewFull,
00834                         '', TRUE, TRUE, 'print_view') . "\n";
00835                 }
00836             } // end displays "printable view"
00837 
00838             echo "\n";
00839         }
00840 
00841         // Export link
00842         // (the url_query has extra parameters that won't be used to export)
00843         // (the single_table parameter is used in display_export.lib.php
00844         //  to hide the SQL and the structure export dialogs)
00845         if (isset($analyzed_sql[0]) && $analyzed_sql[0]['querytype'] == 'SELECT' && !isset($printview)) {
00846             if (isset($analyzed_sql[0]['table_ref'][0]['table_true_name']) && !isset($analyzed_sql[0]['table_ref'][1]['table_true_name'])) {
00847                 $single_table   = '&amp;single_table=true';
00848             } else {
00849                 $single_table   = '';
00850             }
00851             echo '    <!-- Export -->' . "\n";
00852             echo   '    &nbsp;&nbsp;' . "\n";
00853             echo PMA_linkOrButton(
00854                 'tbl_properties_export.php' . $url_query . '&amp;unlim_num_rows=' . $unlim_num_rows . $single_table,
00855                 ($cfg['PropertiesIconic'] ? '<img src="' . $pmaThemeImage . 'b_tblexport.png" border="0" height="16" width="16" align="middle" hspace="2" alt="' . $strExport . '" />' : '') . $strExport,
00856                 '', TRUE, TRUE, '') . "\n";
00857         }
00858 
00859         // Bookmark Support if required
00860         if ($disp_mode[7] == '1'
00861             && ($cfg['Bookmark']['db'] && $cfg['Bookmark']['table'] && empty($id_bookmark))
00862             && !empty($sql_query)) {
00863             echo "\n";
00864 
00865             $goto = 'sql.php?'
00866                   . PMA_generate_common_url($db, $table)
00867                   . '&amp;pos=' . $pos
00868                   . '&amp;session_max_rows=' . $session_max_rows
00869                   . '&amp;disp_direction=' . $disp_direction
00870                   . '&amp;repeat_cells=' . $repeat_cells
00871                   . '&amp;dontlimitchars=' . $dontlimitchars
00872                   . '&amp;sql_query=' . urlencode($sql_query)
00873                   . '&amp;id_bookmark=1';
00874             ?>
00875 <!-- Bookmark the query -->
00876             <?php
00877             echo "\n";
00878             if ($disp_mode[3] == '1') {
00879                 echo '    <i>' . $strOr . '</i>' . "\n";
00880             }else echo '<br /><br />';
00881             ?>
00882 <form action="sql.php" method="post" onsubmit="return emptyFormElements(this, 'fields[label]');">
00883 <table border="0" cellpadding="2" cellspacing="0">
00884 <tr><td class="tblHeaders" colspan="2"><?php
00885      echo ($cfg['PropertiesIconic'] ? '<img src="' . $pmaThemeImage . 'b_bookmark.png" border="0" width="16" height="16" hspace="2" align="middle" alt="' . $strBookmarkThis . '" />' : '')
00886         . $strBookmarkThis;
00887 ?></td></tr>
00888 <tr bgcolor="<?php echo $cfg['BgcolorOne']; ?>"><td>
00889     <?php echo $strBookmarkLabel; ?>:
00890     <?php echo PMA_generate_common_hidden_inputs(); ?>
00891     <input type="hidden" name="goto" value="<?php echo $goto; ?>" />
00892     <input type="hidden" name="fields[dbase]" value="<?php echo htmlspecialchars($db); ?>" />
00893     <input type="hidden" name="fields[user]" value="<?php echo $cfg['Bookmark']['user']; ?>" />
00894     <input type="hidden" name="fields[query]" value="<?php echo urlencode(isset($complete_query) ? $complete_query : $sql_query); ?>" />
00895         </td><td>
00896     <input type="text" name="fields[label]" value="" />
00897         </td></tr>
00898 <tr bgcolor="<?php echo $cfg['BgcolorOne']; ?>"><td align="right" valign="top">
00899     <input type="checkbox" name="bkm_all_users" id="bkm_all_users" value="true" /></td>
00900     <td><label for="bkm_all_users"><?php echo $strBookmarkAllUsers; ?></label></td>
00901 </tr>
00902 <tr>
00903     <td class="tblFooters" colspan="2" align="right">
00904     <input type="submit" name="store_bkm" value="<?php echo $strBookmarkThis; ?>" />
00905     </td></tr>
00906 </table></form>
00907             <?php
00908         } // end bookmark support
00909 
00910         // Do print the page if required
00911         if (isset($printview) && $printview == '1') {
00912             echo "\n";
00913             ?>
00914 <script type="text/javascript" language="javascript1.2">
00915 <!--
00916 // Do print the page
00917 if (typeof(window.print) != 'undefined') {
00918     window.print();
00919 }
00920 //-->
00921 </script>
00922             <?php
00923         } // end print case
00924     } // end rows returned
00925 
00926 } // end executes the query
00927 echo "\n\n";
00928 
00932 require_once('./footer.inc.php');
00933 ?>


Généré par TYPO3 Ameos avec  doxygen 1.4.6