sorry, wrong version checked in
[phpmyadmin/arisferyanto.git] / tbl_select.php
blob9a7b944d0e7ca58de8db59e700acb8e500b7ee9a
1 <?php
2 /* $Id$ */
3 // vim: expandtab sw=4 ts=4 sts=4:
6 /**
7 * Gets some core libraries
8 */
9 require_once('./libraries/common.lib.php');
10 require_once('./libraries/relation.lib.php'); // foreign keys
11 require_once('./libraries/mysql_charsets.lib.php');
13 if ($cfg['PropertiesIconic'] == true) {
14 // We need to copy the value or else the == 'both' check will always return true
15 $propicon = (string)$cfg['PropertiesIconic'];
17 if ($propicon == 'both') {
18 $iconic_spacer = '<div class="nowrap">';
19 } else {
20 $iconic_spacer = '';
23 $titles['Browse'] = $iconic_spacer . '<img width="16" height="16" src="' . $pmaThemeImage . 'b_browse.png" alt="' . $strBrowseForeignValues . '" title="' . $strBrowseForeignValues . '" border="0" />';
25 if ($propicon == 'both') {
26 $titles['Browse'] .= '&nbsp;' . $strBrowseForeignValues . '</div>';
28 } else {
29 $titles['Browse'] = $strBrowseForeignValues;
32 /**
33 * Not selection yet required -> displays the selection form
35 if (!isset($param) || $param[0] == '') {
36 // Gets some core libraries
37 require_once('./tbl_properties_common.php');
38 //$err_url = 'tbl_select.php' . $err_url;
39 $url_query .= '&amp;goto=tbl_select.php&amp;back=tbl_select.php';
41 /**
42 * Gets tables informations
44 require_once('./tbl_properties_table_info.php');
46 /**
47 * Displays top menu links
49 require_once('./tbl_properties_links.php');
51 if (!isset($goto)) {
52 $goto = $cfg['DefaultTabTable'];
54 // Defines the url to return to in case of error in the next sql statement
55 $err_url = $goto . '?' . PMA_generate_common_url($db, $table);
57 // Gets the list and number of fields
58 $result = PMA_DBI_query('SHOW' . (PMA_MYSQL_INT_VERSION >= 40100 ? ' FULL' : '') . ' FIELDS FROM ' . PMA_backquote($table) . ' FROM ' . PMA_backquote($db) . ';', NULL, PMA_DBI_QUERY_STORE);
59 $fields_cnt = PMA_DBI_num_rows($result);
60 // rabue: we'd better ensure, that all arrays are empty.
61 $fields_list = $fields_null = $fields_type = $fields_collation = array();
62 while ($row = PMA_DBI_fetch_assoc($result)) {
63 $fields_list[] = $row['Field'];
64 $type = $row['Type'];
65 // reformat mysql query output - staybyte - 9. June 2001
66 if (strncasecmp($type, 'set', 3) == 0
67 || strncasecmp($type, 'enum', 4) == 0) {
68 $type = str_replace(',', ', ', $type);
69 } else {
71 // strip the "BINARY" attribute, except if we find "BINARY(" because
72 // this would be a BINARY or VARBINARY field type
73 if (!preg_match('@BINARY[\(]@i', $type)) {
74 $type = preg_replace('@BINARY@i', '', $type);
76 $type = preg_replace('@ZEROFILL@i', '', $type);
77 $type = preg_replace('@UNSIGNED@i', '', $type);
79 $type = strtolower($type);
81 if (empty($type)) {
82 $type = '&nbsp;';
84 $fields_null[] = $row['Null'];
85 $fields_type[] = $type;
86 $fields_collation[] = PMA_MYSQL_INT_VERSION >= 40100 && !empty($row['Collation']) && $row['Collation'] != 'NULL'
87 ? $row['Collation']
88 : '';
89 } // end while
90 PMA_DBI_free_result($result);
91 unset($result, $type);
93 // <markus@noga.de>
94 // retrieve keys into foreign fields, if any
95 $cfgRelation = PMA_getRelationsParam();
96 // check also foreigners even if relwork is FALSE (to get
97 // foreign keys from innodb)
98 //$foreigners = ($cfgRelation['relwork'] ? PMA_getForeigners($db, $table) : FALSE);
99 $foreigners = PMA_getForeigners($db, $table);
101 <script language="JavaScript" type="text/javascript">
102 <!--
103 function PMA_tbl_select_operator(f, index, multiple) {
104 switch (f.elements["func[" + index + "]"].options[f.elements["func[" + index + "]"].selectedIndex].value) {
105 <?php
106 reset($GLOBALS['cfg']['UnaryOperators']);
107 while (list($operator) = each($GLOBALS['cfg']['UnaryOperators'])) {
108 echo ' case "' . $operator . "\":\r\n";
111 bDisabled = true;
112 break;
114 default:
115 bDisabled = false;
117 f.elements["fields[" + index + "]" + ((multiple) ? "[]": "")].disabled = bDisabled;
119 // -->
120 </script>
121 <form method="post" action="tbl_select.php" name="insertForm">
122 <?php echo PMA_generate_common_hidden_inputs($db, $table); ?>
123 <input type="hidden" name="goto" value="<?php echo $goto; ?>" />
124 <input type="hidden" name="back" value="tbl_select.php" />
125 <table border="0" cellpadding="0" cellspacing="0">
126 <tr>
127 <td valign="top">
128 <table border="0" cellpadding="3" cellspacing="0">
129 <tr>
130 <th align="left" colspan="2">
131 <?php echo $strSelectFields; ?>
132 </th>
133 </tr>
134 <tr>
135 <td bgcolor="<?php echo $cfg['BgcolorOne']; ?>">
136 <select name="param[]" size="<?php echo min($fields_cnt, 10); ?>" multiple="multiple" align="left">
137 <?php
138 echo "\n";
139 // Displays the list of the fields
140 for ($i = 0 ; $i < $fields_cnt; $i++) {
141 echo ' '
142 . '<option value="' . htmlspecialchars($fields_list[$i]) . '" selected="selected">' . htmlspecialchars($fields_list[$i]) . '</option>' . "\n";
145 </select>
146 </td>
147 <td valign="bottom" bgcolor="<?php echo $cfg['BgcolorOne']; ?>">
148 <input type="checkbox" name="distinct" value="DISTINCT" id="oDistinct" /><label for="oDistinct">DISTINCT</label>
149 </td>
150 </tr>
151 </table>
152 </td>
153 <td nowrap="nowrap" width="50">&nbsp;</td>
154 <td valign="top">
155 <table border="0" cellpadding="3" cellspacing="0">
156 <tr>
157 <th align="left">
158 <?php echo $strLimitNumRows . "\n"; ?>:
159 </th>
160 </tr>
161 <tr>
162 <td bgcolor="<?php echo $cfg['BgcolorOne']; ?>">
163 <input type="text" size="4" name="session_max_rows" value="<?php echo $cfg['MaxRows']; ?>" class="textfield" />
164 </td>
165 </tr>
166 <tr>
167 <td>&nbsp;</td>
168 </tr>
169 <tr>
170 <th align="left">
171 <?php echo $strDisplayOrder; ?>
172 </th>
173 </tr>
174 <tr>
175 <td bgcolor="<?php echo $cfg['BgcolorOne']; ?>">
176 <select name="orderField" style="vertical-align: middle">
177 <option value="--nil--"></option>
178 <?php
179 echo "\n";
180 for ($i = 0; $i < $fields_cnt; $i++) {
181 echo ' ';
182 echo '<option value="' . htmlspecialchars($fields_list[$i]) . '">' . htmlspecialchars($fields_list[$i]) . '</option>' . "\n";
183 } // end for
185 </select><br />
186 <input type="radio" name="order" value="ASC" checked="checked" id="sortASC" /><label for="sortASC"><?php echo $strAscending; ?></label><br />
187 <input type="radio" name="order" value="DESC" id="sortDESC" /><label for="sortDESC"><?php echo $strDescending; ?></label>
188 </td>
189 </tr>
190 </table>
191 </td>
192 </tr>
193 </table>
194 <table border="0" cellpadding="3" cellspacing="0">
195 <tr>
196 <td colspan="2">&nbsp;</td>
197 </tr>
198 <tr>
199 <th align="left" class="tblHeaders" colspan="2">
200 <?php echo $strAddSearchConditions; ?>
201 <?php echo PMA_showMySQLDocu('SQL-Syntax', 'Functions') . "\n"; ?>
202 </th>
203 </tr>
204 <tr>
205 <td>
206 <input type="text" name="where" class="textfield" size="64" />
207 </td>
208 <td align="right">
209 <input type="submit" name="submit" value="<?php echo $strGo; ?>" />
210 </td>
211 </tr>
212 <tr>
213 <td colspan="2">&nbsp;</td>
214 </tr>
215 <tr>
216 <th align="left" class="tblHeaders" colspan="2">
217 <?php echo '<i>' . $strOr . '</i> ' . $strDoAQuery; ?>
218 </th>
219 </tr>
220 <tr>
221 <td colspan="2">
222 <table border="<?php echo $cfg['Border']; ?>" cellpadding="2" cellspacing="1">
223 <tr>
224 <th><?php echo $strField; ?></th>
225 <th><?php echo $strType; ?></th>
226 <?php echo PMA_MYSQL_INT_VERSION >= 40100 ? '<th>' . $strCollation . '</th>' . "\n" : ''; ?>
227 <th><?php echo $strOperator; ?></th>
228 <th><?php echo $strValue; ?></th>
229 </tr>
230 <?php
231 for ($i = 0; $i < $fields_cnt; $i++) {
232 echo "\n";
233 $bgcolor = ($i % 2) ? $cfg['BgcolorOne'] : $cfg['BgcolorTwo'];
235 <tr>
236 <td bgcolor="<?php echo $bgcolor; ?>"><b><?php echo htmlspecialchars($fields_list[$i]); ?></b></td>
237 <td bgcolor="<?php echo $bgcolor; ?>"><?php echo $fields_type[$i]; ?></td>
238 <?php echo PMA_MYSQL_INT_VERSION >= 40100 ? '<td bgcolor="' . $bgcolor . '">' . $fields_collation[$i] . '</td>' . "\n" : ''; ?>
239 <td bgcolor="<?php echo $bgcolor; ?>">
240 <select name="func[]">
241 <?php
242 if (strncasecmp($fields_type[$i], 'enum', 4) == 0) {
243 foreach ($GLOBALS['cfg']['EnumOperators'] as $k => $fc) {
244 echo "\n" . ' '
245 . '<option value="' . htmlspecialchars($fc) . '">' . htmlspecialchars($fc) . '</option>';
247 } elseif (preg_match('@char|blob|text|set@i', $fields_type[$i])) {
248 foreach ($GLOBALS['cfg']['TextOperators'] as $k => $fc) {
249 echo "\n" . ' '
250 . '<option value="' . htmlspecialchars($fc) . '">' . htmlspecialchars($fc) . '</option>';
252 } else {
253 foreach ($GLOBALS['cfg']['NumOperators'] as $k => $fc) {
254 echo "\n" . ' '
255 . '<option value="' . htmlspecialchars($fc) . '">' . htmlspecialchars($fc) . '</option>';
257 } // end if... else...
258 if ($fields_null[$i]) {
259 foreach ($GLOBALS['cfg']['NullOperators'] as $k => $fc) {
260 echo "\n" . ' '
261 . '<option value="' . htmlspecialchars($fc) . '">' . htmlspecialchars($fc) . '</option>';
264 echo "\n";
266 </select>
267 </td>
268 <td bgcolor="<?php echo $bgcolor; ?>">
269 <?php
270 // <markus@noga.de>
271 $field = $fields_list[$i];
273 // do not use require_once here
274 require('./libraries/get_foreign.lib.php');
276 echo "\n";
277 // we got a bug report: in some cases, even if $disp is true,
278 // there are no rows, so we add a fetch_array
280 if ($foreigners && isset($foreigners[$field]) && isset($disp_row) && is_array($disp_row)) {
281 // f o r e i g n k e y s
282 echo ' <select name="fields[' . $i . ']">' . "\n";
283 // go back to first row
285 // here, the 4th parameter is empty because there is no current
286 // value of data for the dropdown (the search page initial values
287 // are displayed empty)
288 echo PMA_foreignDropdown($disp_row, $foreign_field, $foreign_display, '', $cfg['ForeignKeyMaxLimit']);
289 echo ' </select>' . "\n";
290 } else if (isset($foreign_link) && $foreign_link == true) {
292 <input type="text" name="fields[<?php echo $i; ?>]" id="field_<?php echo md5($field); ?>[<?php echo $i; ?>]" class="textfield" />
293 <script type="text/javascript" language="javascript">
294 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>');
295 </script>
296 <?php
297 } else if (strncasecmp($fields_type[$i], 'enum', 4) == 0) {
298 // e n u m s
299 $enum_value=explode(', ', str_replace("'", '', substr($fields_type[$i], 5, -1)));
300 $cnt_enum_value = count($enum_value);
301 echo ' <select name="fields[' . $i . '][]" multiple="multiple" size="' . min(3, $cnt_enum_value) . '">' . "\n";
302 for ($j = 0; $j < $cnt_enum_value; $j++) {
303 echo ' <option value="' . $enum_value[$j] . '">' . $enum_value[$j] . '</option>';
304 } // end for
305 echo ' </select>' . "\n";
306 } else {
307 // o t h e r c a s e s
308 echo ' <input type="text" name="fields[' . $i . ']" size="40" class="textfield" />' . "\n";
312 <input type="hidden" name="names[<?php echo $i; ?>]" value="<?php echo htmlspecialchars($fields_list[$i]); ?>" />
313 <input type="hidden" name="types[<?php echo $i; ?>]" value="<?php echo $fields_type[$i]; ?>" />
314 <input type="hidden" name="collations[<?php echo $i; ?>]" value="<?php echo $fields_collation[$i]; ?>" />
315 </td>
316 </tr>
317 <?php
318 } // end for
319 echo "\n";
321 </table>
322 </td>
323 </tr>
324 <tr>
325 <td nowrap="nowrap" colspan="2" align="right">
326 <input type="hidden" name="max_number_of_fields" value="<?php echo $fields_cnt; ?>" />
327 <input type="submit" name="submit" value="<?php echo $strGo; ?>" />
328 </td>
329 </tr>
330 </table>
331 </form>
332 <?php
333 require_once('./footer.inc.php');
338 * Selection criteria have been submitted -> do the work
340 else {
341 // Builds the query
343 $sql_query = 'SELECT ' . (isset($distinct) ? 'DISTINCT ' : '');
345 // if all fields were selected to display, we do a SELECT *
346 // (more efficient and this helps prevent a problem in IE
347 // if one of the rows is edited and we come back to the Select results)
349 if (count($param) == $max_number_of_fields) {
350 $sql_query .= '* ';
351 } else {
353 $sql_query .= PMA_backquote(urldecode($param[0]));
354 $c = count($param);
355 for ($i = 1; $i < $c; $i++) {
356 $sql_query .= ',' . PMA_backquote(urldecode($param[$i]));
358 } // end if
360 $sql_query .= ' FROM ' . PMA_backquote($table);
362 // The where clause
363 if (trim($where) != '') {
364 $sql_query .= ' WHERE ' . $where;
365 } else {
366 $w = $charsets = array();
367 $cnt_func = count($func);
368 reset($func);
369 while (list($i, $func_type) = each($func)) {
370 if (PMA_MYSQL_INT_VERSION >= 40100) {
371 list($charsets[$i]) = explode('_', $collations[$i]);
373 if (@$GLOBALS['cfg']['UnaryOperators'][$func_type] == 1) {
374 $fields[$i] = '';
375 $w[] = PMA_backquote(urldecode($names[$i])) . ' ' . $func_type;
377 } elseif (strncasecmp($types[$i], 'enum', 4) == 0) {
378 if (!empty($fields[$i])) {
379 if (!is_array($fields[$i])) {
380 $fields[$i] = explode(',', $fields[$i]);
382 $enum_selected_count = count($fields[$i]);
383 if ($func_type == '=' && $enum_selected_count > 1) {
384 $func_type = $func[$i] = 'IN';
385 $parens_open = '(';
386 $parens_close = ')';
388 } elseif ($func_type == '!=' && $enum_selected_count > 1) {
389 $func_type = $func[$i] = 'NOT IN';
390 $parens_open = '(';
391 $parens_close = ')';
393 } else {
394 $parens_open = '';
395 $parens_close = '';
397 $enum_where = '\'' . PMA_sqlAddslashes($fields[$i][0]) . '\'';
398 if (PMA_MYSQL_INT_VERSION >= 40100 && $charsets[$i] != $charset_connection) {
399 $enum_where = 'CONVERT(_utf8 ' . $enum_where . ' USING ' . $charsets[$i] . ') COLLATE ' . $collations[$i];
401 for ($e = 1; $e < $enum_selected_count; $e++) {
402 $enum_where .= ', ';
403 $tmp_literal = '\'' . PMA_sqlAddslashes($fields[$i][$e]) . '\'';
404 if (PMA_MYSQL_INT_VERSION >= 40100 && $charsets[$i] != $charset_connection) {
405 $tmp_literal = 'CONVERT(_utf8 ' . $tmp_literal . ' USING ' . $charsets[$i] . ') COLLATE ' . $collations[$i];
407 $enum_where .= $tmp_literal;
408 unset($tmp_literal);
411 $w[] = PMA_backquote(urldecode($names[$i])) . ' ' . $func_type . ' ' . $parens_open . $enum_where . $parens_close;
414 } elseif ($fields[$i] != '') {
415 if (preg_match('@char|binary|blob|text|set|date|time|year@i', $types[$i])) {
416 $quot = '\'';
417 } else {
418 $quot = '';
421 // Make query independant from the selected connection charset.
422 // But if the field's type is VARBINARY, it has no charset
423 // and $charsets[$i] is empty, so we cannot generate a CONVERT
425 if (PMA_MYSQL_INT_VERSION >= 40101 && !empty($charsets[$i]) && $charsets[$i] != $charset_connection && preg_match('@char|binary|blob|text|set@i', $types[$i])) {
426 $prefix = 'CONVERT(_utf8 ';
427 $suffix = ' USING ' . $charsets[$i] . ') COLLATE ' . $collations[$i];
428 } else {
429 $prefix = $suffix = '';
432 // LIKE %...%
433 if ($func_type == 'LIKE %...%') {
434 $func_type = 'LIKE';
435 $fields[$i] = '%' . $fields[$i] . '%';
437 $w[] = PMA_backquote(urldecode($names[$i])) . ' ' . $func_type . ' ' . $prefix . $quot . PMA_sqlAddslashes($fields[$i]) . $quot . $suffix;
439 } // end if
440 } // end for
442 if ($w) {
443 $sql_query .= ' WHERE ' . implode(' AND ', $w);
445 } // end if
447 if ($orderField != '--nil--') {
448 $sql_query .= ' ORDER BY ' . PMA_backquote(urldecode($orderField)) . ' ' . $order;
449 } // end if
450 include('./sql.php');