Documentation TYPO3 par Ameos |
00001 <?php 00002 /* $Id: tbl_select.php,v 2.27 2005/06/23 14:16:32 robbat2 Exp $ */ 00003 // vim: expandtab sw=4 ts=4 sts=4: 00004 00005 00009 require_once('./libraries/grab_globals.lib.php'); 00010 require_once('./libraries/common.lib.php'); 00011 require_once('./libraries/relation.lib.php'); // foreign keys 00012 require_once('./libraries/mysql_charsets.lib.php'); 00013 00014 if ($cfg['PropertiesIconic'] == true) { 00015 // We need to copy the value or else the == 'both' check will always return true 00016 $propicon = (string)$cfg['PropertiesIconic']; 00017 00018 if ($propicon == 'both') { 00019 $iconic_spacer = '<div class="nowrap">'; 00020 } else { 00021 $iconic_spacer = ''; 00022 } 00023 00024 $titles['Browse'] = $iconic_spacer . '<img width="16" height="16" src="' . $pmaThemeImage . 'b_browse.png" alt="' . $strBrowseForeignValues . '" title="' . $strBrowseForeignValues . '" border="0" />'; 00025 00026 if ($propicon == 'both') { 00027 $titles['Browse'] .= ' ' . $strBrowseForeignValues . '</div>'; 00028 } 00029 } else { 00030 $titles['Browse'] = $strBrowseForeignValues; 00031 } 00032 00036 if (!isset($param) || $param[0] == '') { 00037 // Gets some core libraries 00038 require('./tbl_properties_common.php'); 00039 //$err_url = 'tbl_select.php' . $err_url; 00040 $url_query .= '&goto=tbl_select.php&back=tbl_select.php'; 00041 00045 require('./tbl_properties_table_info.php'); 00046 00050 require('./tbl_properties_links.php'); 00051 00052 if (!isset($goto)) { 00053 $goto = $cfg['DefaultTabTable']; 00054 } 00055 // Defines the url to return to in case of error in the next sql statement 00056 $err_url = $goto . '?' . PMA_generate_common_url($db, $table); 00057 00058 // Gets the list and number of fields 00059 $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); 00060 $fields_cnt = PMA_DBI_num_rows($result); 00061 // rabue: we'd better ensure, that all arrays are empty. 00062 $fields_list = $fields_null = $fields_type = $fields_collation = array(); 00063 while ($row = PMA_DBI_fetch_assoc($result)) { 00064 $fields_list[] = $row['Field']; 00065 $type = $row['Type']; 00066 // reformat mysql query output - staybyte - 9. June 2001 00067 if (strncasecmp($type, 'set', 3) == 0 00068 || strncasecmp($type, 'enum', 4) == 0) { 00069 $type = str_replace(',', ', ', $type); 00070 } else { 00071 00072 // strip the "BINARY" attribute, except if we find "BINARY(" because 00073 // this would be a BINARY or VARBINARY field type 00074 if (!preg_match('@BINARY[\(]@i', $type)) { 00075 $type = preg_replace('@BINARY@i', '', $type); 00076 } 00077 $type = preg_replace('@ZEROFILL@i', '', $type); 00078 $type = preg_replace('@UNSIGNED@i', '', $type); 00079 00080 $type = strtolower($type); 00081 } 00082 if (empty($type)) { 00083 $type = ' '; 00084 } 00085 $fields_null[] = $row['Null']; 00086 $fields_type[] = $type; 00087 $fields_collation[] = PMA_MYSQL_INT_VERSION >= 40100 && !empty($row['Collation']) && $row['Collation'] != 'NULL' 00088 ? $row['Collation'] 00089 : ''; 00090 } // end while 00091 PMA_DBI_free_result($result); 00092 unset($result, $type); 00093 00094 // <markus@noga.de> 00095 // retrieve keys into foreign fields, if any 00096 $cfgRelation = PMA_getRelationsParam(); 00097 // check also foreigners even if relwork is FALSE (to get 00098 // foreign keys from innodb) 00099 //$foreigners = ($cfgRelation['relwork'] ? PMA_getForeigners($db, $table) : FALSE); 00100 $foreigners = PMA_getForeigners($db, $table); 00101 ?> 00102 <script language="JavaScript" type="text/javascript"> 00103 <!-- 00104 function PMA_tbl_select_operator(f, index, multiple) { 00105 switch (f.elements["func[" + index + "]"].options[f.elements["func[" + index + "]"].selectedIndex].value) { 00106 <?php 00107 reset($GLOBALS['cfg']['UnaryOperators']); 00108 while (list($operator) = each($GLOBALS['cfg']['UnaryOperators'])) { 00109 echo ' case "' . $operator . "\":\r\n"; 00110 } 00111 ?> 00112 bDisabled = true; 00113 break; 00114 00115 default: 00116 bDisabled = false; 00117 } 00118 f.elements["fields[" + index + "]" + ((multiple) ? "[]": "")].disabled = bDisabled; 00119 } 00120 // --> 00121 </script> 00122 <form method="post" action="tbl_select.php" name="insertForm"> 00123 <?php echo PMA_generate_common_hidden_inputs($db, $table); ?> 00124 <input type="hidden" name="goto" value="<?php echo $goto; ?>" /> 00125 <input type="hidden" name="back" value="tbl_select.php" /> 00126 <table border="0" cellpadding="0" cellspacing="0"> 00127 <tr> 00128 <td valign="top"> 00129 <table border="0" cellpadding="3" cellspacing="0"> 00130 <tr> 00131 <th align="left" colspan="2"> 00132 <?php echo $strSelectFields; ?> 00133 </th> 00134 </tr> 00135 <tr> 00136 <td bgcolor="<?php echo $cfg['BgcolorOne']; ?>"> 00137 <select name="param[]" size="<?php echo min($fields_cnt, 10); ?>" multiple="multiple" align="left"> 00138 <?php 00139 echo "\n"; 00140 // Displays the list of the fields 00141 for ($i = 0 ; $i < $fields_cnt; $i++) { 00142 echo ' ' 00143 . '<option value="' . htmlspecialchars($fields_list[$i]) . '" selected="selected">' . htmlspecialchars($fields_list[$i]) . '</option>' . "\n"; 00144 } 00145 ?> 00146 </select> 00147 </td> 00148 <td valign="bottom" bgcolor="<?php echo $cfg['BgcolorOne']; ?>"> 00149 <input type="checkbox" name="distinct" value="DISTINCT" id="oDistinct" /><label for="oDistinct">DISTINCT</label> 00150 </td> 00151 </tr> 00152 </table> 00153 </td> 00154 <td nowrap="nowrap" width="50"> </td> 00155 <td valign="top"> 00156 <table border="0" cellpadding="3" cellspacing="0"> 00157 <tr> 00158 <th align="left"> 00159 <?php echo $strLimitNumRows . "\n"; ?>: 00160 </th> 00161 </tr> 00162 <tr> 00163 <td bgcolor="<?php echo $cfg['BgcolorOne']; ?>"> 00164 <input type="text" size="4" name="session_max_rows" value="<?php echo $cfg['MaxRows']; ?>" class="textfield" /> 00165 </td> 00166 </tr> 00167 <tr> 00168 <td> </td> 00169 </tr> 00170 <tr> 00171 <th align="left"> 00172 <?php echo $strDisplayOrder; ?> 00173 </th> 00174 </tr> 00175 <tr> 00176 <td bgcolor="<?php echo $cfg['BgcolorOne']; ?>"> 00177 <select name="orderField" style="vertical-align: middle"> 00178 <option value="--nil--"></option> 00179 <?php 00180 echo "\n"; 00181 for ($i = 0; $i < $fields_cnt; $i++) { 00182 echo ' '; 00183 echo '<option value="' . htmlspecialchars($fields_list[$i]) . '">' . htmlspecialchars($fields_list[$i]) . '</option>' . "\n"; 00184 } // end for 00185 ?> 00186 </select><br /> 00187 <input type="radio" name="order" value="ASC" checked="checked" id="sortASC" /><label for="sortASC"><?php echo $strAscending; ?></label><br /> 00188 <input type="radio" name="order" value="DESC" id="sortDESC" /><label for="sortDESC"><?php echo $strDescending; ?></label> 00189 </td> 00190 </tr> 00191 </table> 00192 </td> 00193 </tr> 00194 </table> 00195 <table border="0" cellpadding="3" cellspacing="0"> 00196 <tr> 00197 <td colspan="2"> </td> 00198 </tr> 00199 <tr> 00200 <th align="left" class="tblHeaders" colspan="2"> 00201 <?php echo $strAddSearchConditions; ?> 00202 <?php echo PMA_showMySQLDocu('Reference', 'Functions') . "\n"; ?> 00203 </th> 00204 </tr> 00205 <tr> 00206 <td> 00207 <input type="text" name="where" class="textfield" size="64" /> 00208 </td> 00209 <td align="right"> 00210 <input type="submit" name="submit" value="<?php echo $strGo; ?>" /> 00211 </td> 00212 </tr> 00213 <tr> 00214 <td colspan="2"> </td> 00215 </tr> 00216 <tr> 00217 <th align="left" class="tblHeaders" colspan="2"> 00218 <?php echo '<i>' . $strOr . '</i> ' . $strDoAQuery; ?> 00219 </th> 00220 </tr> 00221 <tr> 00222 <td colspan="2"> 00223 <table border="<?php echo $cfg['Border']; ?>" cellpadding="2" cellspacing="1"> 00224 <tr> 00225 <th><?php echo $strField; ?></th> 00226 <th><?php echo $strType; ?></th> 00227 <?php echo PMA_MYSQL_INT_VERSION >= 40100 ? '<th>' . $strCollation . '</th>' . "\n" : ''; ?> 00228 <th><?php echo $strOperator; ?></th> 00229 <th><?php echo $strValue; ?></th> 00230 </tr> 00231 <?php 00232 for ($i = 0; $i < $fields_cnt; $i++) { 00233 echo "\n"; 00234 $bgcolor = ($i % 2) ? $cfg['BgcolorOne'] : $cfg['BgcolorTwo']; 00235 ?> 00236 <tr> 00237 <td bgcolor="<?php echo $bgcolor; ?>"><b><?php echo htmlspecialchars($fields_list[$i]); ?></b></td> 00238 <td bgcolor="<?php echo $bgcolor; ?>"><?php echo $fields_type[$i]; ?></td> 00239 <?php echo PMA_MYSQL_INT_VERSION >= 40100 ? '<td bgcolor="' . $bgcolor . '">' . $fields_collation[$i] . '</td>' . "\n" : ''; ?> 00240 <td bgcolor="<?php echo $bgcolor; ?>"> 00241 <select name="func[]"> 00242 <?php 00243 if (strncasecmp($fields_type[$i], 'enum', 4) == 0) { 00244 foreach ($GLOBALS['cfg']['EnumOperators'] as $k => $fc) { 00245 echo "\n" . ' ' 00246 . '<option value="' . htmlspecialchars($fc) . '">' . htmlspecialchars($fc) . '</option>'; 00247 } 00248 } elseif (preg_match('@char|blob|text|set@i', $fields_type[$i])) { 00249 foreach ($GLOBALS['cfg']['TextOperators'] as $k => $fc) { 00250 echo "\n" . ' ' 00251 . '<option value="' . htmlspecialchars($fc) . '">' . htmlspecialchars($fc) . '</option>'; 00252 } 00253 } else { 00254 foreach ($GLOBALS['cfg']['NumOperators'] as $k => $fc) { 00255 echo "\n" . ' ' 00256 . '<option value="' . htmlspecialchars($fc) . '">' . htmlspecialchars($fc) . '</option>'; 00257 } 00258 } // end if... else... 00259 if ($fields_null[$i]) { 00260 foreach ($GLOBALS['cfg']['NullOperators'] as $k => $fc) { 00261 echo "\n" . ' ' 00262 . '<option value="' . htmlspecialchars($fc) . '">' . htmlspecialchars($fc) . '</option>'; 00263 } 00264 } 00265 echo "\n"; 00266 ?> 00267 </select> 00268 </td> 00269 <td bgcolor="<?php echo $bgcolor; ?>"> 00270 <?php 00271 // <markus@noga.de> 00272 $field = $fields_list[$i]; 00273 00274 // do not use require_once here 00275 require('./libraries/get_foreign.lib.php'); 00276 00277 echo "\n"; 00278 // we got a bug report: in some cases, even if $disp is true, 00279 // there are no rows, so we add a fetch_array 00280 00281 if ($foreigners && isset($foreigners[$field]) && isset($disp_row) && is_array($disp_row)) { 00282 // f o r e i g n k e y s 00283 echo ' <select name="fields[' . $i . ']">' . "\n"; 00284 // go back to first row 00285 00286 // here, the 4th parameter is empty because there is no current 00287 // value of data for the dropdown (the search page initial values 00288 // are displayed empty) 00289 echo PMA_foreignDropdown($disp_row, $foreign_field, $foreign_display, '', $cfg['ForeignKeyMaxLimit']); 00290 echo ' </select>' . "\n"; 00291 } else if (isset($foreign_link) && $foreign_link == true) { 00292 ?> 00293 <input type="text" name="fields[<?php echo $i; ?>]" id="field_<?php echo md5($field); ?>[<?php echo $i; ?>]" class="textfield" /> 00294 <script type="text/javascript" language="javascript"> 00295 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>'); 00296 </script> 00297 <?php 00298 } else if (strncasecmp($fields_type[$i], 'enum', 4) == 0) { 00299 // e n u m s 00300 $enum_value=explode(', ', str_replace("'", '', substr($fields_type[$i], 5, -1))); 00301 $cnt_enum_value = count($enum_value); 00302 echo ' <select name="fields[' . $i . '][]" multiple="multiple" size="' . min(3, $cnt_enum_value) . '">' . "\n"; 00303 for ($j = 0; $j < $cnt_enum_value; $j++) { 00304 echo ' <option value="' . $enum_value[$j] . '">' . $enum_value[$j] . '</option>'; 00305 } // end for 00306 echo ' </select>' . "\n"; 00307 } else { 00308 // o t h e r c a s e s 00309 echo ' <input type="text" name="fields[' . $i . ']" size="40" class="textfield" />' . "\n"; 00310 } 00311 00312 ?> 00313 <input type="hidden" name="names[<?php echo $i; ?>]" value="<?php echo htmlspecialchars($fields_list[$i]); ?>" /> 00314 <input type="hidden" name="types[<?php echo $i; ?>]" value="<?php echo $fields_type[$i]; ?>" /> 00315 <input type="hidden" name="collations[<?php echo $i; ?>]" value="<?php echo $fields_collation[$i]; ?>" /> 00316 </td> 00317 </tr> 00318 <?php 00319 } // end for 00320 echo "\n"; 00321 ?> 00322 </table> 00323 </td> 00324 </tr> 00325 <tr> 00326 <td nowrap="nowrap" colspan="2" align="right"> 00327 <input type="hidden" name="max_number_of_fields" value="<?php echo $fields_cnt; ?>" /> 00328 <input type="submit" name="submit" value="<?php echo $strGo; ?>" /> 00329 </td> 00330 </tr> 00331 </table> 00332 </form> 00333 <?php 00334 require_once('./footer.inc.php'); 00335 } 00336 00337 00341 else { 00342 // Builds the query 00343 00344 $sql_query = 'SELECT ' . (isset($distinct) ? 'DISTINCT ' : ''); 00345 00346 // if all fields were selected to display, we do a SELECT * 00347 // (more efficient and this helps prevent a problem in IE 00348 // if one of the rows is edited and we come back to the Select results) 00349 00350 if (count($param) == $max_number_of_fields) { 00351 $sql_query .= '* '; 00352 } else { 00353 00354 $sql_query .= PMA_backquote(urldecode($param[0])); 00355 $c = count($param); 00356 for ($i = 1; $i < $c; $i++) { 00357 $sql_query .= ',' . PMA_backquote(urldecode($param[$i])); 00358 } 00359 } // end if 00360 00361 $sql_query .= ' FROM ' . PMA_backquote($table); 00362 00363 // The where clause 00364 if (trim($where) != '') { 00365 $sql_query .= ' WHERE ' . $where; 00366 } else { 00367 $w = $charsets = array(); 00368 $cnt_func = count($func); 00369 reset($func); 00370 while (list($i, $func_type) = each($func)) { 00371 if (PMA_MYSQL_INT_VERSION >= 40100) { 00372 list($charsets[$i]) = explode('_', $collations[$i]); 00373 } 00374 if (@$GLOBALS['cfg']['UnaryOperators'][$func_type] == 1) { 00375 $fields[$i] = ''; 00376 $w[] = PMA_backquote(urldecode($names[$i])) . ' ' . $func_type; 00377 00378 } elseif (strncasecmp($types[$i], 'enum', 4) == 0) { 00379 if (!empty($fields[$i])) { 00380 if (!is_array($fields[$i])) { 00381 $fields[$i] = explode(',', $fields[$i]); 00382 } 00383 $enum_selected_count = count($fields[$i]); 00384 if ($func_type == '=' && $enum_selected_count > 1) { 00385 $func_type = $func[$i] = 'IN'; 00386 $parens_open = '('; 00387 $parens_close = ')'; 00388 00389 } elseif ($func_type == '!=' && $enum_selected_count > 1) { 00390 $func_type = $func[$i] = 'NOT IN'; 00391 $parens_open = '('; 00392 $parens_close = ')'; 00393 00394 } else { 00395 $parens_open = ''; 00396 $parens_close = ''; 00397 } 00398 $enum_where = '\'' . PMA_sqlAddslashes($fields[$i][0]) . '\''; 00399 if (PMA_MYSQL_INT_VERSION >= 40100 && $charsets[$i] != $charset_connection) { 00400 $enum_where = 'CONVERT(_utf8 ' . $enum_where . ' USING ' . $charsets[$i] . ') COLLATE ' . $collations[$i]; 00401 } 00402 for ($e = 1; $e < $enum_selected_count; $e++) { 00403 $enum_where .= ', '; 00404 $tmp_literal = '\'' . PMA_sqlAddslashes($fields[$i][$e]) . '\''; 00405 if (PMA_MYSQL_INT_VERSION >= 40100 && $charsets[$i] != $charset_connection) { 00406 $tmp_literal = 'CONVERT(_utf8 ' . $tmp_literal . ' USING ' . $charsets[$i] . ') COLLATE ' . $collations[$i]; 00407 } 00408 $enum_where .= $tmp_literal; 00409 unset($tmp_literal); 00410 } 00411 00412 $w[] = PMA_backquote(urldecode($names[$i])) . ' ' . $func_type . ' ' . $parens_open . $enum_where . $parens_close; 00413 } 00414 00415 } elseif ($fields[$i] != '') { 00416 if (preg_match('@char|binary|blob|text|set|date|time|year@i', $types[$i])) { 00417 $quot = '\''; 00418 } else { 00419 $quot = ''; 00420 } 00421 00422 // Make query independant from the selected connection charset. 00423 // But if the field's type is VARBINARY, it has no charset 00424 // and $charsets[$i] is empty, so we cannot generate a CONVERT 00425 00426 if (PMA_MYSQL_INT_VERSION >= 40101 && !empty($charsets[$i]) && $charsets[$i] != $charset_connection && preg_match('@char|binary|blob|text|set@i', $types[$i])) { 00427 $prefix = 'CONVERT(_utf8 '; 00428 $suffix = ' USING ' . $charsets[$i] . ') COLLATE ' . $collations[$i]; 00429 } else { 00430 $prefix = $suffix = ''; 00431 } 00432 00433 // LIKE %...% 00434 if ($func_type == 'LIKE %...%') { 00435 $func_type = 'LIKE'; 00436 $fields[$i] = '%' . $fields[$i] . '%'; 00437 } 00438 $w[] = PMA_backquote(urldecode($names[$i])) . ' ' . $func_type . ' ' . $prefix . $quot . PMA_sqlAddslashes($fields[$i]) . $quot . $suffix; 00439 00440 } // end if 00441 } // end for 00442 00443 if ($w) { 00444 $sql_query .= ' WHERE ' . implode(' AND ', $w); 00445 } 00446 } // end if 00447 00448 if ($orderField != '--nil--') { 00449 $sql_query .= ' ORDER BY ' . PMA_backquote(urldecode($orderField)) . ' ' . $order; 00450 } // end if 00451 include('./sql.php'); 00452 } 00453 00454 ?>