Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

Hi i am new to drupal 7 and also to sql query builder format.i have a requirement to search the string. in that i hav to display exact matched string at the top followed by related matches. i explode a string with space and searched a each word exist or not.the code as follows.

$keys -> contain the search term

 $keys = preg_replace('!\*+!', '%', $keys);

$keys = explode(' ', trim(str_replace(array( '\'', '"', ',' , ';', '<', '>','!','_','-','+','@','#','$','%','^','&','.'), ' ', $keys)));;

 $query = db_select('file')->extend('PagerDefault');
 $query->fields('file', array('fid','filename'))
  ->fields('fs', array('e_id','f_subtitle_value'));

 $subquery = db_select('field_data_sub','fs');
 $subquery->Fields('fs', array('e_id','f_subtitle_value'));
 $subquery->condition('fs.e_type', 'node','!=');

 $query->leftjoin($subquery,'fs','file.fid = fs.e_id');
 $db_or = db_or();
foreach ($keys as $key) {
if(!(empty($key))) {
 $db_or->condition('filename', "%$key%", 'LIKE');

          condition('filemime', 'image/jpeg', '!=')->
           condition('filemime', 'image/png', '!='));

i tried union all - wrote two query and combine it.. it fetches output but the exact searched result came twice . is that a gud way or else is there any other way.. please suggest the best way :)

share|improve this question
add comment

1 Answer 1

You can use default drupal's search from admin/config/search/settings and se Views module for searching specific fields. You need not write any code or Query to search in a Drupal site unless it is needed.

share|improve this answer
it is the custom module. the module is already created and i need to search the value in custom module's table. pls help me. –  suganya Nov 15 '12 at 6:43
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.