38
38
* @uses $_REQUEST['table_select']
39
39
* @uses $_REQUEST['unselectall']
40
40
* @uses $_REQUEST['selectall']
41
* @uses $_REQUEST['field_str']
41
42
* @uses is_string()
42
43
* @uses htmlspecialchars()
43
44
* @uses array_key_exists()
99
100
// For "as regular expression" (search option 4), we should not treat
100
101
// this as an expression that contains a LIKE (second parameter of
101
102
// PMA_sqlAddslashes()).
103
104
// Usage example: If user is seaching for a literal $ in a regexp search,
104
105
// he should enter \$ as the value.
105
106
$search_str = PMA_sqlAddslashes($_REQUEST['search_str'], ($search_option == 4 ? false : true));
146
152
* @param string the table name
153
* @param string restrict the search to this field
147
154
* @param string the string to search
148
155
* @param integer type of search (1 -> 1 word at least, 2 -> all words,
149
156
* 3 -> exact string, 4 -> regexp)
153
160
* @global string the url to return to in case of errors
154
161
* @global string charset connection
156
function PMA_getSearchSqls($table, $search_str, $search_option)
163
function PMA_getSearchSqls($table, $field, $search_str, $search_option)
158
global $err_url, $charset_connection;
160
167
// Statement types
161
168
$sqlstr_select = 'SELECT';
162
169
$sqlstr_delete = 'DELETE';
164
171
// Fields to select
165
$res = PMA_DBI_query('SHOW ' . (PMA_MYSQL_INT_VERSION >= 40100 ? 'FULL ' : '') . 'FIELDS FROM ' . PMA_backquote($table) . ' FROM ' . PMA_backquote($GLOBALS['db']) . ';');
166
while ($current = PMA_DBI_fetch_assoc($res)) {
167
if (PMA_MYSQL_INT_VERSION >= 40100) {
168
list($current['Charset']) = explode('_', $current['Collation']);
170
$current['Field'] = PMA_backquote($current['Field']);
171
$tblfields[] = $current;
173
PMA_DBI_free_result($res);
174
unset($current, $res);
172
$tblfields = PMA_DBI_fetch_result('SHOW FIELDS FROM ' . PMA_backquote($table) . ' FROM ' . PMA_backquote($GLOBALS['db']),
177
176
$sqlstr_from = ' FROM ' . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($table);
180
179
$search_wds_cnt = count($search_words);
182
181
$like_or_regex = (($search_option == 4) ? 'REGEXP' : 'LIKE');
183
$automatic_wildcard = (($search_option <3) ? '%' : '');
182
$automatic_wildcard = (($search_option < 3) ? '%' : '');
185
184
$fieldslikevalues = array();
186
185
foreach ($search_words as $search_word) {
187
186
// Eliminates empty values
188
// In MySQL 4.1, if a field has no collation we get NULL in Charset
189
// but in MySQL 5.0.x we get ''
190
187
if (strlen($search_word) === 0) {
194
191
$thefieldlikevalue = array();
195
192
foreach ($tblfields as $tblfield) {
196
if (PMA_MYSQL_INT_VERSION >= 40100
197
&& $tblfield['Charset'] != $charset_connection
198
&& $tblfield['Charset'] != 'NULL'
199
&& $tblfield['Charset'] != '') {
200
$prefix = 'CONVERT(_utf8 ';
201
$suffix = ' USING ' . $tblfield['Charset'] . ') COLLATE ' . $tblfield['Collation'];
203
$prefix = $suffix = '';
193
if (! isset($field) || strlen($field) == 0 || $tblfield == $field) {
194
$thefieldlikevalue[] = PMA_backquote($tblfield)
195
. ' ' . $like_or_regex . ' '
196
. "'" . $automatic_wildcard
198
. $automatic_wildcard . "'";
205
$thefieldlikevalue[] = $tblfield['Field']
206
. ' ' . $like_or_regex . ' '
209
. $automatic_wildcard
211
. $automatic_wildcard . "'"
215
$fieldslikevalues[] = implode(' OR ', $thefieldlikevalue);
202
if (count($thefieldlikevalue) > 0) {
203
$fieldslikevalues[] = implode(' OR ', $thefieldlikevalue);
218
207
$implode_str = ($search_option == 1 ? ' OR ' : ' AND ');
219
$sqlstr_where = ' WHERE (' . implode(') ' . $implode_str . ' (', $fieldslikevalues) . ')';
208
if ( empty($fieldslikevalues)) {
209
// this could happen when the "inside field" does not exist
210
// in any selected tables
211
$sqlstr_where = ' WHERE FALSE';
213
$sqlstr_where = ' WHERE (' . implode(') ' . $implode_str . ' (', $fieldslikevalues) . ')';
220
215
unset($fieldslikevalues);
222
217
// Builds complete queries
225
220
// VIEWs, anyway we have a WHERE clause that should limit results
226
221
$sql['select_count'] = $sqlstr_select . ' COUNT(*) AS `count`' . $sqlstr_from . $sqlstr_where;
227
222
$sql['delete'] = $sqlstr_delete . $sqlstr_from . $sqlstr_where;
229
225
} // end of the "PMA_getSearchSqls()" function
253
249
foreach ($tables_selected as $each_table) {
254
250
// Gets the SQL statements
255
$newsearchsqls = PMA_getSearchSqls($each_table,
256
$search_str, $search_option);
251
$newsearchsqls = PMA_getSearchSqls($each_table, (! empty($field_str) ? $field_str : ''), $search_str, $search_option);
258
253
// Executes the "COUNT" statement
259
254
$res_cnt = PMA_DBI_fetch_value($newsearchsqls['select_count']);
311
306
<tr><td align="right" valign="top">
312
307
<?php echo $GLOBALS['strSearchType']; ?></td>
313
<td><input type="radio" id="search_option_1" name="search_option"
314
value="1"<?php if ($search_option == 1) echo ' checked="checked"'; ?> />
315
<label for="search_option_1">
316
<?php echo $GLOBALS['strSearchOption1']; ?></label><sup>1</sup><br />
317
<input type="radio" id="search_option_2" name="search_option"
318
value="2"<?php if ($search_option == 2) echo ' checked="checked"'; ?> />
319
<label for="search_option_2">
320
<?php echo $GLOBALS['strSearchOption2']; ?></label><sup>1</sup><br />
321
<input type="radio" id="search_option_3" name="search_option"
322
value="3"<?php if ($search_option == 3) echo ' checked="checked"'; ?> />
323
<label for="search_option_3">
324
<?php echo $GLOBALS['strSearchOption3']; ?></label><br />
325
<input type="radio" id="search_option_4" name="search_option"
326
value="4"<?php if ($search_option == 4) echo ' checked="checked"'; ?> />
327
<label for="search_option_4">
328
<?php echo $GLOBALS['strSearchOption4']; ?></label>
330
<?php echo PMA_showMySQLDocu('Regexp', 'Regexp'); ?><br />
332
<sup>1</sup><?php echo $GLOBALS['strSplitWordsWithSpace']; ?></td>
311
'1' => $GLOBALS['strSearchOption1'] . PMA_showHint($GLOBALS['strSplitWordsWithSpace']),
312
'2' => $GLOBALS['strSearchOption2'] . PMA_showHint($GLOBALS['strSplitWordsWithSpace']),
313
'3' => $GLOBALS['strSearchOption3'],
314
'4' => $GLOBALS['strSearchOption4'] . ' ' . PMA_showMySQLDocu('Regexp', 'Regexp')
316
// 4th parameter set to false to add line breaks
317
// 5th parameter set to false to avoid htmlspecialchars() escaping in the label
318
// since we have some HTML in some labels
319
PMA_generate_html_radio('search_option', $choices, $search_option, true, false);
334
324
<tr><td align="right" valign="top">
335
325
<?php echo $GLOBALS['strSearchInTables']; ?></td>
361
351
<tr><td align="right" valign="bottom">
362
352
<?php echo $alter_select; ?></td></tr>
354
<tr><td align="right">
355
<?php echo $GLOBALS['strSearchInField']; ?></td>
356
<td><input type="text" name="field_str" size="60"
357
value="<?php echo ! empty($field_str) ? $field_str : ''; ?>" /></td>
366
361
<fieldset class="tblFooters">