home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / phpMyAdmin / tbl_select.php < prev    next >
PHP Script  |  2008-06-23  |  18KB  |  441 lines

  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4.  *
  5.  * @version $Id: tbl_select.php 10241 2007-04-01 11:13:46Z cybot_tm $
  6.  */
  7.  
  8. /**
  9.  * Gets some core libraries
  10.  */
  11. require_once './libraries/common.inc.php';
  12. require_once './libraries/relation.lib.php'; // foreign keys
  13. require_once './libraries/mysql_charsets.lib.php';
  14.  
  15. if ($GLOBALS['cfg']['PropertiesIconic'] == true) {
  16.     $titles['Browse'] =
  17.         '<img class="icon" width="16" height="16" src="' . $pmaThemeImage
  18.         .'b_browse.png" alt="' . $strBrowseForeignValues . '" title="'
  19.         .$strBrowseForeignValues . '" />';
  20.  
  21.     if ($GLOBALS['cfg']['PropertiesIconic'] === 'both') {
  22.         $titles['Browse'] .= $strBrowseForeignValues;
  23.     }
  24. } else {
  25.     $titles['Browse'] = $strBrowseForeignValues;
  26. }
  27.  
  28. /**
  29.  * Not selection yet required -> displays the selection form
  30.  */
  31. if (!isset($param) || $param[0] == '') {
  32.     // Gets some core libraries
  33.     require_once './libraries/tbl_common.php';
  34.     //$err_url   = 'tbl_select.php' . $err_url;
  35.     $url_query .= '&goto=tbl_select.php&back=tbl_select.php';
  36.  
  37.     /**
  38.      * Gets tables informations
  39.      */
  40.     require_once './libraries/tbl_info.inc.php';
  41.  
  42.     /**
  43.      * Displays top menu links
  44.      */
  45.     require_once './libraries/tbl_links.inc.php';
  46.  
  47.     if (!isset($goto)) {
  48.         $goto = $GLOBALS['cfg']['DefaultTabTable'];
  49.     }
  50.     // Defines the url to return to in case of error in the next sql statement
  51.     $err_url   = $goto . '?' . PMA_generate_common_url($db, $table);
  52.  
  53.     // Gets the list and number of fields
  54.     $result     = PMA_DBI_query('SHOW' . (PMA_MYSQL_INT_VERSION >= 40100 ? ' FULL' : '') . ' FIELDS FROM ' . PMA_backquote($table) . ' FROM ' . PMA_backquote($db) . ';', null, PMA_DBI_QUERY_STORE);
  55.     $fields_cnt = PMA_DBI_num_rows($result);
  56.     // rabue: we'd better ensure, that all arrays are empty.
  57.     $fields_list = $fields_null = $fields_type = $fields_collation = array();
  58.     while ($row = PMA_DBI_fetch_assoc($result)) {
  59.         $fields_list[] = $row['Field'];
  60.         $type          = $row['Type'];
  61.         // reformat mysql query output - staybyte - 9. June 2001
  62.         if (strncasecmp($type, 'set', 3) == 0
  63.             || strncasecmp($type, 'enum', 4) == 0) {
  64.             $type = str_replace(',', ', ', $type);
  65.         } else {
  66.  
  67.             // strip the "BINARY" attribute, except if we find "BINARY(" because
  68.             // this would be a BINARY or VARBINARY field type
  69.             if (!preg_match('@BINARY[\(]@i', $type)) {
  70.                 $type = preg_replace('@BINARY@i', '', $type);
  71.             }
  72.             $type = preg_replace('@ZEROFILL@i', '', $type);
  73.             $type = preg_replace('@UNSIGNED@i', '', $type);
  74.  
  75.             $type = strtolower($type);
  76.         }
  77.         if (empty($type)) {
  78.             $type = ' ';
  79.         }
  80.         $fields_null[] = $row['Null'];
  81.         $fields_type[] = $type;
  82.         $fields_collation[] = PMA_MYSQL_INT_VERSION >= 40100 && !empty($row['Collation']) && $row['Collation'] != 'NULL'
  83.                           ? $row['Collation']
  84.                           : '';
  85.     } // end while
  86.     PMA_DBI_free_result($result);
  87.     unset($result, $type);
  88.  
  89.     // <markus@noga.de>
  90.     // retrieve keys into foreign fields, if any
  91.     $cfgRelation = PMA_getRelationsParam();
  92.     // check also foreigners even if relwork is FALSE (to get
  93.     // foreign keys from innodb)
  94.     //$foreigners  = ($cfgRelation['relwork'] ? PMA_getForeigners($db, $table) : FALSE);
  95.     $foreigners  = PMA_getForeigners($db, $table);
  96.     ?>
  97. <script type="text/javascript">
  98. // <![CDATA[
  99. function PMA_tbl_select_operator(f, index, multiple) {
  100.     switch (f.elements["func[" + index + "]"].options[f.elements["func[" + index + "]"].selectedIndex].value) {
  101. <?php
  102. reset($GLOBALS['cfg']['UnaryOperators']);
  103. while (list($operator) = each($GLOBALS['cfg']['UnaryOperators'])) {
  104.     echo '        case "' . $operator . "\":\r\n";
  105. }
  106. ?>
  107.             bDisabled = true;
  108.             break;
  109.  
  110.         default:
  111.             bDisabled = false;
  112.     }
  113.     f.elements["fields[" + index + "]" + ((multiple) ? "[]": "")].disabled = bDisabled;
  114. }
  115. // ]]>
  116. </script>
  117. <form method="post" action="tbl_select.php" name="insertForm">
  118. <?php echo PMA_generate_common_hidden_inputs($db, $table); ?>
  119. <input type="hidden" name="goto" value="<?php echo $goto; ?>" />
  120. <input type="hidden" name="back" value="tbl_select.php" />
  121.  
  122. <fieldset id="fieldset_table_search">
  123.  
  124. <fieldset id="fieldset_select_fields">
  125.     <legend><?php echo $strSelectFields; ?></legend>
  126.     <select name="param[]" size="<?php echo min($fields_cnt, 10); ?>"
  127.         multiple="multiple">
  128.     <?php
  129.     // Displays the list of the fields
  130.     foreach ($fields_list as $each_field) {
  131.         echo '        '
  132.             .'<option value="' . htmlspecialchars($each_field) . '"'
  133.             .' selected="selected">' . htmlspecialchars($each_field)
  134.             .'</option>' . "\n";
  135.     }
  136.     ?>
  137.     </select>
  138.     <input type="checkbox" name="distinct" value="DISTINCT" id="oDistinct" />
  139.     <label for="oDistinct">DISTINCT</label>
  140. </fieldset>
  141.  
  142. <fieldset id="fieldset_limit_rows">
  143.     <legend><?php echo $strLimitNumRows; ?></legend>
  144.     <input type="text" size="4" name="session_max_rows"
  145.         value="<?php echo $GLOBALS['cfg']['MaxRows']; ?>" class="textfield" />
  146. </fieldset>
  147.  
  148. <fieldset id="fieldset_display_order">
  149.     <legend><?php echo $strDisplayOrder; ?></legend>
  150.     <select name="orderField" style="vertical-align: middle">
  151.         <option value="--nil--"></option>
  152.     <?php
  153.     foreach ($fields_list as $each_field) {
  154.         echo '        '
  155.             .'<option value="' . htmlspecialchars($each_field) . '">'
  156.             .htmlspecialchars($each_field) . '</option>' . "\n";
  157.     } // end for
  158.     ?>
  159.     </select>
  160.  
  161.     <div class="formelement">
  162.         <input type="radio" name="order" value="ASC" checked="checked" id="sortASC" />
  163.         <label for="sortASC"><?php echo $strAscending; ?></label>
  164.     </div>
  165.  
  166.     <div class="formelement">
  167.         <input type="radio" name="order" value="DESC" id="sortDESC" />
  168.         <label for="sortDESC"><?php echo $strDescending; ?></label>
  169.     </div>
  170. </fieldset>
  171.  
  172. <br class="clearfloat" />
  173. <?php echo $strAddSearchConditions; ?>
  174. <?php echo PMA_showMySQLDocu('SQL-Syntax', 'Functions'); ?>
  175.  
  176. <input type="text" name="where" class="textfield" size="64" />
  177.  
  178. </fieldset>
  179. <fieldset class="tblFooters">
  180.     <input type="submit" name="submit" value="<?php echo $strGo; ?>" />
  181. </fieldset>
  182.  
  183. <fieldset id="fieldset_table_qbe">
  184.     <legend><?php echo '<em>' . $strOr . '</em> ' . $strDoAQuery; ?></legend>
  185.     <table class="data">
  186.     <thead>
  187.     <tr><th><?php echo $strField; ?></th>
  188.         <th><?php echo $strType; ?></th>
  189.         <?php echo PMA_MYSQL_INT_VERSION >= 40100 ? '<th>' . $strCollation . '</th>' . "\n" : ''; ?>
  190.         <th><?php echo $strOperator; ?></th>
  191.         <th><?php echo $strValue; ?></th>
  192.     </tr>
  193.     </thead>
  194.     <tbody>
  195.     <?php
  196.     $odd_row = true;
  197. ?>
  198. <script type="text/javascript" src="./js/tbl_change.js"></script>
  199. <?php
  200.     for ($i = 0; $i < $fields_cnt; $i++) {
  201.         ?>
  202.         <tr class="<?php echo $odd_row ? 'odd' : 'even'; $odd_row = ! $odd_row; ?>">
  203.             <th><?php echo htmlspecialchars($fields_list[$i]); ?></th>
  204.             <td><?php echo $fields_type[$i]; ?></td>
  205.             <?php echo PMA_MYSQL_INT_VERSION >= 40100 ? '<td>'
  206.                 . $fields_collation[$i] . '</td>' . "\n" : ''; ?>
  207.             <td><select name="func[]">
  208.         <?php
  209.         if (strncasecmp($fields_type[$i], 'enum', 4) == 0) {
  210.             foreach ($GLOBALS['cfg']['EnumOperators'] as $fc) {
  211.                 echo "\n" . '                        '
  212.                    . '<option value="' . htmlspecialchars($fc) . '">'
  213.                    . htmlspecialchars($fc) . '</option>';
  214.             }
  215.         } elseif (preg_match('@char|blob|text|set@i', $fields_type[$i])) {
  216.             foreach ($GLOBALS['cfg']['TextOperators'] as $fc) {
  217.             echo "\n" . '                        '
  218.                . '<option value="' . htmlspecialchars($fc) . '">'
  219.                . htmlspecialchars($fc) . '</option>';
  220.             }
  221.         } else {
  222.             foreach ($GLOBALS['cfg']['NumOperators'] as $fc) {
  223.                 echo "\n" . '                        '
  224.                    . '<option value="' .  htmlspecialchars($fc) . '">'
  225.                    . htmlspecialchars($fc) . '</option>';
  226.             }
  227.         } // end if... else...
  228.         if ($fields_null[$i]) {
  229.             foreach ($GLOBALS['cfg']['NullOperators'] as $fc) {
  230.                 echo "\n" . '                        '
  231.                    . '<option value="' .  htmlspecialchars($fc) . '">'
  232.                    . htmlspecialchars($fc) . '</option>';
  233.             }
  234.         }
  235.         ?>
  236.  
  237.                 </select>
  238.             </td>
  239.             <td>
  240.         <?php
  241.         // <markus@noga.de>
  242.         $field = $fields_list[$i];
  243.  
  244.         // do not use require_once here
  245.         require './libraries/get_foreign.lib.php';
  246.  
  247.         // we got a bug report: in some cases, even if $disp is true,
  248.         // there are no rows, so we add a fetch_array
  249.  
  250.         if ($foreigners && isset($foreigners[$field]) && isset($disp_row) && is_array($disp_row)) {
  251.             // f o r e i g n    k e y s
  252.             echo '            <select name="fields[' . $i . ']">' . "\n";
  253.             // go back to first row
  254.  
  255.             // here, the 4th parameter is empty because there is no current
  256.             // value of data for the dropdown (the search page initial values
  257.             // are displayed empty)
  258.             echo PMA_foreignDropdown($disp_row, $foreign_field, $foreign_display,
  259.                 '', $GLOBALS['cfg']['ForeignKeyMaxLimit']);
  260.             echo '            </select>' . "\n";
  261.         } elseif (isset($foreign_link) && $foreign_link == true) {
  262.             ?>
  263.             <input type="text" name="fields[<?php echo $i; ?>]"
  264.                 id="field_<?php echo md5($field); ?>[<?php echo $i; ?>]"
  265.                 class="textfield" />
  266.             <script type="text/javascript">
  267.             // <![CDATA[
  268.                 document.writeln('<a target="_blank" onclick="window.open(this.href, \'foreigners\', \'width=640,height=240,scrollbars=yes\'); return false" href="browse_foreigners.php?<?php echo PMA_generate_common_url($db, $table); ?>&field=<?php echo urlencode($field); ?>&fieldkey=<?php echo $i; ?>"><?php echo str_replace("'", "\'", $titles['Browse']); ?></a>');
  269.             // ]]>
  270.             </script>
  271.             <?php
  272.         } elseif (strncasecmp($fields_type[$i], 'enum', 4) == 0) {
  273.             // e n u m s
  274.             $enum_value=explode(', ', str_replace("'", '', substr($fields_type[$i], 5, -1)));
  275.             $cnt_enum_value = count($enum_value);
  276.             echo '            <select name="fields[' . $i . '][]"'
  277.                 .' multiple="multiple" size="' . min(3, $cnt_enum_value) . '">' . "\n";
  278.             for ($j = 0; $j < $cnt_enum_value; $j++) {
  279.                 echo '                <option value="' . $enum_value[$j] . '">'
  280.                     . $enum_value[$j] . '</option>';
  281.             } // end for
  282.             echo '            </select>' . "\n";
  283.         } else {
  284.             // o t h e r   c a s e s
  285.             echo '            <input type="text" name="fields[' . $i . ']"'
  286.                 .' size="40" class="textfield" id="field_' . $i . '" />' .  "\n";
  287.         };
  288.         $type = $fields_type[$i];
  289.         if ($type == 'date' || $type == 'datetime' || substr($type, 0, 9) == 'timestamp') {
  290.         ?>
  291.                     <script type="text/javascript">
  292.                     //<![CDATA[
  293.                     document.write('<a title="<?php echo $strCalendar;?>" href="javascript:openCalendar(\'<?php echo PMA_generate_common_url();?>\', \'insertForm\', \'field_<?php echo ($i); ?>\', \'<?php echo (PMA_MYSQL_INT_VERSION >= 40100 && substr($type, 0, 9) == 'timestamp') ? 'datetime' : substr($type, 0, 9); ?>\')"><img class="calendar" src="<?php echo $pmaThemeImage; ?>b_calendar.png" alt="<?php echo $strCalendar; ?>"/></a>');
  294.                     //]]>
  295.                     </script>
  296.         <?php
  297.         }
  298.         ?>
  299.             <input type="hidden" name="names[<?php echo $i; ?>]"
  300.                 value="<?php echo htmlspecialchars($fields_list[$i]); ?>" />
  301.             <input type="hidden" name="types[<?php echo $i; ?>]"
  302.                 value="<?php echo $fields_type[$i]; ?>" />
  303.             <input type="hidden" name="collations[<?php echo $i; ?>]"
  304.                 value="<?php echo $fields_collation[$i]; ?>" />
  305.         </td>
  306.     </tr>
  307.         <?php
  308.     } // end for
  309.     ?>
  310.     </tbody>
  311.     </table>
  312. </fieldset>
  313. <fieldset class="tblFooters">
  314.     <input type="hidden" name="max_number_of_fields"
  315.         value="<?php echo $fields_cnt; ?>" />
  316.     <input type="submit" name="submit" value="<?php echo $strGo; ?>" />
  317. </fieldset>
  318. </form>
  319.     <?php
  320.     require_once './libraries/footer.inc.php';
  321. }
  322.  
  323.  
  324. /**
  325.  * Selection criteria have been submitted -> do the work
  326.  */
  327. else {
  328.     // Builds the query
  329.  
  330.     $sql_query = 'SELECT ' . (isset($distinct) ? 'DISTINCT ' : '');
  331.  
  332.     // if all fields were selected to display, we do a SELECT *
  333.     // (more efficient and this helps prevent a problem in IE
  334.     // if one of the rows is edited and we come back to the Select results)
  335.  
  336.     if (count($param) == $max_number_of_fields) {
  337.         $sql_query .= '* ';
  338.     } else {
  339.         $param = PMA_backquote($param);
  340.         $sql_query .= implode(', ', $param);
  341.         unset($param);
  342.     } // end if
  343.  
  344.     $sql_query .= ' FROM ' . PMA_backquote($table);
  345.  
  346.     // The where clause
  347.     if (trim($where) != '') {
  348.         $sql_query .= ' WHERE ' . $where;
  349.     } else {
  350.         $w = $charsets = array();
  351.         $cnt_func = count($func);
  352.         reset($func);
  353.         while (list($i, $func_type) = each($func)) {
  354.             if (PMA_MYSQL_INT_VERSION >= 40100) {
  355.                 list($charsets[$i]) = explode('_', $collations[$i]);
  356.             }
  357.             if (@$GLOBALS['cfg']['UnaryOperators'][$func_type] == 1) {
  358.                 $fields[$i] = '';
  359.                 $w[] = PMA_backquote(urldecode($names[$i])) . ' ' . $func_type;
  360.  
  361.             } elseif (strncasecmp($types[$i], 'enum', 4) == 0) {
  362.                 if (!empty($fields[$i])) {
  363.                     if (!is_array($fields[$i])) {
  364.                         $fields[$i] = explode(',', $fields[$i]);
  365.                     }
  366.                     $enum_selected_count = count($fields[$i]);
  367.                     if ($func_type == '=' && $enum_selected_count > 1) {
  368.                         $func_type    = $func[$i] = 'IN';
  369.                         $parens_open  = '(';
  370.                         $parens_close = ')';
  371.  
  372.                     } elseif ($func_type == '!=' && $enum_selected_count > 1) {
  373.                         $func_type    = $func[$i] = 'NOT IN';
  374.                         $parens_open  = '(';
  375.                         $parens_close = ')';
  376.  
  377.                     } else {
  378.                         $parens_open  = '';
  379.                         $parens_close = '';
  380.                     }
  381.                     $enum_where = '\'' . PMA_sqlAddslashes($fields[$i][0]) . '\'';
  382.                     if (PMA_MYSQL_INT_VERSION >= 40100 && $charsets[$i] != $charset_connection) {
  383.                         $enum_where = 'CONVERT(_utf8 ' . $enum_where . ' USING ' . $charsets[$i] . ') COLLATE ' . $collations[$i];
  384.                     }
  385.                     for ($e = 1; $e < $enum_selected_count; $e++) {
  386.                         $enum_where .= ', ';
  387.                         $tmp_literal = '\'' . PMA_sqlAddslashes($fields[$i][$e]) . '\'';
  388.                         if (PMA_MYSQL_INT_VERSION >= 40100 && $charsets[$i] != $charset_connection) {
  389.                             $tmp_literal = 'CONVERT(_utf8 ' . $tmp_literal . ' USING ' . $charsets[$i] . ') COLLATE ' . $collations[$i];
  390.                         }
  391.                         $enum_where .= $tmp_literal;
  392.                         unset($tmp_literal);
  393.                     }
  394.  
  395.                     $w[] = PMA_backquote(urldecode($names[$i])) . ' ' . $func_type . ' ' . $parens_open . $enum_where . $parens_close;
  396.                 }
  397.  
  398.             } elseif ($fields[$i] != '') {
  399.                 // For these types we quote the value. Even if it's another type (like INT),
  400.                 // for a LIKE we always quote the value. MySQL converts strings to numbers
  401.                 // and numbers to strings as necessary during the comparison
  402.                 if (preg_match('@char|binary|blob|text|set|date|time|year@i', $types[$i]) || strpos(' ' . $func_type, 'LIKE')) {
  403.                     $quot = '\'';
  404.                 } else {
  405.                     $quot = '';
  406.                 }
  407.  
  408.                 // Make query independant from the selected connection charset.
  409.                 // But if the field's type is VARBINARY, it has no charset
  410.                 // and $charsets[$i] is empty, so we cannot generate a CONVERT
  411.  
  412.                 if (PMA_MYSQL_INT_VERSION >= 40101 && !empty($charsets[$i]) && $charsets[$i] != $charset_connection && preg_match('@char|binary|blob|text|set@i', $types[$i])) {
  413.                     $prefix = 'CONVERT(_utf8 ';
  414.                     $suffix = ' USING ' . $charsets[$i] . ') COLLATE ' . $collations[$i];
  415.                 } else {
  416.                     $prefix = $suffix = '';
  417.                 }
  418.  
  419.                 // LIKE %...%
  420.                 if ($func_type == 'LIKE %...%') {
  421.                     $func_type = 'LIKE';
  422.                     $fields[$i] = '%' . $fields[$i] . '%';
  423.                 }
  424.                 $w[] = PMA_backquote(urldecode($names[$i])) . ' ' . $func_type . ' ' . $prefix . $quot . PMA_sqlAddslashes($fields[$i]) . $quot . $suffix;
  425.  
  426.             } // end if
  427.         } // end for
  428.  
  429.         if ($w) {
  430.             $sql_query .= ' WHERE ' . implode(' AND ', $w);
  431.         }
  432.     } // end if
  433.  
  434.     if ($orderField != '--nil--') {
  435.         $sql_query .= ' ORDER BY ' . PMA_backquote(urldecode($orderField)) . ' ' . $order;
  436.     } // end if
  437.     include './sql.php';
  438. }
  439.  
  440. ?>
  441.