2 /* vim: set expandtab sw=4 ts=4 sts=4: */
9 * Gets some core libraries
11 require_once './libraries/common.inc.php';
12 require_once './libraries/relation.lib.php'; // foreign keys
13 require_once './libraries/mysql_charsets.lib.php';
15 if ($GLOBALS['cfg']['PropertiesIconic'] == true) {
17 '<img class="icon" width="16" height="16" src="' . $pmaThemeImage
18 .'b_browse.png" alt="' . $strBrowseForeignValues . '" title="'
19 .$strBrowseForeignValues . '" />';
21 if ($GLOBALS['cfg']['PropertiesIconic'] === 'both') {
22 $titles['Browse'] .= $strBrowseForeignValues;
25 $titles['Browse'] = $strBrowseForeignValues;
29 * Not selection yet required -> displays the selection form
31 if (!isset($param) ||
$param[0] == '') {
32 // Gets some core libraries
33 require_once './libraries/tbl_common.php';
34 //$err_url = 'tbl_select.php' . $err_url;
35 $url_query .= '&goto=tbl_select.php&back=tbl_select.php';
38 * Gets tables informations
40 require_once './libraries/tbl_info.inc.php';
43 * Displays top menu links
45 require_once './libraries/tbl_links.inc.php';
48 $goto = $GLOBALS['cfg']['DefaultTabTable'];
50 // Defines the url to return to in case of error in the next sql statement
51 $err_url = $goto . '?' . PMA_generate_common_url($db, $table);
53 // Gets the list and number of fields
54 $result = PMA_DBI_query('SHOW FULL FIELDS FROM ' . PMA_backquote($table) . ' FROM ' . PMA_backquote($db) . ';', null, PMA_DBI_QUERY_STORE
);
55 $fields_cnt = PMA_DBI_num_rows($result);
56 // rabue: we'd better ensure, that all arrays are empty.
57 $fields_list = $fields_null = $fields_type = $fields_collation = array();
58 while ($row = PMA_DBI_fetch_assoc($result)) {
59 $fields_list[] = $row['Field'];
61 // reformat mysql query output - staybyte - 9. June 2001
62 if (strncasecmp($type, 'set', 3) == 0
63 ||
strncasecmp($type, 'enum', 4) == 0) {
64 $type = str_replace(',', ', ', $type);
67 // strip the "BINARY" attribute, except if we find "BINARY(" because
68 // this would be a BINARY or VARBINARY field type
69 if (!preg_match('@BINARY[\(]@i', $type)) {
70 $type = preg_replace('@BINARY@i', '', $type);
72 $type = preg_replace('@ZEROFILL@i', '', $type);
73 $type = preg_replace('@UNSIGNED@i', '', $type);
75 $type = strtolower($type);
80 $fields_null[] = $row['Null'];
81 $fields_type[] = $type;
82 $fields_collation[] = !empty($row['Collation']) && $row['Collation'] != 'NULL'
86 PMA_DBI_free_result($result);
87 unset($result, $type);
90 // retrieve keys into foreign fields, if any
91 $cfgRelation = PMA_getRelationsParam();
92 // check also foreigners even if relwork is FALSE (to get
93 // foreign keys from innodb)
94 //$foreigners = ($cfgRelation['relwork'] ? PMA_getForeigners($db, $table) : FALSE);
95 $foreigners = PMA_getForeigners($db, $table);
97 <script type
="text/javascript">
99 function PMA_tbl_select_operator(f
, index
, multiple
) {
100 switch (f
.elements
["func[" + index +
"]"].options
[f
.elements
["func[" + index +
"]"].selectedIndex
].value
) {
102 reset($GLOBALS['cfg']['UnaryOperators']);
103 while (list($operator) = each($GLOBALS['cfg']['UnaryOperators'])) {
104 echo ' case "' . $operator . "\":\r\n";
113 f
.elements
["fields[" + index +
"]" +
((multiple
) ?
"[]": "")].disabled
= bDisabled
;
117 <form method
="post" action
="tbl_select.php" name
="insertForm">
118 <?php
echo PMA_generate_common_hidden_inputs($db, $table); ?
>
119 <input type
="hidden" name
="goto" value
="<?php echo $goto; ?>" />
120 <input type
="hidden" name
="back" value
="tbl_select.php" />
122 <fieldset id
="fieldset_table_search">
124 <fieldset id
="fieldset_select_fields">
125 <legend
><?php
echo $strSelectFields; ?
></legend
>
126 <select name
="param[]" size
="<?php echo min($fields_cnt, 10); ?>"
129 // Displays the list of the fields
130 foreach ($fields_list as $each_field) {
132 .'<option value="' . htmlspecialchars($each_field) . '"'
133 .' selected="selected">' . htmlspecialchars($each_field)
138 <input type
="checkbox" name
="distinct" value
="DISTINCT" id
="oDistinct" />
139 <label
for="oDistinct">DISTINCT
</label
>
142 <fieldset id
="fieldset_limit_rows">
143 <legend
><?php
echo $strLimitNumRows; ?
></legend
>
144 <input type
="text" size
="4" name
="session_max_rows"
145 value
="<?php echo $GLOBALS['cfg']['MaxRows']; ?>" class="textfield" />
148 <fieldset id
="fieldset_display_order">
149 <legend
><?php
echo $strDisplayOrder; ?
></legend
>
150 <select name
="orderField" style
="vertical-align: middle">
151 <option value
="--nil--"></option
>
153 foreach ($fields_list as $each_field) {
155 .'<option value="' . htmlspecialchars($each_field) . '">'
156 .htmlspecialchars($each_field) . '</option>' . "\n";
161 <div
class="formelement">
162 <input type
="radio" name
="order" value
="ASC" checked
="checked" id
="sortASC" />
163 <label
for="sortASC"><?php
echo $strAscending; ?
></label
>
166 <div
class="formelement">
167 <input type
="radio" name
="order" value
="DESC" id
="sortDESC" />
168 <label
for="sortDESC"><?php
echo $strDescending; ?
></label
>
172 <br
class="clearfloat" />
173 <?php
echo $strAddSearchConditions; ?
>
174 <?php
echo PMA_showMySQLDocu('SQL-Syntax', 'Functions'); ?
>
176 <input type
="text" name
="where" class="textfield" size
="64" />
179 <fieldset
class="tblFooters">
180 <input type
="submit" name
="submit" value
="<?php echo $strGo; ?>" />
183 <fieldset id
="fieldset_table_qbe">
184 <legend
><?php
echo '<em>' . $strOr . '</em> ' . $strDoAQuery; ?
></legend
>
187 <tr
><th
><?php
echo $strField; ?
></th
>
188 <th
><?php
echo $strType; ?
></th
>
189 <th
><?php
echo $strCollation; ?
></th
>
190 <th
><?php
echo $strOperator; ?
></th
>
191 <th
><?php
echo $strValue; ?
></th
>
198 <script type
="text/javascript" src
="./js/tbl_change.js"></script
>
200 for ($i = 0; $i < $fields_cnt; $i++
) {
202 <tr
class="<?php echo $odd_row ? 'odd' : 'even'; $odd_row = ! $odd_row; ?>">
203 <th
><?php
echo htmlspecialchars($fields_list[$i]); ?
></th
>
204 <td
><?php
echo $fields_type[$i]; ?
></td
>
205 <td
><?php
echo $fields_collation[$i]; ?
></td
>
206 <td
><select name
="func[]">
208 if (strncasecmp($fields_type[$i], 'enum', 4) == 0) {
209 foreach ($GLOBALS['cfg']['EnumOperators'] as $fc) {
211 . '<option value="' . htmlspecialchars($fc) . '">'
212 . htmlspecialchars($fc) . '</option>';
214 } elseif (preg_match('@char|blob|text|set@i', $fields_type[$i])) {
215 foreach ($GLOBALS['cfg']['TextOperators'] as $fc) {
217 . '<option value="' . htmlspecialchars($fc) . '">'
218 . htmlspecialchars($fc) . '</option>';
221 foreach ($GLOBALS['cfg']['NumOperators'] as $fc) {
223 . '<option value="' . htmlspecialchars($fc) . '">'
224 . htmlspecialchars($fc) . '</option>';
226 } // end if... else...
227 if ($fields_null[$i]) {
228 foreach ($GLOBALS['cfg']['NullOperators'] as $fc) {
230 . '<option value="' . htmlspecialchars($fc) . '">'
231 . htmlspecialchars($fc) . '</option>';
241 $field = $fields_list[$i];
243 $foreignData = PMA_getForeignData($foreigners, $field, false, '', '');
245 if ($foreigners && isset($foreigners[$field]) && is_array($foreignData['disp_row'])) {
246 // f o r e i g n k e y s
247 echo ' <select name="fields[' . $i . ']">' . "\n";
248 // go back to first row
250 // here, the 4th parameter is empty because there is no current
251 // value of data for the dropdown (the search page initial values
252 // are displayed empty)
253 echo PMA_foreignDropdown($foreignData['disp_row'],
254 $foreignData['foreign_field'],
255 $foreignData['foreign_display'],
256 '', $GLOBALS['cfg']['ForeignKeyMaxLimit']);
257 echo ' </select>' . "\n";
258 } elseif ($foreignData['foreign_link'] == true) {
260 <input type
="text" name
="fields[<?php echo $i; ?>]"
261 id
="field_<?php echo md5($field); ?>[<?php echo $i; ?>]"
263 <script type
="text/javascript">
265 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>');
269 } elseif (strncasecmp($fields_type[$i], 'enum', 4) == 0) {
271 $enum_value=explode(', ', str_replace("'", '', substr($fields_type[$i], 5, -1)));
272 $cnt_enum_value = count($enum_value);
273 echo ' <select name
="fields[' . $i . '][]"'
274 .' multiple
="multiple" size
="' . min(3, $cnt_enum_value) . '">' . "\n";
275 for ($j = 0; $j < $cnt_enum_value; $j++) {
276 echo ' <option value
="' . $enum_value[$j] . '">'
277 . $enum_value[$j] . '</option
>';
279 echo ' </select
>' . "\n";
281 // o t h e r c a s e s
282 echo ' <input type
="text" name
="fields[' . $i . ']"'
283 .' size
="40" class="textfield" id
="field_' . $i . '" />' . "\n";
285 $type = $fields_type[$i];
286 if ($type == 'date
' || $type == 'datetime
' || substr($type, 0, 9) == 'timestamp
') {
288 <script type="text/javascript">
290 document.write('<a title
="<?php echo $strCalendar;?>" href
="javascript:openCalendar(\'<?php echo PMA_generate_common_url();?>\', \'insertForm\', \'field_<?php echo ($i); ?>\', \'<?php echo (substr($type, 0, 9) == 'timestamp') ? 'datetime' : substr($type, 0, 9); ?>\')"><img
class="calendar" src
="<?php echo $pmaThemeImage; ?>b_calendar.png" alt
="<?php echo $strCalendar; ?>"/></a
>');
296 <input type="hidden" name="names[<?php echo $i; ?>]"
297 value="<?php echo htmlspecialchars($fields_list[$i]); ?>" />
298 <input type="hidden" name="types[<?php echo $i; ?>]"
299 value="<?php echo $fields_type[$i]; ?>" />
300 <input type="hidden" name="collations[<?php echo $i; ?>]"
301 value="<?php echo $fields_collation[$i]; ?>" />
310 <fieldset class="tblFooters">
311 <input type="hidden" name="max_number_of_fields"
312 value="<?php echo $fields_cnt; ?>" />
313 <input type="submit" name="submit" value="<?php echo $strGo; ?>" />
317 require_once './libraries
/footer
.inc
.php
';
322 * Selection criteria have been submitted -> do the work
327 $sql_query = 'SELECT
' . (isset($distinct) ? 'DISTINCT
' : '');
329 // if all fields were selected to display, we do a SELECT *
330 // (more efficient and this helps prevent a problem in IE
331 // if one of the rows is edited and we come back to the Select results)
333 if (count($param) == $max_number_of_fields) {
336 $param = PMA_backquote($param);
337 $sql_query .= implode(', ', $param);
341 $sql_query .= ' FROM
' . PMA_backquote($table);
344 if (trim($where) != '') {
345 $sql_query .= ' WHERE
' . $where;
347 $w = $charsets = array();
348 $cnt_func = count($func);
350 while (list($i, $func_type) = each($func)) {
351 list($charsets[$i]) = explode('_
', $collations[$i]);
352 if (isset($GLOBALS['cfg
']['UnaryOperators
'][$func_type]) && $GLOBALS['cfg
']['UnaryOperators
'][$func_type] == 1) {
354 $w[] = PMA_backquote(urldecode($names[$i])) . ' ' . $func_type;
356 } elseif (strncasecmp($types[$i], 'enum
', 4) == 0) {
357 if (!empty($fields[$i])) {
358 if (!is_array($fields[$i])) {
359 $fields[$i] = explode(',', $fields[$i]);
361 $enum_selected_count = count($fields[$i]);
362 if ($func_type == '=' && $enum_selected_count > 1) {
363 $func_type = $func[$i] = 'IN
';
367 } elseif ($func_type == '!=' && $enum_selected_count > 1) {
368 $func_type = $func[$i] = 'NOT IN
';
376 $enum_where = '\'
' . PMA_sqlAddslashes($fields[$i][0]) . '\'
';
377 if ($charsets[$i] != $charset_connection) {
378 $enum_where = 'CONVERT(_utf8
' . $enum_where . ' USING
' . $charsets[$i] . ') COLLATE
' . $collations[$i];
380 for ($e = 1; $e < $enum_selected_count; $e++) {
382 $tmp_literal = '\'
' . PMA_sqlAddslashes($fields[$i][$e]) . '\'
';
383 if ($charsets[$i] != $charset_connection) {
384 $tmp_literal = 'CONVERT(_utf8
' . $tmp_literal . ' USING
' . $charsets[$i] . ') COLLATE
' . $collations[$i];
386 $enum_where .= $tmp_literal;
390 $w[] = PMA_backquote(urldecode($names[$i])) . ' ' . $func_type . ' ' . $parens_open . $enum_where . $parens_close;
393 } elseif ($fields[$i] != '') {
394 // For these types we quote the value. Even if it's another
type (like
INT),
395 // for a LIKE we always quote the value. MySQL converts strings to numbers
396 // and numbers to strings as necessary during the comparison
397 if (preg_match('@char|binary|blob|text|set|date|time|year@i', $types[$i]) ||
strpos(' ' . $func_type, 'LIKE')) {
403 // Make query independant from the selected connection charset.
404 // But if the field's type is VARBINARY, it has no charset
405 // and $charsets[$i] is empty, so we cannot generate a CONVERT
407 if (!empty($charsets[$i]) && $charsets[$i] != $charset_connection && preg_match('@char|binary|blob|text|set@i', $types[$i])) {
408 $prefix = 'CONVERT(_utf8 ';
409 $suffix = ' USING ' . $charsets[$i] . ') COLLATE ' . $collations[$i];
411 $prefix = $suffix = '';
415 if ($func_type == 'LIKE %...%') {
417 $fields[$i] = '%' . $fields[$i] . '%';
419 $w[] = PMA_backquote(urldecode($names[$i])) . ' ' . $func_type . ' ' . $prefix . $quot . PMA_sqlAddslashes($fields[$i]) . $quot . $suffix;
425 $sql_query .= ' WHERE ' . implode(' AND ', $w);
429 if ($orderField != '--nil--') {
430 $sql_query .= ' ORDER BY ' . PMA_backquote(urldecode($orderField)) . ' ' . $order;