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'][] = '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) {
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');
33 $titles['Browse'] = __('Browse foreign values');
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 .= '&goto=tbl_select.php&back=tbl_select.php';
46 * Gets tables informations
48 require_once './libraries/tbl_info.inc.php';
51 * Displays top menu links
53 require_once './libraries/tbl_links.inc.php';
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'];
68 // reformat mysql query output
69 if (strncasecmp($type, 'set', 3) == 0
70 ||
strncasecmp($type, 'enum', 4) == 0) {
71 $type = str_replace(',', ', ', $type);
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);
87 $fields_null[] = $row['Null'];
88 $fields_type[] = $type;
89 $fields_collation[] = !empty($row['Collation']) && $row['Collation'] != 'NULL'
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">
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
>
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
>
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[]">
131 if (strncasecmp($fields_type[$i], 'enum', 4) == 0) {
132 foreach ($GLOBALS['cfg']['EnumOperators'] as $fc) {
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) {
140 . '<option value="' . htmlspecialchars($fc) . '">'
141 . htmlspecialchars($fc) . '</option>';
144 foreach ($GLOBALS['cfg']['NumOperators'] as $fc) {
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) {
153 . '<option value="' . htmlspecialchars($fc) . '">'
154 . htmlspecialchars($fc) . '</option>';
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; ?>]"
185 <script type
="text/javascript">
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); ?>&field=<?php echo urlencode($field); ?>&fieldkey=<?php echo $i; ?>"><?php echo str_replace("'", "\'
", $titles['Browse']); ?></a>');
191 } elseif (strncasecmp($fields_type[$i], 'enum', 4) == 0) {
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
>';
201 echo ' </select
>' . "\n";
203 // o t h e r c a s e s
204 echo ' <input type
="text" name
="fields[' . $i . ']"'
205 .' size
="40" class="textfield" id
="field_' . $i . '" />' . "\n";
207 $type = $fields_type[$i];
208 if ($type == 'date
' || $type == 'datetime
' || substr($type, 0, 9) == 'timestamp
') {
210 <script type="text/javascript">
213 $('#field_<?php echo $i; ?>').datepicker({
218 <?php
echo ($type == 'date' ?
"showTime: false,":"showTime: true,"); ?
>
220 constrainInput
: false
228 <input type
="hidden" name
="names[<?php echo $i; ?>]"
229 value
="<?php echo htmlspecialchars($fields_list[$i]); ?>" />
230 <input type
="hidden" name
="types[<?php echo $i; ?>]"
231 value
="<?php echo $fields_type[$i]; ?>" />
232 <input type
="hidden" name
="collations[<?php echo $i; ?>]"
233 value
="<?php echo $fields_collation[$i]; ?>" />
243 PMA_generate_slider_effect('searchoptions', __('Options'));
245 <fieldset id
="fieldset_select_fields">
246 <legend
><?php
echo __('Select columns (at least one):'); ?
></legend
>
247 <select name
="param[]" size
="<?php echo min($fields_cnt, 10); ?>"
250 // Displays the list of the fields
251 foreach ($fields_list as $each_field) {
253 .'<option value="' . htmlspecialchars($each_field) . '"'
254 .' selected="selected">' . htmlspecialchars($each_field)
259 <input type
="checkbox" name
="distinct" value
="DISTINCT" id
="oDistinct" />
260 <label
for="oDistinct">DISTINCT
</label
>
263 <fieldset id
="fieldset_search_conditions">
264 <legend
><?php
echo '<em>' . __('Or') . '</em> ' . __('Add search conditions (body of the "where" clause):'); ?
></legend
>
265 <?php
echo PMA_showMySQLDocu('SQL-Syntax', 'Functions'); ?
>
267 <input type
="text" name
="where" class="textfield" size
="64" />
270 <fieldset id
="fieldset_limit_rows">
271 <legend
><?php
echo __('Number of rows per page'); ?
></legend
>
272 <input type
="text" size
="4" name
="session_max_rows"
273 value
="<?php echo $GLOBALS['cfg']['MaxRows']; ?>" class="textfield" />
276 <fieldset id
="fieldset_display_order">
277 <legend
><?php
echo __('Display order:'); ?
></legend
>
278 <select name
="orderField">
279 <option value
="--nil--"></option
>
281 foreach ($fields_list as $each_field) {
283 .'<option value="' . htmlspecialchars($each_field) . '">'
284 .htmlspecialchars($each_field) . '</option>' . "\n";
290 'ASC' => __('Ascending'),
291 'DESC' => __('Descending')
293 PMA_display_html_radio('order', $choices, 'ASC', false, true, "formelement");
297 <br style
="clear: both;"/>
300 <fieldset
class="tblFooters">
301 <input type
="hidden" name
="max_number_of_fields"
302 value
="<?php echo $fields_cnt; ?>" />
303 <input type
="submit" name
="submit" value
="<?php echo __('Go'); ?>" />
306 <div id
="sqlqueryresults"></div
>
308 require './libraries/footer.inc.php';
313 * Selection criteria have been submitted -> do the work
318 $sql_query = 'SELECT ' . (isset($distinct) ?
'DISTINCT ' : '');
320 // if all fields were selected to display, we do a SELECT *
321 // (more efficient and this helps prevent a problem in IE
322 // if one of the rows is edited and we come back to the Select results)
324 if (count($param) == $max_number_of_fields) {
327 $param = PMA_backquote($param);
328 $sql_query .= implode(', ', $param);
331 // avoid a loop, for example when $cfg['DefaultTabTable'] is set
332 // to 'tbl_select.php'
335 $sql_query .= ' FROM ' . PMA_backquote($table);
338 if (trim($where) != '') {
339 $sql_query .= ' WHERE ' . $where;
341 $w = $charsets = array();
342 $cnt_func = count($func);
344 while (list($i, $func_type) = each($func)) {
345 list($charsets[$i]) = explode('_', $collations[$i]);
346 if (isset($GLOBALS['cfg']['UnaryOperators'][$func_type]) && $GLOBALS['cfg']['UnaryOperators'][$func_type] == 1) {
348 $w[] = PMA_backquote($names[$i]) . ' ' . $func_type;
350 } elseif (strncasecmp($types[$i], 'enum', 4) == 0) {
351 if (!empty($fields[$i])) {
352 if (!is_array($fields[$i])) {
353 $fields[$i] = explode(',', $fields[$i]);
355 $enum_selected_count = count($fields[$i]);
356 if ($func_type == '=' && $enum_selected_count > 1) {
357 $func_type = $func[$i] = 'IN';
361 } elseif ($func_type == '!=' && $enum_selected_count > 1) {
362 $func_type = $func[$i] = 'NOT IN';
370 $enum_where = '\'' . PMA_sqlAddslashes($fields[$i][0]) . '\'';
371 for ($e = 1; $e < $enum_selected_count; $e++
) {
372 $enum_where .= ', \'' . PMA_sqlAddslashes($fields[$i][$e]) . '\'';
375 $w[] = PMA_backquote($names[$i]) . ' ' . $func_type . ' ' . $parens_open . $enum_where . $parens_close;
378 } elseif ($fields[$i] != '') {
379 // For these types we quote the value. Even if it's another type (like INT),
380 // for a LIKE we always quote the value. MySQL converts strings to numbers
381 // and numbers to strings as necessary during the comparison
382 if (preg_match('@char|binary|blob|text|set|date|time|year@i', $types[$i]) ||
strpos(' ' . $func_type, 'LIKE')) {
389 if ($func_type == 'LIKE %...%') {
391 $fields[$i] = '%' . $fields[$i] . '%';
393 if ($func_type == 'REGEXP ^...$') {
394 $func_type = 'REGEXP';
395 $fields[$i] = '^' . $fields[$i] . '$';
398 if ($func_type == 'IN (...)' ||
$func_type == 'NOT IN (...)' ||
$func_type == 'BETWEEN' ||
$func_type == 'NOT BETWEEN') {
399 $func_type = str_replace(' (...)', '', $func_type);
401 // quote values one by one
402 $values = explode(',', $fields[$i]);
403 foreach ($values as &$value)
404 $value = $quot . PMA_sqlAddslashes(trim($value)) . $quot;
406 if ($func_type == 'BETWEEN' ||
$func_type == 'NOT BETWEEN')
407 $w[] = PMA_backquote($names[$i]) . ' ' . $func_type . ' ' . (isset($values[0]) ?
$values[0] : '') . ' AND ' . (isset($values[1]) ?
$values[1] : '');
409 $w[] = PMA_backquote($names[$i]) . ' ' . $func_type . ' (' . implode(',', $values) . ')';
412 $w[] = PMA_backquote($names[$i]) . ' ' . $func_type . ' ' . $quot . PMA_sqlAddslashes($fields[$i]) . $quot;;
419 $sql_query .= ' WHERE ' . implode(' AND ', $w);
423 if ($orderField != '--nil--') {
424 $sql_query .= ' ORDER BY ' . PMA_backquote($orderField) . ' ' . $order;