Translation update done using Pootle.
[phpmyadmin-themes.git] / tbl_select.php
bloba680c86b83ab028d49b065007ac2e37f852371d5
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 * @package phpMyAdmin
13 /**
14 * Gets some core libraries
16 require_once './libraries/common.inc.php';
17 require_once './libraries/mysql_charsets.lib.php';
19 $GLOBALS['js_include'][] = 'tbl_select.js';
20 $GLOBALS['js_include'][] = 'jquery/jquery-ui-1.8.custom.js';
21 $GLOBALS['js_include'][] = 'jquery/timepicker.js';
22 if ($GLOBALS['cfg']['PropertiesIconic'] == true) {
23 $titles['Browse'] =
24 '<img class="icon" width="16" height="16" src="' . $pmaThemeImage
25 .'b_browse.png" alt="' . __('Browse foreign values') . '" title="'
26 . __('Browse foreign values') . '" />';
28 if ($GLOBALS['cfg']['PropertiesIconic'] === 'both') {
29 $titles['Browse'] .= __('Browse foreign values');
31 } else {
32 $titles['Browse'] = __('Browse foreign values');
35 /**
36 * Not selection yet required -> displays the selection form
38 if (!isset($param) || $param[0] == '') {
39 // Gets some core libraries
40 require_once './libraries/tbl_common.php';
41 //$err_url = 'tbl_select.php' . $err_url;
42 $url_query .= '&amp;goto=tbl_select.php&amp;back=tbl_select.php';
44 /**
45 * Gets tables informations
47 require_once './libraries/tbl_info.inc.php';
49 /**
50 * Displays top menu links
52 require_once './libraries/tbl_links.inc.php';
54 if (!isset($goto)) {
55 $goto = $GLOBALS['cfg']['DefaultTabTable'];
57 // Defines the url to return to in case of error in the next sql statement
58 $err_url = $goto . '?' . PMA_generate_common_url($db, $table);
60 // Gets the list and number of fields
61 $result = PMA_DBI_query('SHOW FULL FIELDS FROM ' . PMA_backquote($table) . ' FROM ' . PMA_backquote($db) . ';', null, PMA_DBI_QUERY_STORE);
62 $fields_cnt = PMA_DBI_num_rows($result);
63 $fields_list = $fields_null = $fields_type = $fields_collation = array();
64 while ($row = PMA_DBI_fetch_assoc($result)) {
65 $fields_list[] = $row['Field'];
66 $type = $row['Type'];
67 // reformat mysql query output
68 if (strncasecmp($type, 'set', 3) == 0
69 || strncasecmp($type, 'enum', 4) == 0) {
70 $type = str_replace(',', ', ', $type);
71 } else {
73 // strip the "BINARY" attribute, except if we find "BINARY(" because
74 // this would be a BINARY or VARBINARY field type
75 if (!preg_match('@BINARY[\(]@i', $type)) {
76 $type = preg_replace('@BINARY@i', '', $type);
78 $type = preg_replace('@ZEROFILL@i', '', $type);
79 $type = preg_replace('@UNSIGNED@i', '', $type);
81 $type = strtolower($type);
83 if (empty($type)) {
84 $type = '&nbsp;';
86 $fields_null[] = $row['Null'];
87 $fields_type[] = $type;
88 $fields_collation[] = !empty($row['Collation']) && $row['Collation'] != 'NULL'
89 ? $row['Collation']
90 : '';
91 } // end while
92 PMA_DBI_free_result($result);
93 unset($result, $type);
95 // retrieve keys into foreign fields, if any
96 // check also foreigners even if relwork is FALSE (to get
97 // foreign keys from innodb)
98 $foreigners = PMA_getForeigners($db, $table);
100 <form method="post" action="tbl_select.php" name="insertForm" id="tbl_search_form">
101 <?php echo PMA_generate_common_hidden_inputs($db, $table); ?>
102 <input type="hidden" name="goto" value="<?php echo $goto; ?>" />
103 <input type="hidden" name="back" value="tbl_select.php" />
105 <fieldset id="fieldset_table_search">
107 <fieldset id="fieldset_table_qbe">
108 <legend><?php echo __('Do a "query by example" (wildcard: "%")') ?></legend>
109 <table class="data">
110 <thead>
111 <tr><th><?php echo __('Column'); ?></th>
112 <th><?php echo __('Type'); ?></th>
113 <th><?php echo __('Collation'); ?></th>
114 <th><?php echo __('Operator'); ?></th>
115 <th><?php echo __('Value'); ?></th>
116 </tr>
117 </thead>
118 <tbody>
119 <?php
120 $odd_row = true;
122 for ($i = 0; $i < $fields_cnt; $i++) {
124 <tr class="<?php echo $odd_row ? 'odd' : 'even'; $odd_row = ! $odd_row; ?>">
125 <th><?php echo htmlspecialchars($fields_list[$i]); ?></th>
126 <td><?php echo $fields_type[$i]; ?></td>
127 <td><?php echo $fields_collation[$i]; ?></td>
128 <td><select name="func[]">
129 <?php
130 if (strncasecmp($fields_type[$i], 'enum', 4) == 0) {
131 foreach ($GLOBALS['cfg']['EnumOperators'] as $fc) {
132 echo "\n" . ' '
133 . '<option value="' . htmlspecialchars($fc) . '">'
134 . htmlspecialchars($fc) . '</option>';
136 } elseif (preg_match('@char|blob|text|set@i', $fields_type[$i])) {
137 foreach ($GLOBALS['cfg']['TextOperators'] as $fc) {
138 echo "\n" . ' '
139 . '<option value="' . htmlspecialchars($fc) . '">'
140 . htmlspecialchars($fc) . '</option>';
142 } else {
143 foreach ($GLOBALS['cfg']['NumOperators'] as $fc) {
144 echo "\n" . ' '
145 . '<option value="' . htmlspecialchars($fc) . '">'
146 . htmlspecialchars($fc) . '</option>';
148 } // end if... else...
149 if ($fields_null[$i]) {
150 foreach ($GLOBALS['cfg']['NullOperators'] as $fc) {
151 echo "\n" . ' '
152 . '<option value="' . htmlspecialchars($fc) . '">'
153 . htmlspecialchars($fc) . '</option>';
158 </select>
159 </td>
160 <td>
161 <?php
162 $field = $fields_list[$i];
164 $foreignData = PMA_getForeignData($foreigners, $field, false, '', '');
166 if ($foreigners && isset($foreigners[$field]) && is_array($foreignData['disp_row'])) {
167 // f o r e i g n k e y s
168 echo ' <select name="fields[' . $i . ']">' . "\n";
169 // go back to first row
171 // here, the 4th parameter is empty because there is no current
172 // value of data for the dropdown (the search page initial values
173 // are displayed empty)
174 echo PMA_foreignDropdown($foreignData['disp_row'],
175 $foreignData['foreign_field'],
176 $foreignData['foreign_display'],
177 '', $GLOBALS['cfg']['ForeignKeyMaxLimit']);
178 echo ' </select>' . "\n";
179 } elseif ($foreignData['foreign_link'] == true) {
181 <input type="text" name="fields[<?php echo $i; ?>]"
182 id="field_<?php echo md5($field); ?>[<?php echo $i; ?>]"
183 class="textfield" />
184 <script type="text/javascript">
185 // <![CDATA[
186 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>');
187 // ]]>
188 </script>
189 <?php
190 } elseif (strncasecmp($fields_type[$i], 'enum', 4) == 0) {
191 // e n u m s
192 $enum_value=explode(', ', str_replace("'", '', substr($fields_type[$i], 5, -1)));
193 $cnt_enum_value = count($enum_value);
194 echo ' <select name="fields[' . $i . '][]"'
195 .' multiple="multiple" size="' . min(3, $cnt_enum_value) . '">' . "\n";
196 for ($j = 0; $j < $cnt_enum_value; $j++) {
197 echo ' <option value="' . $enum_value[$j] . '">'
198 . $enum_value[$j] . '</option>';
199 } // end for
200 echo ' </select>' . "\n";
201 } else {
202 // o t h e r c a s e s
203 echo ' <input type="text" name="fields[' . $i . ']"'
204 .' size="40" class="textfield" id="field_' . $i . '" />' . "\n";
206 $type = $fields_type[$i];
207 if ($type == 'date' || $type == 'datetime' || substr($type, 0, 9) == 'timestamp') {
209 <script type="text/javascript">
210 //<![CDATA[
211 $(function() {
212 $('#field_<?php echo $i; ?>').datepicker({
213 duration: '',
214 time24h: true,
215 stepMinutes: 1,
216 stepHours: 1,
217 <?php echo ($type == 'date' ? "showTime: false,":"showTime: true,"); ?>
218 altTimeField: '',
219 constrainInput: false
222 //]]>
223 </script>
224 <?php
227 <input type="hidden" name="names[<?php echo $i; ?>]"
228 value="<?php echo htmlspecialchars($fields_list[$i]); ?>" />
229 <input type="hidden" name="types[<?php echo $i; ?>]"
230 value="<?php echo $fields_type[$i]; ?>" />
231 <input type="hidden" name="collations[<?php echo $i; ?>]"
232 value="<?php echo $fields_collation[$i]; ?>" />
233 </td>
234 </tr>
235 <?php
236 } // end for
238 </tbody>
239 </table>
240 </fieldset>
241 <?php
242 PMA_generate_slider_effect('searchoptions', __('Options'));
244 <fieldset id="fieldset_select_fields">
245 <legend><?php echo __('Select columns (at least one):'); ?></legend>
246 <select name="param[]" size="<?php echo min($fields_cnt, 10); ?>"
247 multiple="multiple">
248 <?php
249 // Displays the list of the fields
250 foreach ($fields_list as $each_field) {
251 echo ' '
252 .'<option value="' . htmlspecialchars($each_field) . '"'
253 .' selected="selected">' . htmlspecialchars($each_field)
254 .'</option>' . "\n";
257 </select>
258 <input type="checkbox" name="distinct" value="DISTINCT" id="oDistinct" />
259 <label for="oDistinct">DISTINCT</label>
260 </fieldset>
262 <fieldset id="fieldset_search_conditions">
263 <legend><?php echo '<em>' . __('Or') . '</em> ' . __('Add search conditions (body of the "where" clause):'); ?></legend>
264 <?php echo PMA_showMySQLDocu('SQL-Syntax', 'Functions'); ?>
266 <input type="text" name="where" class="textfield" size="64" />
267 </fieldset>
269 <fieldset id="fieldset_limit_rows">
270 <legend><?php echo __('Number of rows per page'); ?></legend>
271 <input type="text" size="4" name="session_max_rows"
272 value="<?php echo $GLOBALS['cfg']['MaxRows']; ?>" class="textfield" />
273 </fieldset>
275 <fieldset id="fieldset_display_order">
276 <legend><?php echo __('Display order:'); ?></legend>
277 <select name="orderField">
278 <option value="--nil--"></option>
279 <?php
280 foreach ($fields_list as $each_field) {
281 echo ' '
282 .'<option value="' . htmlspecialchars($each_field) . '">'
283 .htmlspecialchars($each_field) . '</option>' . "\n";
284 } // end for
286 </select>
287 <?php
288 $choices = array(
289 'ASC' => __('Ascending'),
290 'DESC' => __('Descending')
292 PMA_display_html_radio('order', $choices, 'ASC', false, true, "formelement");
293 unset($choices);
295 </fieldset>
296 <br style="clear: both;"/>
297 </div>
298 </fieldset>
299 <fieldset class="tblFooters">
300 <input type="hidden" name="max_number_of_fields"
301 value="<?php echo $fields_cnt; ?>" />
302 <input type="submit" name="submit" value="<?php echo __('Go'); ?>" />
303 </fieldset>
304 </form>
305 <div id="searchresults"></div>
306 <?php
307 require './libraries/footer.inc.php';
312 * Selection criteria have been submitted -> do the work
314 else {
315 // Builds the query
317 $sql_query = 'SELECT ' . (isset($distinct) ? 'DISTINCT ' : '');
319 // if all fields were selected to display, we do a SELECT *
320 // (more efficient and this helps prevent a problem in IE
321 // if one of the rows is edited and we come back to the Select results)
323 if (count($param) == $max_number_of_fields) {
324 $sql_query .= '* ';
325 } else {
326 $param = PMA_backquote($param);
327 $sql_query .= implode(', ', $param);
328 } // end if
330 // avoid a loop, for example when $cfg['DefaultTabTable'] is set
331 // to 'tbl_select.php'
332 unset($param);
334 $sql_query .= ' FROM ' . PMA_backquote($table);
336 // The where clause
337 if (trim($where) != '') {
338 $sql_query .= ' WHERE ' . $where;
339 } else {
340 $w = $charsets = array();
341 $cnt_func = count($func);
342 reset($func);
343 while (list($i, $func_type) = each($func)) {
344 list($charsets[$i]) = explode('_', $collations[$i]);
345 if (isset($GLOBALS['cfg']['UnaryOperators'][$func_type]) && $GLOBALS['cfg']['UnaryOperators'][$func_type] == 1) {
346 $fields[$i] = '';
347 $w[] = PMA_backquote($names[$i]) . ' ' . $func_type;
349 } elseif (strncasecmp($types[$i], 'enum', 4) == 0) {
350 if (!empty($fields[$i])) {
351 if (!is_array($fields[$i])) {
352 $fields[$i] = explode(',', $fields[$i]);
354 $enum_selected_count = count($fields[$i]);
355 if ($func_type == '=' && $enum_selected_count > 1) {
356 $func_type = $func[$i] = 'IN';
357 $parens_open = '(';
358 $parens_close = ')';
360 } elseif ($func_type == '!=' && $enum_selected_count > 1) {
361 $func_type = $func[$i] = 'NOT IN';
362 $parens_open = '(';
363 $parens_close = ')';
365 } else {
366 $parens_open = '';
367 $parens_close = '';
369 $enum_where = '\'' . PMA_sqlAddslashes($fields[$i][0]) . '\'';
370 for ($e = 1; $e < $enum_selected_count; $e++) {
371 $enum_where .= ', \'' . PMA_sqlAddslashes($fields[$i][$e]) . '\'';
374 $w[] = PMA_backquote($names[$i]) . ' ' . $func_type . ' ' . $parens_open . $enum_where . $parens_close;
377 } elseif ($fields[$i] != '') {
378 // For these types we quote the value. Even if it's another type (like INT),
379 // for a LIKE we always quote the value. MySQL converts strings to numbers
380 // and numbers to strings as necessary during the comparison
381 if (preg_match('@char|binary|blob|text|set|date|time|year@i', $types[$i]) || strpos(' ' . $func_type, 'LIKE')) {
382 $quot = '\'';
383 } else {
384 $quot = '';
387 // LIKE %...%
388 if ($func_type == 'LIKE %...%') {
389 $func_type = 'LIKE';
390 $fields[$i] = '%' . $fields[$i] . '%';
392 if ($func_type == 'REGEXP ^...$') {
393 $func_type = 'REGEXP';
394 $fields[$i] = '^' . $fields[$i] . '$';
397 if ($func_type == 'IN (...)' || $func_type == 'NOT IN (...)' || $func_type == 'BETWEEN' || $func_type == 'NOT BETWEEN') {
398 $func_type = str_replace(' (...)', '', $func_type);
400 // quote values one by one
401 $values = explode(',', $fields[$i]);
402 foreach ($values as &$value)
403 $value = $quot . PMA_sqlAddslashes(trim($value)) . $quot;
405 if ($func_type == 'BETWEEN' || $func_type == 'NOT BETWEEN')
406 $w[] = PMA_backquote($names[$i]) . ' ' . $func_type . ' ' . (isset($values[0]) ? $values[0] : '') . ' AND ' . (isset($values[1]) ? $values[1] : '');
407 else
408 $w[] = PMA_backquote($names[$i]) . ' ' . $func_type . ' (' . implode(',', $values) . ')';
410 else {
411 $w[] = PMA_backquote($names[$i]) . ' ' . $func_type . ' ' . $quot . PMA_sqlAddslashes($fields[$i]) . $quot;;
414 } // end if
415 } // end for
417 if ($w) {
418 $sql_query .= ' WHERE ' . implode(' AND ', $w);
420 } // end if
422 if ($orderField != '--nil--') {
423 $sql_query .= ' ORDER BY ' . PMA_backquote($orderField) . ' ' . $order;
424 } // end if
426 require './sql.php';