Updated MySQL function list
[phpmyadmin.git] / tbl_select.php
blob33b7e48dad7f6a438f4a229cbf9cdbcb6d1c4038
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'][] = 'makegrid.js';
20 $GLOBALS['js_include'][] = 'sql.js';
21 $GLOBALS['js_include'][] = 'tbl_select.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="' . __('Browse foreign values') . '" title="'
28 . __('Browse foreign values') . '" />';
30 if ($GLOBALS['cfg']['PropertiesIconic'] === 'both') {
31 $titles['Browse'] .= __('Browse foreign values');
33 } else {
34 $titles['Browse'] = __('Browse foreign values');
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 $fields_list = $fields_null = $fields_type = $fields_collation = array();
66 while ($row = PMA_DBI_fetch_assoc($result)) {
67 $fields_list[] = $row['Field'];
68 $type = $row['Type'];
69 // reformat mysql query output
70 if (strncasecmp($type, 'set', 3) == 0
71 || strncasecmp($type, 'enum', 4) == 0) {
72 $type = str_replace(',', ', ', $type);
73 } else {
75 // strip the "BINARY" attribute, except if we find "BINARY(" because
76 // this would be a BINARY or VARBINARY field type
77 if (!preg_match('@BINARY[\(]@i', $type)) {
78 $type = preg_replace('@BINARY@i', '', $type);
80 $type = preg_replace('@ZEROFILL@i', '', $type);
81 $type = preg_replace('@UNSIGNED@i', '', $type);
83 $type = strtolower($type);
85 if (empty($type)) {
86 $type = '&nbsp;';
88 $fields_null[] = $row['Null'];
89 $fields_type[] = $type;
90 $fields_collation[] = !empty($row['Collation']) && $row['Collation'] != 'NULL'
91 ? $row['Collation']
92 : '';
93 } // end while
94 PMA_DBI_free_result($result);
95 unset($result, $type);
97 // retrieve keys into foreign fields, if any
98 // check also foreigners even if relwork is FALSE (to get
99 // foreign keys from innodb)
100 $foreigners = PMA_getForeigners($db, $table);
102 <form method="post" action="tbl_select.php" name="insertForm" id="tbl_search_form" <?php echo ($GLOBALS['cfg']['AjaxEnable'] ? ' class="ajax"' : ''); ?>>
103 <?php echo PMA_generate_common_hidden_inputs($db, $table); ?>
104 <input type="hidden" name="goto" value="<?php echo $goto; ?>" />
105 <input type="hidden" name="back" value="tbl_select.php" />
107 <fieldset id="fieldset_table_search">
109 <fieldset id="fieldset_table_qbe">
110 <legend><?php echo __('Do a "query by example" (wildcard: "%")') ?></legend>
111 <table class="data">
112 <thead>
113 <tr><th><?php echo __('Column'); ?></th>
114 <th><?php echo __('Type'); ?></th>
115 <th><?php echo __('Collation'); ?></th>
116 <th><?php echo __('Operator'); ?></th>
117 <th><?php echo __('Value'); ?></th>
118 </tr>
119 </thead>
120 <tbody>
121 <?php
122 $odd_row = true;
124 for ($i = 0; $i < $fields_cnt; $i++) {
126 <tr class="noclick <?php echo $odd_row ? 'odd' : 'even'; $odd_row = ! $odd_row; ?>">
127 <th><?php echo htmlspecialchars($fields_list[$i]); ?></th>
128 <td><?php echo $fields_type[$i]; ?></td>
129 <td><?php echo $fields_collation[$i]; ?></td>
130 <td><select name="func[]">
131 <?php
132 if (strncasecmp($fields_type[$i], 'enum', 4) == 0) {
133 foreach ($GLOBALS['cfg']['EnumOperators'] as $fc) {
134 echo "\n" . ' '
135 . '<option value="' . htmlspecialchars($fc) . '">'
136 . htmlspecialchars($fc) . '</option>';
138 } elseif (preg_match('@char|blob|text|set@i', $fields_type[$i])) {
139 foreach ($GLOBALS['cfg']['TextOperators'] as $fc) {
140 echo "\n" . ' '
141 . '<option value="' . htmlspecialchars($fc) . '">'
142 . htmlspecialchars($fc) . '</option>';
144 } else {
145 foreach ($GLOBALS['cfg']['NumOperators'] as $fc) {
146 echo "\n" . ' '
147 . '<option value="' . htmlspecialchars($fc) . '">'
148 . htmlspecialchars($fc) . '</option>';
150 } // end if... else...
151 if ($fields_null[$i]) {
152 foreach ($GLOBALS['cfg']['NullOperators'] as $fc) {
153 echo "\n" . ' '
154 . '<option value="' . htmlspecialchars($fc) . '">'
155 . htmlspecialchars($fc) . '</option>';
160 </select>
161 </td>
162 <td>
163 <?php
164 $field = $fields_list[$i];
166 $foreignData = PMA_getForeignData($foreigners, $field, false, '', '');
168 if ($foreigners && isset($foreigners[$field]) && is_array($foreignData['disp_row'])) {
169 // f o r e i g n k e y s
170 echo ' <select name="fields[' . $i . ']">' . "\n";
171 // go back to first row
173 // here, the 4th parameter is empty because there is no current
174 // value of data for the dropdown (the search page initial values
175 // are displayed empty)
176 echo PMA_foreignDropdown($foreignData['disp_row'],
177 $foreignData['foreign_field'],
178 $foreignData['foreign_display'],
179 '', $GLOBALS['cfg']['ForeignKeyMaxLimit']);
180 echo ' </select>' . "\n";
181 } elseif ($foreignData['foreign_link'] == true) {
183 <input type="text" name="fields[<?php echo $i; ?>]"
184 id="field_<?php echo md5($field); ?>[<?php echo $i; ?>]"
185 class="textfield" />
186 <script type="text/javascript">
187 // <![CDATA[
188 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>');
189 // ]]>
190 </script>
191 <?php
192 } elseif (strncasecmp($fields_type[$i], 'enum', 4) == 0) {
193 // e n u m s
194 $enum_value=explode(', ', str_replace("'", '', substr($fields_type[$i], 5, -1)));
195 $cnt_enum_value = count($enum_value);
196 echo ' <select name="fields[' . $i . '][]"'
197 .' multiple="multiple" size="' . min(3, $cnt_enum_value) . '">' . "\n";
198 for ($j = 0; $j < $cnt_enum_value; $j++) {
199 echo ' <option value="' . $enum_value[$j] . '">'
200 . $enum_value[$j] . '</option>';
201 } // end for
202 echo ' </select>' . "\n";
203 } else {
204 // o t h e r c a s e s
205 $the_class = 'textfield';
206 $type = $fields_type[$i];
207 if ($type == 'date') {
208 $the_class .= ' datefield';
209 } elseif ($type == 'datetime' || substr($type, 0, 9) == 'timestamp') {
210 $the_class .= ' datetimefield';
212 echo ' <input type="text" name="fields[' . $i . ']"'
213 .' size="40" class="' . $the_class . '" id="field_' . $i . '" />' . "\n";
216 <input type="hidden" name="names[<?php echo $i; ?>]"
217 value="<?php echo htmlspecialchars($fields_list[$i]); ?>" />
218 <input type="hidden" name="types[<?php echo $i; ?>]"
219 value="<?php echo $fields_type[$i]; ?>" />
220 <input type="hidden" name="collations[<?php echo $i; ?>]"
221 value="<?php echo $fields_collation[$i]; ?>" />
222 </td>
223 </tr>
224 <?php
225 } // end for
227 </tbody>
228 </table>
229 </fieldset>
230 <?php
231 PMA_generate_slider_effect('searchoptions', __('Options'));
233 <fieldset id="fieldset_select_fields">
234 <legend><?php echo __('Select columns (at least one):'); ?></legend>
235 <select name="param[]" size="<?php echo min($fields_cnt, 10); ?>"
236 multiple="multiple">
237 <?php
238 // Displays the list of the fields
239 foreach ($fields_list as $each_field) {
240 echo ' '
241 .'<option value="' . htmlspecialchars($each_field) . '"'
242 .' selected="selected">' . htmlspecialchars($each_field)
243 .'</option>' . "\n";
246 </select>
247 <input type="checkbox" name="distinct" value="DISTINCT" id="oDistinct" />
248 <label for="oDistinct">DISTINCT</label>
249 </fieldset>
251 <fieldset id="fieldset_search_conditions">
252 <legend><?php echo '<em>' . __('Or') . '</em> ' . __('Add search conditions (body of the "where" clause):'); ?></legend>
253 <?php echo PMA_showMySQLDocu('SQL-Syntax', 'Functions'); ?>
255 <input type="text" name="where" class="textfield" size="64" />
256 </fieldset>
258 <fieldset id="fieldset_limit_rows">
259 <legend><?php echo __('Number of rows per page'); ?></legend>
260 <input type="text" size="4" name="session_max_rows"
261 value="<?php echo $GLOBALS['cfg']['MaxRows']; ?>" class="textfield" />
262 </fieldset>
264 <fieldset id="fieldset_display_order">
265 <legend><?php echo __('Display order:'); ?></legend>
266 <select name="orderField">
267 <option value="--nil--"></option>
268 <?php
269 foreach ($fields_list as $each_field) {
270 echo ' '
271 .'<option value="' . htmlspecialchars($each_field) . '">'
272 .htmlspecialchars($each_field) . '</option>' . "\n";
273 } // end for
275 </select>
276 <?php
277 $choices = array(
278 'ASC' => __('Ascending'),
279 'DESC' => __('Descending')
281 PMA_display_html_radio('order', $choices, 'ASC', false, true, "formelement");
282 unset($choices);
284 </fieldset>
285 <br style="clear: both;"/>
286 </div>
287 </fieldset>
288 <fieldset class="tblFooters">
289 <input type="hidden" name="max_number_of_fields"
290 value="<?php echo $fields_cnt; ?>" />
291 <input type="submit" name="submit" value="<?php echo __('Go'); ?>" />
292 </fieldset>
293 </form>
294 <div id="sqlqueryresults"></div>
295 <?php
296 require './libraries/footer.inc.php';
301 * Selection criteria have been submitted -> do the work
303 else {
304 // Builds the query
306 $sql_query = 'SELECT ' . (isset($distinct) ? 'DISTINCT ' : '');
308 // if all fields were selected to display, we do a SELECT *
309 // (more efficient and this helps prevent a problem in IE
310 // if one of the rows is edited and we come back to the Select results)
312 if (count($param) == $max_number_of_fields) {
313 $sql_query .= '* ';
314 } else {
315 $param = PMA_backquote($param);
316 $sql_query .= implode(', ', $param);
317 } // end if
319 // avoid a loop, for example when $cfg['DefaultTabTable'] is set
320 // to 'tbl_select.php'
321 unset($param);
323 $sql_query .= ' FROM ' . PMA_backquote($table);
325 // The where clause
326 if (trim($where) != '') {
327 $sql_query .= ' WHERE ' . $where;
328 } else {
329 $w = $charsets = array();
330 $cnt_func = count($func);
331 reset($func);
332 while (list($i, $func_type) = each($func)) {
333 list($charsets[$i]) = explode('_', $collations[$i]);
334 if (isset($GLOBALS['cfg']['UnaryOperators'][$func_type]) && $GLOBALS['cfg']['UnaryOperators'][$func_type] == 1) {
335 $fields[$i] = '';
336 $w[] = PMA_backquote($names[$i]) . ' ' . $func_type;
338 } elseif (strncasecmp($types[$i], 'enum', 4) == 0) {
339 if (!empty($fields[$i])) {
340 if (! is_array($fields[$i])) {
341 $fields[$i] = explode(',', $fields[$i]);
343 $enum_selected_count = count($fields[$i]);
344 if ($func_type == '=' && $enum_selected_count > 1) {
345 $func_type = $func[$i] = 'IN';
346 $parens_open = '(';
347 $parens_close = ')';
349 } elseif ($func_type == '!=' && $enum_selected_count > 1) {
350 $func_type = $func[$i] = 'NOT IN';
351 $parens_open = '(';
352 $parens_close = ')';
354 } else {
355 $parens_open = '';
356 $parens_close = '';
358 $enum_where = '\'' . PMA_sqlAddSlashes($fields[$i][0]) . '\'';
359 for ($e = 1; $e < $enum_selected_count; $e++) {
360 $enum_where .= ', \'' . PMA_sqlAddSlashes($fields[$i][$e]) . '\'';
363 $w[] = PMA_backquote($names[$i]) . ' ' . $func_type . ' ' . $parens_open . $enum_where . $parens_close;
366 } elseif ($fields[$i] != '') {
367 // For these types we quote the value. Even if it's another type (like INT),
368 // for a LIKE we always quote the value. MySQL converts strings to numbers
369 // and numbers to strings as necessary during the comparison
370 if (preg_match('@char|binary|blob|text|set|date|time|year@i', $types[$i]) || strpos(' ' . $func_type, 'LIKE')) {
371 $quot = '\'';
372 } else {
373 $quot = '';
376 // LIKE %...%
377 if ($func_type == 'LIKE %...%') {
378 $func_type = 'LIKE';
379 $fields[$i] = '%' . $fields[$i] . '%';
381 if ($func_type == 'REGEXP ^...$') {
382 $func_type = 'REGEXP';
383 $fields[$i] = '^' . $fields[$i] . '$';
386 if ($func_type == 'IN (...)' || $func_type == 'NOT IN (...)' || $func_type == 'BETWEEN' || $func_type == 'NOT BETWEEN') {
387 $func_type = str_replace(' (...)', '', $func_type);
389 // quote values one by one
390 $values = explode(',', $fields[$i]);
391 foreach ($values as &$value)
392 $value = $quot . PMA_sqlAddSlashes(trim($value)) . $quot;
394 if ($func_type == 'BETWEEN' || $func_type == 'NOT BETWEEN')
395 $w[] = PMA_backquote($names[$i]) . ' ' . $func_type . ' ' . (isset($values[0]) ? $values[0] : '') . ' AND ' . (isset($values[1]) ? $values[1] : '');
396 else
397 $w[] = PMA_backquote($names[$i]) . ' ' . $func_type . ' (' . implode(',', $values) . ')';
399 else {
400 $w[] = PMA_backquote($names[$i]) . ' ' . $func_type . ' ' . $quot . PMA_sqlAddSlashes($fields[$i]) . $quot;;
403 } // end if
404 } // end for
406 if ($w) {
407 $sql_query .= ' WHERE ' . implode(' AND ', $w);
409 } // end if
411 if ($orderField != '--nil--') {
412 $sql_query .= ' ORDER BY ' . PMA_backquote($orderField) . ' ' . $order;
413 } // end if
415 require './sql.php';