5 * Credits for this script goes to Thomas Chaumeny <chaume92 at aol.com>
10 * Gets some core libraries and send headers
12 require('./db_details_common.php3');
13 // If config variable $cfg['Usedbsearch'] is on FALSE : exit.
14 if (!$cfg['UseDbSearch']) {
15 PMA_mysqlDie($strAccessDenied, '', FALSE, $err_url);
17 $url_query .= '&goto=db_search.php3';
21 * Get the list of tables from the current database
23 $list_tables = PMA_mysql_list_tables($db);
24 $num_tables = ($list_tables ?
mysql_num_rows($list_tables) : 0);
25 for ($i = 0; $i < $num_tables; $i++
) {
26 $tables[] = PMA_mysql_tablename($list_tables, $i);
29 mysql_free_result($list_tables);
37 require('./db_details_links.php3');
41 * 1. Main search form has been submitted
43 if (isset($submit_search)) {
46 * Builds the SQL search query
48 * @param string the table name
49 * @param string the string to search
50 * @param integer type of search (1 -> 1 word at least, 2 -> all words,
51 * 3 -> exact string, 4 -> regexp)
53 * @return array 3 SQL querys (for count, display and delete results)
55 * @global string the url to retun to in case of errors
57 function PMA_getSearchSqls($table, $search_str, $search_option)
62 $sqlstr_select = 'SELECT';
63 $sqlstr_delete = 'DELETE';
66 $local_query = 'SHOW FIELDS FROM ' . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($table);
67 $res = @PMA_mysql_query
($local_query) or PMA_mysqlDie('', $local_query, FALSE, $err_url);
68 $res_cnt = ($res ?
mysql_num_rows($res) : 0);
69 for ($i = 0; $i < $res_cnt; $i++
) {
70 $tblfields[] = PMA_backquote(PMA_mysql_result($res, $i, 'field'));
72 $sqlstr_fieldstoselect = ' ' . implode(', ', $tblfields);
73 $tblfields_cnt = count($tblfields);
75 mysql_free_result($res);
79 $sqlstr_from = ' FROM ' . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($table);
81 // Beginning of WHERE clause
82 $sqlstr_where = ' WHERE';
84 $search_words = (($search_option > 2) ?
array($search_str) : explode(' ', $search_str));
85 $search_wds_cnt = count($search_words);
87 $like_or_regex = (($search_option == 4) ?
'REGEXP' : 'LIKE');
89 for ($i = 0; $i < $search_wds_cnt; $i++
) {
90 // Elimines empty values
91 if (!empty($search_words[$i])) {
92 for ($j = 0; $j < $tblfields_cnt; $j++
) {
93 $thefieldlikevalue[] = $tblfields[$j]
94 . ' ' . $like_or_regex
95 . ' \'' . $search_words[$i] . '\'';
98 $fieldslikevalues[] = ($search_wds_cnt > 1)
99 ?
'(' . implode(' OR ', $thefieldlikevalue) . ')'
100 : implode(' OR ', $thefieldlikevalue);
101 unset($thefieldlikevalue);
105 $implode_str = ($search_option == 1 ?
' OR ' : ' AND ');
106 $sqlstr_where .= ' ' . implode($implode_str, $fieldslikevalues);
107 unset($fieldslikevalues);
109 // Builds complete queries
110 $sql['select_fields'] = $sqlstr_select . $sqlstr_fieldstoselect . $sqlstr_from . $sqlstr_where;
111 $sql['select_count'] = $sqlstr_select . ' COUNT(*) AS count' . $sqlstr_from . $sqlstr_where;
112 $sql['delete'] = $sqlstr_delete . $sqlstr_from . $sqlstr_where;
115 } // end of the "PMA_getSearchSqls()" function
119 * Strip slashes if necessary
121 if (get_magic_quotes_gpc()) {
122 $search_str = stripslashes($search_str);
124 $table = stripslashes($table);
126 else if (isset($table_select)) {
127 $table_select_cnt = count($table_select);
128 reset($table_select);
129 for ($i = 0; $i < $table_select_cnt; $i++
) {
130 $table_select[$i] = stripslashes($table_select[$i]);
132 } // end if... else if...
137 * Displays the results
139 if (!empty($search_str) && !empty($search_option)) {
141 $original_search_str = $search_str;
142 $search_str = PMA_sqlAddslashes($search_str, TRUE);
144 // Get the true string to display as option's comment
145 switch ($search_option) {
147 $option_str = ' (' . $strSearchOption1 . ')';
150 $option_str = ' (' . $strSearchOption2 . ')';
153 $option_str = ' (' . $strSearchOption3 . ')';
156 $option_str = ' (' . $strSearchOption4 . ')';
160 // If $table is defined or if there is only one table in $table_select
161 // set $onetable to the table's name (display is different if there is
165 // $tables is an array with all tables in database $db
166 // $num_tables is the size of $tables
170 else if (isset($table_select)) {
171 $num_selectedtables = count($table_select);
172 if ($num_selectedtables == 1) {
173 $onetable = $table_select[0];
176 else if ($num_tables == 1) {
177 $onetable = $tables[0];
180 for ($i = 0; $i < $num_tables; $i++
) {
181 $table_select[] = $tables[$i];
183 $num_selectedtables = $num_tables;
184 } // end if... else if... else
188 <!-- Results form
-->
189 <form method
="post" action
="sql.php3" name
="db_search_results_form">
190 <input type
="hidden" name
="is_js_confirmed" value
="0" />
191 <input type
="hidden" name
="lang" value
="<?php echo $lang; ?>" />
192 <input type
="hidden" name
="convcharset" value
="<?php echo $convcharset; ?>" />
193 <input type
="hidden" name
="server" value
="<?php echo $server; ?>" />
194 <input type
="hidden" name
="db" value
="<?php echo $db; ?>" />
195 <input type
="hidden" name
="goto" value
="db_details.php3" />
196 <input type
="hidden" name
="pos" value
="0" />
199 // Only one table defined in an variable $onetable
200 if (isset($onetable)) {
201 // Displays search string
202 echo ' ' . sprintf($strSearchResultsFor, htmlspecialchars($original_search_str), $option_str) . "\n";
203 echo ' <br />' . "\n";
205 // Gets the SQL statements
206 $newsearchsqls = PMA_getSearchSqls($onetable, $search_str, $search_option);
208 // Executes the "COUNT" statement
209 $local_query = $newsearchsqls['select_count'];
210 $res = @PMA_mysql_query
($local_query) or PMA_mysqlDie('', $local_query, FALSE, $err_url);
212 $res_cnt = PMA_mysql_result($res, 0, 'count');
213 mysql_free_result($res);
216 } // end if... else ...
217 $num_search_result_total = $res_cnt;
219 echo ' <!-- Search results in table ' . $onetable . ' (' . $res_cnt . ') -->' . "\n"
221 . ' ' . sprintf($strNumSearchResultsInTable, $res_cnt, htmlspecialchars($onetable)) . "\n";
224 echo ' <input id="checkbox_select_' . $i . '" type="radio" name="sql_query" value="' . htmlspecialchars($newsearchsqls['select_fields']) . '" onclick="this.form.submit()" />' . "\n";
225 echo ' <label for="checkbox_select_' . $i . '">' . $strBrowse . '</label>' . "\n";
226 echo ' <input id="checkbox_delete_' . $i . '" type="radio" name="sql_query" value="' . htmlspecialchars($newsearchsqls['delete']).'" onclick="this.form.submit()" />' . "\n";
227 echo ' <label for="checkbox_delete_' . $i . '">' . $strDelete . '</label>' . "\n";
229 } // end only one table
231 // Several tables defined in the array $table_select
232 else if (isset($table_select)) {
233 // Displays search string
234 echo ' ' . sprintf($strSearchResultsFor, htmlspecialchars($original_search_str), $option_str) . "\n";
237 $num_search_result_total = 0;
238 for ($i = 0; $i < $num_selectedtables; $i++
) {
239 // Gets the SQL statements
240 $newsearchsqls = PMA_getSearchSqls($table_select[$i], $search_str, $search_option);
242 // Executes the "COUNT" statement
243 $local_query = $newsearchsqls['select_count'];
244 $res = @PMA_mysql_query
($local_query) or PMA_mysqlDie('', $local_query, FALSE, $err_url);
246 $res_cnt = PMA_mysql_result($res, 0, 'count');
247 mysql_free_result($res);
250 } // end if... else ...
251 $num_search_result_total +
= $res_cnt;
253 echo ' <!-- Search results in table ' . $table_select[$i] . ' (' . $res_cnt . ') -->' . "\n"
255 . ' ' . sprintf($strNumSearchResultsInTable, $res_cnt, htmlspecialchars($table_select[$i])) . "\n";
258 echo ' <input id="checkbox_select_' . $i .'" type="radio" name="sql_query" value="' . htmlspecialchars($newsearchsqls['select_fields']) . '" onclick="this.form.submit()" />' . "\n";
259 echo ' <label for="checkbox_select_' . $i . '">' . $strBrowse . '</label>' . "\n";
260 echo ' <input id="checkbox_delete_' . $i . '" type="radio" name="sql_query" value="' . htmlspecialchars($newsearchsqls['delete']) . '" onclick="this.form.submit()" />' . "\n";
261 echo ' <label for="checkbox_delete_' . $i . '">' . $strDelete . '</label>' . "\n";
264 echo ' </li>' . "\n";
267 echo ' </ul>' . "\n";
268 echo ' <p>' . sprintf($strNumSearchResultsTotal, $num_search_result_total) . '</p>' . "\n";
269 } // end several tables
271 // Submit button if string found
272 if ($num_search_result_total) {
274 <script type
="text/javascript" language
="javascript">
276 // Fake js to allow the use of the <noscript> tag
280 <input type
="submit" value
="<?php echo $strGo; ?>" />
290 } // end if (!empty($search_str) && !empty($search_option))
296 * 2. Displays the main search form
299 $searched = (isset($original_search_str))
300 ?
htmlspecialchars($original_search_str)
302 if (empty($search_option)) {
306 <!-- Display search form
-->
308 <b
><?php
echo $strSearchFormTitle; ?
></b
>
311 <a name
="db_search"></a
>
312 <form method
="post" action
="db_search.php3" name
="db_search">
313 <input type
="hidden" name
="lang" value
="<?php echo $lang; ?>" />
314 <input type
="hidden" name
="convcharset" value
="<?php echo $convcharset; ?>" />
315 <input type
="hidden" name
="server" value
="<?php echo $server; ?>" />
316 <input type
="hidden" name
="db" value
="<?php echo $db; ?>" />
321 <?php
echo $strSearchNeedle; ?
> 
;
324 <input type
="text" name
="search_str" size
="30" value
="<?php echo $searched; ?>" />
328 <td colspan
="2"> 
;</td
>
332 <?php
echo $strSearchType; ?
> 
;
335 <input type
="radio" id
="search_option_1" name
="search_option" value
="1"<?php
if ($search_option == 1) echo ' checked="checked"'; ?
> />
336 <label
for="search_option_1"><?php
echo $strSearchOption1; ?
></label
> 
;*<br
/>
337 <input type
="radio" id
="search_option_2" name
="search_option" value
="2"<?php
if ($search_option == 2) echo ' checked="checked"'; ?
> />
338 <label
for="search_option_2"><?php
echo $strSearchOption2; ?
></label
> 
;*<br
/>
339 <input type
="radio" id
="search_option_3" name
="search_option" value
="3"<?php
if ($search_option == 3) echo ' checked="checked"'; ?
> />
340 <label
for="search_option_3"><?php
echo $strSearchOption3; ?
></label
><br
/>
341 <input type
="radio" id
="search_option_4" name
="search_option" value
="4"<?php
if ($search_option == 4) echo ' checked="checked"'; ?
> />
342 <label
for="search_option_4"><?php
echo $strSearchOption4 . '</label> ' . PMA_showDocuShort('R/e/Regexp.html'); ?
><br
/>
344 * 
;<?php
echo $strSplitWordsWithSpace . "\n"; ?
>
348 <td colspan
="2"> 
;</td
>
352 <?php
echo $strSearchInTables; ?
> 
;
356 if ($num_tables > 1) {
359 echo ' <select name="table_select[]" size="6" multiple="multiple">' . "\n";
360 while ($i < $num_tables) {
361 if (!empty($unselectall)) {
364 else if ((isset($table_select) && PMA_isInto($tables[$i], $table_select) != -1)
365 ||
(!empty($selectall))
366 ||
(isset($onetable) && $onetable == $tables[$i])) {
367 $is_selected = ' selected="selected"';
373 echo ' <option value="' . htmlspecialchars($tables[$i]) . '"' . $is_selected . '>' . htmlspecialchars($tables[$i]) . '</option>' . "\n";
376 echo ' </select>' . "\n";
379 <a href
="db_search.php3?<?php echo $url_query; ?>&selectall=1#db_search" onclick
="setSelectOptions('db_search', 'table_select[]', true); return false;"><?php
echo $strSelectAll; ?
></a
>
381 <a href
="db_search.php3?<?php echo $url_query; ?>&unselectall=1#db_search" onclick
="setSelectOptions('db_search', 'table_select[]', false); return false;"><?php
echo $strUnselectAll; ?
></a
>
386 echo ' ' . htmlspecialchars($tables[0]) . "\n";
387 echo ' <input type="hidden" name="table" value="' . htmlspecialchars($tables[0]) . '" />' . "\n";
388 } // end if... else...
396 <td colspan
="2"> 
;</td
>
399 <td colspan
="2"><input type
="submit" name
="submit_search" value
="<?php echo $strGo; ?>" /></td
>
407 * Displays the footer
410 require('./footer.inc.php3');