Merge branch 'QA_3_3'
[phpmyadmin/dkf.git] / tbl_select.php
blob440bd3e2055848272a98ee9fb4e96a8a98f76f06
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Handles table search tab
6 * display table search form, create SQL query from form data
7 * and include sql.php to execute it
9 * @todo display search form again if no results from previous search
10 * @version $Id$
11 * @package phpMyAdmin
14 /**
15 * Gets some core libraries
17 require_once './libraries/common.inc.php';
18 require_once './libraries/relation.lib.php'; // foreign keys
19 require_once './libraries/mysql_charsets.lib.php';
21 $GLOBALS['js_include'][] = 'tbl_change.js';
22 $GLOBALS['js_include'][] = 'jquery/jquery-ui-1.8.custom.js';
23 $GLOBALS['js_include'][] = 'jquery/timepicker.js';
24 if ($GLOBALS['cfg']['PropertiesIconic'] == true) {
25 $titles['Browse'] =
26 '<img class="icon" width="16" height="16" src="' . $pmaThemeImage
27 .'b_browse.png" alt="' . $strBrowseForeignValues . '" title="'
28 .$strBrowseForeignValues . '" />';
30 if ($GLOBALS['cfg']['PropertiesIconic'] === 'both') {
31 $titles['Browse'] .= $strBrowseForeignValues;
33 } else {
34 $titles['Browse'] = $strBrowseForeignValues;
37 /**
38 * Not selection yet required -> displays the selection form
40 if (!isset($param) || $param[0] == '') {
41 // Gets some core libraries
42 require_once './libraries/tbl_common.php';
43 //$err_url = 'tbl_select.php' . $err_url;
44 $url_query .= '&amp;goto=tbl_select.php&amp;back=tbl_select.php';
46 /**
47 * Gets tables informations
49 require_once './libraries/tbl_info.inc.php';
51 /**
52 * Displays top menu links
54 require_once './libraries/tbl_links.inc.php';
56 if (!isset($goto)) {
57 $goto = $GLOBALS['cfg']['DefaultTabTable'];
59 // Defines the url to return to in case of error in the next sql statement
60 $err_url = $goto . '?' . PMA_generate_common_url($db, $table);
62 // Gets the list and number of fields
63 $result = PMA_DBI_query('SHOW FULL FIELDS FROM ' . PMA_backquote($table) . ' FROM ' . PMA_backquote($db) . ';', null, PMA_DBI_QUERY_STORE);
64 $fields_cnt = PMA_DBI_num_rows($result);
65 // rabue: we'd better ensure, that all arrays are empty.
66 $fields_list = $fields_null = $fields_type = $fields_collation = array();
67 while ($row = PMA_DBI_fetch_assoc($result)) {
68 $fields_list[] = $row['Field'];
69 $type = $row['Type'];
70 // reformat mysql query output
71 if (strncasecmp($type, 'set', 3) == 0
72 || strncasecmp($type, 'enum', 4) == 0) {
73 $type = str_replace(',', ', ', $type);
74 } else {
76 // strip the "BINARY" attribute, except if we find "BINARY(" because
77 // this would be a BINARY or VARBINARY field type
78 if (!preg_match('@BINARY[\(]@i', $type)) {
79 $type = preg_replace('@BINARY@i', '', $type);
81 $type = preg_replace('@ZEROFILL@i', '', $type);
82 $type = preg_replace('@UNSIGNED@i', '', $type);
84 $type = strtolower($type);
86 if (empty($type)) {
87 $type = '&nbsp;';
89 $fields_null[] = $row['Null'];
90 $fields_type[] = $type;
91 $fields_collation[] = !empty($row['Collation']) && $row['Collation'] != 'NULL'
92 ? $row['Collation']
93 : '';
94 } // end while
95 PMA_DBI_free_result($result);
96 unset($result, $type);
98 // <markus@noga.de>
99 // retrieve keys into foreign fields, if any
100 // check also foreigners even if relwork is FALSE (to get
101 // foreign keys from innodb)
102 $foreigners = PMA_getForeigners($db, $table);
104 <script type="text/javascript">
105 // <![CDATA[
106 function PMA_tbl_select_operator(f, index, multiple) {
107 switch (f.elements["func[" + index + "]"].options[f.elements["func[" + index + "]"].selectedIndex].value) {
108 <?php
109 reset($GLOBALS['cfg']['UnaryOperators']);
110 while (list($operator) = each($GLOBALS['cfg']['UnaryOperators'])) {
111 echo ' case "' . $operator . "\":\r\n";
114 bDisabled = true;
115 break;
117 default:
118 bDisabled = false;
120 f.elements["fields[" + index + "]" + ((multiple) ? "[]": "")].disabled = bDisabled;
122 // ]]>
123 </script>
124 <form method="post" action="tbl_select.php" name="insertForm">
125 <?php echo PMA_generate_common_hidden_inputs($db, $table); ?>
126 <input type="hidden" name="goto" value="<?php echo $goto; ?>" />
127 <input type="hidden" name="back" value="tbl_select.php" />
129 <fieldset id="fieldset_table_search">
131 <fieldset id="fieldset_table_qbe">
132 <legend><?php echo $strDoAQuery; ?></legend>
133 <table class="data">
134 <thead>
135 <tr><th><?php echo $strField; ?></th>
136 <th><?php echo $strType; ?></th>
137 <th><?php echo $strCollation; ?></th>
138 <th><?php echo $strOperator; ?></th>
139 <th><?php echo $strValue; ?></th>
140 </tr>
141 </thead>
142 <tbody>
143 <?php
144 $odd_row = true;
146 for ($i = 0; $i < $fields_cnt; $i++) {
148 <tr class="<?php echo $odd_row ? 'odd' : 'even'; $odd_row = ! $odd_row; ?>">
149 <th><?php echo htmlspecialchars($fields_list[$i]); ?></th>
150 <td><?php echo $fields_type[$i]; ?></td>
151 <td><?php echo $fields_collation[$i]; ?></td>
152 <td><select name="func[]">
153 <?php
154 if (strncasecmp($fields_type[$i], 'enum', 4) == 0) {
155 foreach ($GLOBALS['cfg']['EnumOperators'] as $fc) {
156 echo "\n" . ' '
157 . '<option value="' . htmlspecialchars($fc) . '">'
158 . htmlspecialchars($fc) . '</option>';
160 } elseif (preg_match('@char|blob|text|set@i', $fields_type[$i])) {
161 foreach ($GLOBALS['cfg']['TextOperators'] as $fc) {
162 echo "\n" . ' '
163 . '<option value="' . htmlspecialchars($fc) . '">'
164 . htmlspecialchars($fc) . '</option>';
166 } else {
167 foreach ($GLOBALS['cfg']['NumOperators'] as $fc) {
168 echo "\n" . ' '
169 . '<option value="' . htmlspecialchars($fc) . '">'
170 . htmlspecialchars($fc) . '</option>';
172 } // end if... else...
173 if ($fields_null[$i]) {
174 foreach ($GLOBALS['cfg']['NullOperators'] as $fc) {
175 echo "\n" . ' '
176 . '<option value="' . htmlspecialchars($fc) . '">'
177 . htmlspecialchars($fc) . '</option>';
182 </select>
183 </td>
184 <td>
185 <?php
186 // <markus@noga.de>
187 $field = $fields_list[$i];
189 $foreignData = PMA_getForeignData($foreigners, $field, false, '', '');
191 if ($foreigners && isset($foreigners[$field]) && is_array($foreignData['disp_row'])) {
192 // f o r e i g n k e y s
193 echo ' <select name="fields[' . $i . ']">' . "\n";
194 // go back to first row
196 // here, the 4th parameter is empty because there is no current
197 // value of data for the dropdown (the search page initial values
198 // are displayed empty)
199 echo PMA_foreignDropdown($foreignData['disp_row'],
200 $foreignData['foreign_field'],
201 $foreignData['foreign_display'],
202 '', $GLOBALS['cfg']['ForeignKeyMaxLimit']);
203 echo ' </select>' . "\n";
204 } elseif ($foreignData['foreign_link'] == true) {
206 <input type="text" name="fields[<?php echo $i; ?>]"
207 id="field_<?php echo md5($field); ?>[<?php echo $i; ?>]"
208 class="textfield" />
209 <script type="text/javascript">
210 // <![CDATA[
211 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); ?>&amp;field=<?php echo urlencode($field); ?>&amp;fieldkey=<?php echo $i; ?>"><?php echo str_replace("'", "\'", $titles['Browse']); ?></a>');
212 // ]]>
213 </script>
214 <?php
215 } elseif (strncasecmp($fields_type[$i], 'enum', 4) == 0) {
216 // e n u m s
217 $enum_value=explode(', ', str_replace("'", '', substr($fields_type[$i], 5, -1)));
218 $cnt_enum_value = count($enum_value);
219 echo ' <select name="fields[' . $i . '][]"'
220 .' multiple="multiple" size="' . min(3, $cnt_enum_value) . '">' . "\n";
221 for ($j = 0; $j < $cnt_enum_value; $j++) {
222 echo ' <option value="' . $enum_value[$j] . '">'
223 . $enum_value[$j] . '</option>';
224 } // end for
225 echo ' </select>' . "\n";
226 } else {
227 // o t h e r c a s e s
228 echo ' <input type="text" name="fields[' . $i . ']"'
229 .' size="40" class="textfield" id="field_' . $i . '" />' . "\n";
231 $type = $fields_type[$i];
232 if ($type == 'date' || $type == 'datetime' || substr($type, 0, 9) == 'timestamp') {
234 <script type="text/javascript">
235 //<![CDATA[
236 $(function() {
237 $('#field_<?php echo $i; ?>').datepicker({
238 duration: '',
239 time24h: true,
240 stepMinutes: 1,
241 stepHours: 1,
242 <?php echo ($type == 'date' ? "showTime: false,":"showTime: true,"); ?>
243 altTimeField: '',
244 constrainInput: false
247 //]]>
248 </script>
249 <?php
252 <input type="hidden" name="names[<?php echo $i; ?>]"
253 value="<?php echo htmlspecialchars($fields_list[$i]); ?>" />
254 <input type="hidden" name="types[<?php echo $i; ?>]"
255 value="<?php echo $fields_type[$i]; ?>" />
256 <input type="hidden" name="collations[<?php echo $i; ?>]"
257 value="<?php echo $fields_collation[$i]; ?>" />
258 </td>
259 </tr>
260 <?php
261 } // end for
263 </tbody>
264 </table>
265 </fieldset>
266 <?php
267 PMA_generate_slider_effect('searchoptions', $strOptions);
269 <fieldset id="fieldset_select_fields">
270 <legend><?php echo $strSelectFields; ?></legend>
271 <select name="param[]" size="<?php echo min($fields_cnt, 10); ?>"
272 multiple="multiple">
273 <?php
274 // Displays the list of the fields
275 foreach ($fields_list as $each_field) {
276 echo ' '
277 .'<option value="' . htmlspecialchars($each_field) . '"'
278 .' selected="selected">' . htmlspecialchars($each_field)
279 .'</option>' . "\n";
282 </select>
283 <input type="checkbox" name="distinct" value="DISTINCT" id="oDistinct" />
284 <label for="oDistinct">DISTINCT</label>
285 </fieldset>
287 <fieldset id="fieldset_search_conditions">
288 <legend><?php echo '<em>' . $strOr . '</em> ' .$strAddSearchConditions; ?></legend>
289 <?php echo PMA_showMySQLDocu('SQL-Syntax', 'Functions'); ?>
291 <input type="text" name="where" class="textfield" size="64" />
292 </fieldset>
294 <fieldset id="fieldset_limit_rows">
295 <legend><?php echo $strLimitNumRows; ?></legend>
296 <input type="text" size="4" name="session_max_rows"
297 value="<?php echo $GLOBALS['cfg']['MaxRows']; ?>" class="textfield" />
298 </fieldset>
300 <fieldset id="fieldset_display_order">
301 <legend><?php echo $strDisplayOrder; ?></legend>
302 <select name="orderField" style="vertical-align: middle">
303 <option value="--nil--"></option>
304 <?php
305 foreach ($fields_list as $each_field) {
306 echo ' '
307 .'<option value="' . htmlspecialchars($each_field) . '">'
308 .htmlspecialchars($each_field) . '</option>' . "\n";
309 } // end for
311 </select>
312 <?php
313 $choices = array(
314 'ASC' => $strAscending,
315 'DESC' => $strDescending
317 PMA_display_html_radio('order', $choices, 'ASC', false, true, "formelement");
318 unset($choices);
320 </fieldset>
321 <br style="clear: both;"/>
322 </div>
323 </fieldset>
324 <fieldset class="tblFooters">
325 <input type="hidden" name="max_number_of_fields"
326 value="<?php echo $fields_cnt; ?>" />
327 <input type="submit" name="submit" value="<?php echo $strGo; ?>" />
328 </fieldset>
329 </form>
330 <?php
331 require_once './libraries/footer.inc.php';
336 * Selection criteria have been submitted -> do the work
338 else {
339 // Builds the query
341 $sql_query = 'SELECT ' . (isset($distinct) ? 'DISTINCT ' : '');
343 // if all fields were selected to display, we do a SELECT *
344 // (more efficient and this helps prevent a problem in IE
345 // if one of the rows is edited and we come back to the Select results)
347 if (count($param) == $max_number_of_fields) {
348 $sql_query .= '* ';
349 } else {
350 $param = PMA_backquote($param);
351 $sql_query .= implode(', ', $param);
352 } // end if
354 // avoid a loop, for example when $cfg['DefaultTabTable'] is set
355 // to 'tbl_select.php'
356 unset($param);
358 $sql_query .= ' FROM ' . PMA_backquote($table);
360 // The where clause
361 if (trim($where) != '') {
362 $sql_query .= ' WHERE ' . $where;
363 } else {
364 $w = $charsets = array();
365 $cnt_func = count($func);
366 reset($func);
367 while (list($i, $func_type) = each($func)) {
368 list($charsets[$i]) = explode('_', $collations[$i]);
369 if (isset($GLOBALS['cfg']['UnaryOperators'][$func_type]) && $GLOBALS['cfg']['UnaryOperators'][$func_type] == 1) {
370 $fields[$i] = '';
371 $w[] = PMA_backquote($names[$i]) . ' ' . $func_type;
373 } elseif (strncasecmp($types[$i], 'enum', 4) == 0) {
374 if (!empty($fields[$i])) {
375 if (!is_array($fields[$i])) {
376 $fields[$i] = explode(',', $fields[$i]);
378 $enum_selected_count = count($fields[$i]);
379 if ($func_type == '=' && $enum_selected_count > 1) {
380 $func_type = $func[$i] = 'IN';
381 $parens_open = '(';
382 $parens_close = ')';
384 } elseif ($func_type == '!=' && $enum_selected_count > 1) {
385 $func_type = $func[$i] = 'NOT IN';
386 $parens_open = '(';
387 $parens_close = ')';
389 } else {
390 $parens_open = '';
391 $parens_close = '';
393 $enum_where = '\'' . PMA_sqlAddslashes($fields[$i][0]) . '\'';
394 for ($e = 1; $e < $enum_selected_count; $e++) {
395 $enum_where .= ', \'' . PMA_sqlAddslashes($fields[$i][$e]) . '\'';
398 $w[] = PMA_backquote($names[$i]) . ' ' . $func_type . ' ' . $parens_open . $enum_where . $parens_close;
401 } elseif ($fields[$i] != '') {
402 // For these types we quote the value. Even if it's another type (like INT),
403 // for a LIKE we always quote the value. MySQL converts strings to numbers
404 // and numbers to strings as necessary during the comparison
405 if (preg_match('@char|binary|blob|text|set|date|time|year@i', $types[$i]) || strpos(' ' . $func_type, 'LIKE')) {
406 $quot = '\'';
407 } else {
408 $quot = '';
411 // LIKE %...%
412 if ($func_type == 'LIKE %...%') {
413 $func_type = 'LIKE';
414 $fields[$i] = '%' . $fields[$i] . '%';
416 if ($func_type == 'REGEXP ^...$') {
417 $func_type = 'REGEXP';
418 $fields[$i] = '^' . $fields[$i] . '$';
421 if ($func_type == 'IN (...)' || $func_type == 'NOT IN (...)' || $func_type == 'BETWEEN' || $func_type == 'NOT BETWEEN') {
422 $func_type = str_replace(' (...)', '', $func_type);
424 // quote values one by one
425 $values = explode(',', $fields[$i]);
426 foreach ($values as &$value)
427 $value = $quot . PMA_sqlAddslashes(trim($value)) . $quot;
429 if ($func_type == 'BETWEEN' || $func_type == 'NOT BETWEEN')
430 $w[] = PMA_backquote($names[$i]) . ' ' . $func_type . ' ' . (isset($values[0]) ? $values[0] : '') . ' AND ' . (isset($values[1]) ? $values[1] : '');
431 else
432 $w[] = PMA_backquote($names[$i]) . ' ' . $func_type . ' (' . implode(',', $values) . ')';
434 else {
435 $w[] = PMA_backquote($names[$i]) . ' ' . $func_type . ' ' . $quot . PMA_sqlAddslashes($fields[$i]) . $quot;;
438 } // end if
439 } // end for
441 if ($w) {
442 $sql_query .= ' WHERE ' . implode(' AND ', $w);
444 } // end if
446 if ($orderField != '--nil--') {
447 $sql_query .= ' ORDER BY ' . PMA_backquote($orderField) . ' ' . $order;
448 } // end if
450 require './sql.php';