Translation update done using Pootle.
[phpmyadmin-themes.git] / tbl_select.php
blob0d8cbb77701d0925fc6591e7a509026271f0858f
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'][] = 'sql.js';
20 $GLOBALS['js_include'][] = 'tbl_select.js';
21 $GLOBALS['js_include'][] = 'jquery/jquery-ui-1.8.custom.js';
22 $GLOBALS['js_include'][] = 'jquery/timepicker.js';
23 if ($GLOBALS['cfg']['PropertiesIconic'] == true) {
24 $titles['Browse'] =
25 '<img class="icon" width="16" height="16" src="' . $pmaThemeImage
26 .'b_browse.png" alt="' . __('Browse foreign values') . '" title="'
27 . __('Browse foreign values') . '" />';
29 if ($GLOBALS['cfg']['PropertiesIconic'] === 'both') {
30 $titles['Browse'] .= __('Browse foreign values');
32 } else {
33 $titles['Browse'] = __('Browse foreign values');
36 /**
37 * Not selection yet required -> displays the selection form
39 if (!isset($param) || $param[0] == '') {
40 // Gets some core libraries
41 require_once './libraries/tbl_common.php';
42 //$err_url = 'tbl_select.php' . $err_url;
43 $url_query .= '&amp;goto=tbl_select.php&amp;back=tbl_select.php';
45 /**
46 * Gets tables informations
48 require_once './libraries/tbl_info.inc.php';
50 /**
51 * Displays top menu links
53 require_once './libraries/tbl_links.inc.php';
55 if (!isset($goto)) {
56 $goto = $GLOBALS['cfg']['DefaultTabTable'];
58 // Defines the url to return to in case of error in the next sql statement
59 $err_url = $goto . '?' . PMA_generate_common_url($db, $table);
61 // Gets the list and number of fields
62 $result = PMA_DBI_query('SHOW FULL FIELDS FROM ' . PMA_backquote($table) . ' FROM ' . PMA_backquote($db) . ';', null, PMA_DBI_QUERY_STORE);
63 $fields_cnt = PMA_DBI_num_rows($result);
64 $fields_list = $fields_null = $fields_type = $fields_collation = array();
65 while ($row = PMA_DBI_fetch_assoc($result)) {
66 $fields_list[] = $row['Field'];
67 $type = $row['Type'];
68 // reformat mysql query output
69 if (strncasecmp($type, 'set', 3) == 0
70 || strncasecmp($type, 'enum', 4) == 0) {
71 $type = str_replace(',', ', ', $type);
72 } else {
74 // strip the "BINARY" attribute, except if we find "BINARY(" because
75 // this would be a BINARY or VARBINARY field type
76 if (!preg_match('@BINARY[\(]@i', $type)) {
77 $type = preg_replace('@BINARY@i', '', $type);
79 $type = preg_replace('@ZEROFILL@i', '', $type);
80 $type = preg_replace('@UNSIGNED@i', '', $type);
82 $type = strtolower($type);
84 if (empty($type)) {
85 $type = '&nbsp;';
87 $fields_null[] = $row['Null'];
88 $fields_type[] = $type;
89 $fields_collation[] = !empty($row['Collation']) && $row['Collation'] != 'NULL'
90 ? $row['Collation']
91 : '';
92 } // end while
93 PMA_DBI_free_result($result);
94 unset($result, $type);
96 // retrieve keys into foreign fields, if any
97 // check also foreigners even if relwork is FALSE (to get
98 // foreign keys from innodb)
99 $foreigners = PMA_getForeigners($db, $table);
101 <form method="post" action="tbl_select.php" name="insertForm" id="tbl_search_form" <?php echo ($GLOBALS['cfg']['AjaxEnable'] ? ' class="ajax"' : ''); ?>>
102 <?php echo PMA_generate_common_hidden_inputs($db, $table); ?>
103 <input type="hidden" name="goto" value="<?php echo $goto; ?>" />
104 <input type="hidden" name="back" value="tbl_select.php" />
106 <fieldset id="fieldset_table_search">
108 <fieldset id="fieldset_table_qbe">
109 <legend><?php echo __('Do a "query by example" (wildcard: "%")') ?></legend>
110 <table class="data">
111 <thead>
112 <tr><th><?php echo __('Column'); ?></th>
113 <th><?php echo __('Type'); ?></th>
114 <th><?php echo __('Collation'); ?></th>
115 <th><?php echo __('Operator'); ?></th>
116 <th><?php echo __('Value'); ?></th>
117 </tr>
118 </thead>
119 <tbody>
120 <?php
121 $odd_row = true;
123 for ($i = 0; $i < $fields_cnt; $i++) {
125 <tr class="<?php echo $odd_row ? 'odd' : 'even'; $odd_row = ! $odd_row; ?>">
126 <th><?php echo htmlspecialchars($fields_list[$i]); ?></th>
127 <td><?php echo $fields_type[$i]; ?></td>
128 <td><?php echo $fields_collation[$i]; ?></td>
129 <td><select name="func[]">
130 <?php
131 if (strncasecmp($fields_type[$i], 'enum', 4) == 0) {
132 foreach ($GLOBALS['cfg']['EnumOperators'] as $fc) {
133 echo "\n" . ' '
134 . '<option value="' . htmlspecialchars($fc) . '">'
135 . htmlspecialchars($fc) . '</option>';
137 } elseif (preg_match('@char|blob|text|set@i', $fields_type[$i])) {
138 foreach ($GLOBALS['cfg']['TextOperators'] as $fc) {
139 echo "\n" . ' '
140 . '<option value="' . htmlspecialchars($fc) . '">'
141 . htmlspecialchars($fc) . '</option>';
143 } else {
144 foreach ($GLOBALS['cfg']['NumOperators'] as $fc) {
145 echo "\n" . ' '
146 . '<option value="' . htmlspecialchars($fc) . '">'
147 . htmlspecialchars($fc) . '</option>';
149 } // end if... else...
150 if ($fields_null[$i]) {
151 foreach ($GLOBALS['cfg']['NullOperators'] as $fc) {
152 echo "\n" . ' '
153 . '<option value="' . htmlspecialchars($fc) . '">'
154 . htmlspecialchars($fc) . '</option>';
159 </select>
160 </td>
161 <td>
162 <?php
163 $field = $fields_list[$i];
165 $foreignData = PMA_getForeignData($foreigners, $field, false, '', '');
167 if ($foreigners && isset($foreigners[$field]) && is_array($foreignData['disp_row'])) {
168 // f o r e i g n k e y s
169 echo ' <select name="fields[' . $i . ']">' . "\n";
170 // go back to first row
172 // here, the 4th parameter is empty because there is no current
173 // value of data for the dropdown (the search page initial values
174 // are displayed empty)
175 echo PMA_foreignDropdown($foreignData['disp_row'],
176 $foreignData['foreign_field'],
177 $foreignData['foreign_display'],
178 '', $GLOBALS['cfg']['ForeignKeyMaxLimit']);
179 echo ' </select>' . "\n";
180 } elseif ($foreignData['foreign_link'] == true) {
182 <input type="text" name="fields[<?php echo $i; ?>]"
183 id="field_<?php echo md5($field); ?>[<?php echo $i; ?>]"
184 class="textfield" />
185 <script type="text/javascript">
186 // <![CDATA[
187 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>');
188 // ]]>
189 </script>
190 <?php
191 } elseif (strncasecmp($fields_type[$i], 'enum', 4) == 0) {
192 // e n u m s
193 $enum_value=explode(', ', str_replace("'", '', substr($fields_type[$i], 5, -1)));
194 $cnt_enum_value = count($enum_value);
195 echo ' <select name="fields[' . $i . '][]"'
196 .' multiple="multiple" size="' . min(3, $cnt_enum_value) . '">' . "\n";
197 for ($j = 0; $j < $cnt_enum_value; $j++) {
198 echo ' <option value="' . $enum_value[$j] . '">'
199 . $enum_value[$j] . '</option>';
200 } // end for
201 echo ' </select>' . "\n";
202 } else {
203 // o t h e r c a s e s
204 $the_class = 'textfield';
205 $type = $fields_type[$i];
206 if ($type == 'date') {
207 $the_class .= ' datefield';
208 } elseif ($type == 'datetime' || substr($type, 0, 9) == 'timestamp') {
209 $the_class .= ' datetimefield';
211 echo ' <input type="text" name="fields[' . $i . ']"'
212 .' size="40" class="' . $the_class . '" id="field_' . $i . '" />' . "\n";
215 <input type="hidden" name="names[<?php echo $i; ?>]"
216 value="<?php echo htmlspecialchars($fields_list[$i]); ?>" />
217 <input type="hidden" name="types[<?php echo $i; ?>]"
218 value="<?php echo $fields_type[$i]; ?>" />
219 <input type="hidden" name="collations[<?php echo $i; ?>]"
220 value="<?php echo $fields_collation[$i]; ?>" />
221 </td>
222 </tr>
223 <?php
224 } // end for
226 </tbody>
227 </table>
228 </fieldset>
229 <?php
230 PMA_generate_slider_effect('searchoptions', __('Options'));
232 <fieldset id="fieldset_select_fields">
233 <legend><?php echo __('Select columns (at least one):'); ?></legend>
234 <select name="param[]" size="<?php echo min($fields_cnt, 10); ?>"
235 multiple="multiple">
236 <?php
237 // Displays the list of the fields
238 foreach ($fields_list as $each_field) {
239 echo ' '
240 .'<option value="' . htmlspecialchars($each_field) . '"'
241 .' selected="selected">' . htmlspecialchars($each_field)
242 .'</option>' . "\n";
245 </select>
246 <input type="checkbox" name="distinct" value="DISTINCT" id="oDistinct" />
247 <label for="oDistinct">DISTINCT</label>
248 </fieldset>
250 <fieldset id="fieldset_search_conditions">
251 <legend><?php echo '<em>' . __('Or') . '</em> ' . __('Add search conditions (body of the "where" clause):'); ?></legend>
252 <?php echo PMA_showMySQLDocu('SQL-Syntax', 'Functions'); ?>
254 <input type="text" name="where" class="textfield" size="64" />
255 </fieldset>
257 <fieldset id="fieldset_limit_rows">
258 <legend><?php echo __('Number of rows per page'); ?></legend>
259 <input type="text" size="4" name="session_max_rows"
260 value="<?php echo $GLOBALS['cfg']['MaxRows']; ?>" class="textfield" />
261 </fieldset>
263 <fieldset id="fieldset_display_order">
264 <legend><?php echo __('Display order:'); ?></legend>
265 <select name="orderField">
266 <option value="--nil--"></option>
267 <?php
268 foreach ($fields_list as $each_field) {
269 echo ' '
270 .'<option value="' . htmlspecialchars($each_field) . '">'
271 .htmlspecialchars($each_field) . '</option>' . "\n";
272 } // end for
274 </select>
275 <?php
276 $choices = array(
277 'ASC' => __('Ascending'),
278 'DESC' => __('Descending')
280 PMA_display_html_radio('order', $choices, 'ASC', false, true, "formelement");
281 unset($choices);
283 </fieldset>
284 <br style="clear: both;"/>
285 </div>
286 </fieldset>
287 <fieldset class="tblFooters">
288 <input type="hidden" name="max_number_of_fields"
289 value="<?php echo $fields_cnt; ?>" />
290 <input type="submit" name="submit" value="<?php echo __('Go'); ?>" />
291 </fieldset>
292 </form>
293 <div id="sqlqueryresults"></div>
294 <?php
295 require './libraries/footer.inc.php';
300 * Selection criteria have been submitted -> do the work
302 else {
303 // Builds the query
305 $sql_query = 'SELECT ' . (isset($distinct) ? 'DISTINCT ' : '');
307 // if all fields were selected to display, we do a SELECT *
308 // (more efficient and this helps prevent a problem in IE
309 // if one of the rows is edited and we come back to the Select results)
311 if (count($param) == $max_number_of_fields) {
312 $sql_query .= '* ';
313 } else {
314 $param = PMA_backquote($param);
315 $sql_query .= implode(', ', $param);
316 } // end if
318 // avoid a loop, for example when $cfg['DefaultTabTable'] is set
319 // to 'tbl_select.php'
320 unset($param);
322 $sql_query .= ' FROM ' . PMA_backquote($table);
324 // The where clause
325 if (trim($where) != '') {
326 $sql_query .= ' WHERE ' . $where;
327 } else {
328 $w = $charsets = array();
329 $cnt_func = count($func);
330 reset($func);
331 while (list($i, $func_type) = each($func)) {
332 list($charsets[$i]) = explode('_', $collations[$i]);
333 if (isset($GLOBALS['cfg']['UnaryOperators'][$func_type]) && $GLOBALS['cfg']['UnaryOperators'][$func_type] == 1) {
334 $fields[$i] = '';
335 $w[] = PMA_backquote($names[$i]) . ' ' . $func_type;
337 } elseif (strncasecmp($types[$i], 'enum', 4) == 0) {
338 if (!empty($fields[$i])) {
339 if (!is_array($fields[$i])) {
340 $fields[$i] = explode(',', $fields[$i]);
342 $enum_selected_count = count($fields[$i]);
343 if ($func_type == '=' && $enum_selected_count > 1) {
344 $func_type = $func[$i] = 'IN';
345 $parens_open = '(';
346 $parens_close = ')';
348 } elseif ($func_type == '!=' && $enum_selected_count > 1) {
349 $func_type = $func[$i] = 'NOT IN';
350 $parens_open = '(';
351 $parens_close = ')';
353 } else {
354 $parens_open = '';
355 $parens_close = '';
357 $enum_where = '\'' . PMA_sqlAddslashes($fields[$i][0]) . '\'';
358 for ($e = 1; $e < $enum_selected_count; $e++) {
359 $enum_where .= ', \'' . PMA_sqlAddslashes($fields[$i][$e]) . '\'';
362 $w[] = PMA_backquote($names[$i]) . ' ' . $func_type . ' ' . $parens_open . $enum_where . $parens_close;
365 } elseif ($fields[$i] != '') {
366 // For these types we quote the value. Even if it's another type (like INT),
367 // for a LIKE we always quote the value. MySQL converts strings to numbers
368 // and numbers to strings as necessary during the comparison
369 if (preg_match('@char|binary|blob|text|set|date|time|year@i', $types[$i]) || strpos(' ' . $func_type, 'LIKE')) {
370 $quot = '\'';
371 } else {
372 $quot = '';
375 // LIKE %...%
376 if ($func_type == 'LIKE %...%') {
377 $func_type = 'LIKE';
378 $fields[$i] = '%' . $fields[$i] . '%';
380 if ($func_type == 'REGEXP ^...$') {
381 $func_type = 'REGEXP';
382 $fields[$i] = '^' . $fields[$i] . '$';
385 if ($func_type == 'IN (...)' || $func_type == 'NOT IN (...)' || $func_type == 'BETWEEN' || $func_type == 'NOT BETWEEN') {
386 $func_type = str_replace(' (...)', '', $func_type);
388 // quote values one by one
389 $values = explode(',', $fields[$i]);
390 foreach ($values as &$value)
391 $value = $quot . PMA_sqlAddslashes(trim($value)) . $quot;
393 if ($func_type == 'BETWEEN' || $func_type == 'NOT BETWEEN')
394 $w[] = PMA_backquote($names[$i]) . ' ' . $func_type . ' ' . (isset($values[0]) ? $values[0] : '') . ' AND ' . (isset($values[1]) ? $values[1] : '');
395 else
396 $w[] = PMA_backquote($names[$i]) . ' ' . $func_type . ' (' . implode(',', $values) . ')';
398 else {
399 $w[] = PMA_backquote($names[$i]) . ' ' . $func_type . ' ' . $quot . PMA_sqlAddslashes($fields[$i]) . $quot;;
402 } // end if
403 } // end for
405 if ($w) {
406 $sql_query .= ' WHERE ' . implode(' AND ', $w);
408 } // end if
410 if ($orderField != '--nil--') {
411 $sql_query .= ' ORDER BY ' . PMA_backquote($orderField) . ' ' . $order;
412 } // end if
414 require './sql.php';