2 /* vim: set expandtab sw=4 ts=4 sts=4: */
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
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) {
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');
34 $titles['Browse'] = __('Browse foreign values');
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 .= '&goto=tbl_select.php&back=tbl_select.php';
47 * Gets tables informations
49 require_once './libraries/tbl_info.inc.php';
52 * Displays top menu links
54 require_once './libraries/tbl_links.inc.php';
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'];
69 // reformat mysql query output
70 if (strncasecmp($type, 'set', 3) == 0
71 ||
strncasecmp($type, 'enum', 4) == 0) {
72 $type = str_replace(',', ', ', $type);
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);
88 $fields_null[] = $row['Null'];
89 $fields_type[] = $type;
90 $fields_collation[] = !empty($row['Collation']) && $row['Collation'] != 'NULL'
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
>
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
>
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[]">
132 // determine valid operators
133 if (strncasecmp($fields_type[$i], 'enum', 4) == 0) {
139 } elseif (preg_match('@char|blob|text|set@i', $fields_type[$i])) {
173 } // end if... else...
175 // if field can be NULL, add IS NULL and IS NOT NULL
176 if ($fields_null[$i]) {
177 $operators[] = 'IS NULL';
178 $operators[] = 'IS NOT NULL';
180 foreach ($operators as $op) {
182 . '<option value="' . htmlspecialchars($op) . '">' . htmlspecialchars($op) . '</option>';
190 $field = $fields_list[$i];
192 $foreignData = PMA_getForeignData($foreigners, $field, false, '', '');
194 if ($foreigners && isset($foreigners[$field]) && is_array($foreignData['disp_row'])) {
195 // f o r e i g n k e y s
196 echo ' <select name="fields[' . $i . ']">' . "\n";
197 // go back to first row
199 // here, the 4th parameter is empty because there is no current
200 // value of data for the dropdown (the search page initial values
201 // are displayed empty)
202 echo PMA_foreignDropdown($foreignData['disp_row'],
203 $foreignData['foreign_field'],
204 $foreignData['foreign_display'],
205 '', $GLOBALS['cfg']['ForeignKeyMaxLimit']);
206 echo ' </select>' . "\n";
207 } elseif ($foreignData['foreign_link'] == true) {
209 <input type
="text" name
="fields[<?php echo $i; ?>]"
210 id
="field_<?php echo md5($field); ?>[<?php echo $i; ?>]"
212 <script type
="text/javascript">
214 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>');
218 } elseif (strncasecmp($fields_type[$i], 'enum', 4) == 0) {
220 $enum_value=explode(', ', str_replace("'", '', substr($fields_type[$i], 5, -1)));
221 $cnt_enum_value = count($enum_value);
222 echo ' <select name
="fields[' . $i . '][]"'
223 .' multiple
="multiple" size
="' . min(3, $cnt_enum_value) . '">' . "\n";
224 for ($j = 0; $j < $cnt_enum_value; $j++) {
225 echo ' <option value
="' . $enum_value[$j] . '">'
226 . $enum_value[$j] . '</option
>';
228 echo ' </select
>' . "\n";
230 // o t h e r c a s e s
231 $the_class = 'textfield
';
232 $type = $fields_type[$i];
233 if ($type == 'date
') {
234 $the_class .= ' datefield
';
235 } elseif ($type == 'datetime
' || substr($type, 0, 9) == 'timestamp
') {
236 $the_class .= ' datetimefield
';
238 echo ' <input type
="text" name
="fields[' . $i . ']"'
239 .' size
="40" class="' . $the_class . '" id
="field_' . $i . '" />' . "\n";
242 <input type="hidden" name="names[<?php echo $i; ?>]"
243 value="<?php echo htmlspecialchars($fields_list[$i]); ?>" />
244 <input type="hidden" name="types[<?php echo $i; ?>]"
245 value="<?php echo $fields_type[$i]; ?>" />
246 <input type="hidden" name="collations[<?php echo $i; ?>]"
247 value="<?php echo $fields_collation[$i]; ?>" />
257 PMA_generate_slider_effect('searchoptions
', __('Options
'));
259 <fieldset id="fieldset_select_fields">
260 <legend><?php echo __('Select
columns (at least one
):'); ?></legend>
261 <select name="param[]" size="<?php echo min($fields_cnt, 10); ?>"
264 // Displays the list of the fields
265 foreach ($fields_list as $each_field) {
267 .'<option value
="' . htmlspecialchars($each_field) . '"'
268 .' selected
="selected">' . htmlspecialchars($each_field)
273 <input type="checkbox" name="distinct" value="DISTINCT" id="oDistinct" />
274 <label for="oDistinct">DISTINCT</label>
277 <fieldset id="fieldset_search_conditions">
278 <legend><?php echo '<em
>' . __('Or') . '</em
> ' . __('Add search
conditions (body of the
"where" clause
):'); ?></legend>
279 <?php echo PMA_showMySQLDocu('SQL
-Syntax
', 'Functions
'); ?>
281 <input type="text" name="where" class="textfield" size="64" />
284 <fieldset id="fieldset_limit_rows">
285 <legend><?php echo __('Number of rows per page
'); ?></legend>
286 <input type="text" size="4" name="session_max_rows"
287 value="<?php echo $GLOBALS['cfg
']['MaxRows
']; ?>" class="textfield" />
290 <fieldset id="fieldset_display_order">
291 <legend><?php echo __('Display order
:'); ?></legend>
292 <select name="orderField">
293 <option value="--nil--"></option>
295 foreach ($fields_list as $each_field) {
297 .'<option value
="' . htmlspecialchars($each_field) . '">'
298 .htmlspecialchars($each_field) . '</option
>' . "\n";
304 'ASC
' => __('Ascending
'),
305 'DESC
' => __('Descending
')
307 PMA_display_html_radio('order
', $choices, 'ASC
', false, true, "formelement");
311 <br style="clear: both;"/>
314 <fieldset class="tblFooters">
315 <input type="hidden" name="max_number_of_fields"
316 value="<?php echo $fields_cnt; ?>" />
317 <input type="submit" name="submit" value="<?php echo __('Go
'); ?>" />
320 <div id="sqlqueryresults"></div>
322 require './libraries
/footer
.inc
.php
';
327 * Selection criteria have been submitted -> do the work
332 $sql_query = 'SELECT
' . (isset($distinct) ? 'DISTINCT
' : '');
334 // if all fields were selected to display, we do a SELECT *
335 // (more efficient and this helps prevent a problem in IE
336 // if one of the rows is edited and we come back to the Select results)
338 if (count($param) == $max_number_of_fields) {
341 $param = PMA_backquote($param);
342 $sql_query .= implode(', ', $param);
345 // avoid a loop, for example when $cfg['DefaultTabTable
'] is set
346 // to 'tbl_select
.php
'
349 $sql_query .= ' FROM
' . PMA_backquote($table);
352 if (trim($where) != '') {
353 $sql_query .= ' WHERE
' . $where;
355 $w = $charsets = array();
356 $unary_operators = array(
362 $cnt_func = count($func);
364 while (list($i, $func_type) = each($func)) {
365 list($charsets[$i]) = explode('_
', $collations[$i]);
366 if (isset($unary_operators[$func_type])) {
368 $w[] = PMA_backquote($names[$i]) . ' ' . $func_type;
370 } elseif (strncasecmp($types[$i], 'enum
', 4) == 0) {
371 if (!empty($fields[$i])) {
372 if (! is_array($fields[$i])) {
373 $fields[$i] = explode(',', $fields[$i]);
375 $enum_selected_count = count($fields[$i]);
376 if ($func_type == '=' && $enum_selected_count > 1) {
377 $func_type = $func[$i] = 'IN
';
381 } elseif ($func_type == '!=' && $enum_selected_count > 1) {
382 $func_type = $func[$i] = 'NOT IN
';
390 $enum_where = '\'
' . PMA_sqlAddSlashes($fields[$i][0]) . '\'
';
391 for ($e = 1; $e < $enum_selected_count; $e++) {
392 $enum_where .= ', \'' . PMA_sqlAddSlashes($fields[$i][$e]) . '\'
';
395 $w[] = PMA_backquote($names[$i]) . ' ' . $func_type . ' ' . $parens_open . $enum_where . $parens_close;
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')) {
409 if ($func_type == 'LIKE %...%') {
411 $fields[$i] = '%' . $fields[$i] . '%';
413 if ($func_type == 'REGEXP ^...$') {
414 $func_type = 'REGEXP';
415 $fields[$i] = '^' . $fields[$i] . '$';
418 if ($func_type == 'IN (...)' ||
$func_type == 'NOT IN (...)' ||
$func_type == 'BETWEEN' ||
$func_type == 'NOT BETWEEN') {
419 $func_type = str_replace(' (...)', '', $func_type);
421 // quote values one by one
422 $values = explode(',', $fields[$i]);
423 foreach ($values as &$value)
424 $value = $quot . PMA_sqlAddSlashes(trim($value)) . $quot;
426 if ($func_type == 'BETWEEN' ||
$func_type == 'NOT BETWEEN')
427 $w[] = PMA_backquote($names[$i]) . ' ' . $func_type . ' ' . (isset($values[0]) ?
$values[0] : '') . ' AND ' . (isset($values[1]) ?
$values[1] : '');
429 $w[] = PMA_backquote($names[$i]) . ' ' . $func_type . ' (' . implode(',', $values) . ')';
432 $w[] = PMA_backquote($names[$i]) . ' ' . $func_type . ' ' . $quot . PMA_sqlAddSlashes($fields[$i]) . $quot;;
439 $sql_query .= ' WHERE ' . implode(' AND ', $w);
443 if ($orderField != '--nil--') {
444 $sql_query .= ' ORDER BY ' . PMA_backquote($orderField) . ' ' . $order;