Search Drupal User Profiles

Drupal
PHP
It is difficult to search for users (in code) based on profile information in Drupal 6 due to the flexible nature in which the profiles module stores information. Basically profiles are stored in 2 tables.
mysql> select * from profile_fields;
+-----+------------+-------------------+-------------+---------------------+------+-----------+--------+----------+----------+------------+--------------+---------+
| fid | title      | name              | explanation | category            | page | type      | weight | required | register | visibility | autocomplete | options |
+-----+------------+-------------------+-------------+---------------------+------+-----------+--------+----------+----------+------------+--------------+---------+
|   1 | First Name | profile_firstname |             | General Information |      | textfield |      0 |        0 |        1 |          1 |            0 |         |
|   2 | Last Name  | profile_lastname  |             | General Information |      | textfield |      0 |        0 |        1 |          1 |            0 |         |                                          
+-----+------------+-------------------+-------------+---------------------+------+-----------+--------+----------+----------+------------+--------------+---------+                                          
2 rows in set (0.00 sec) 
select * from profile_values limit 4;                                                                                                                                                            
+-----+-----+---------+                                                                                                                                                                                       
| fid | uid | value   |                                                                                                                                                                                       
+-----+-----+---------+                                                                                                                                                                                       
|   1 |   1 | John    |                                                                                                                                                                                       
|   2 |   1 | Doe     |                                                                                                                                                                                       
|   1 | 115 | John    |                                                                                                                                                                                       
|   2 | 115 | Smith   |                                                                                                                                                                                       
+-----+-----+---------+                                                                                                                                                                                       
4 rows in set (0.00 sec)
With only one value per row it is difficult to create a generic function to search for multiple values at once. We could examine both tables and hard code a mysql function using multiple joins for each fid but that isn'r very reusable. Thankfully the mysql GROUP_CONCAT function comes to the rescue. Combined with a simple function to get profile field definitions it is possible to search for users based on profile information. Here are two simple functions I came up with that make it easy to search for users based on profile information in drupal
/**
 * dgs_get_profile_fields
 * @return array(name => fid)
 */
function dgs_get_profile_fields($category = null){
  if(isset($profile_fields)){
    return $profile_fields;
  }
  static $profile_fields = array();
  $sql = "SELECT * FROM {profile_fields}";
  $args = array();
  if(!empty($category)){
    $sql .= " WHERE category='%s'";
    $args= array($category);
  }
  $result = db_query($sql,$args);
  while($row = db_fetch_object($result)){
    $profile_fields[$row->name] = $row->fid;
  }
  return $profile_fields;
}

/**
 * dgs_search_profiles
 * @param(array(fieldname => fieldvalue));
 * @return an array of user uids
 */
function dgs_search_profiles($params = array()){
  //Example SQL
  //SELECT GROUP_CONCAT(fid,'=',value) as profile from profile_values GROUP BY(uid) HAVING profile like "%1=Gary%" AND profile like "%2=Varnell%";
  $profile_fields = dgs_get_profile_fields();
  $having = array();
  $args = array();
  foreach($params as $fieldname => $fieldvalue){
    $having[] = "profile LIKE '%s'";
    $args[] = "%" . $profile_fields[$fieldname] . "=$fieldvalue%";
  }
  $having = join('AND ',array_values($having));
  $result = db_query("SELECT uid,GROUP_CONCAT(fid,'=',value) as profile from profile_values GROUP BY(uid) HAVING $having",$args);
  $users = array();
  while($row = db_fetch_object($result)){
    $users[$row->uid] = user_load($row->uid);
  }
  return $users;
}
And here is a usage example
    $users = dgs_search_profiles(array('profile_firstname' => 'John','profile_lastname' => 'Doe'));
    print_r($users);
Which returns
Array
(
    [xxx] => stdClass Object
        (
            [uid] => xxx
            [name] => jdoe
            [pass] => xxxxxxxx
            [mail] => jdoe@somewhere.com
            [mode] => 0
            [sort] => 0
            [threshold] => 0
            [theme] => 
            [signature] => 
            [signature_format] => 0
            [created] => 1264712914
            [access] => 1318017735
            [login] => 1317926353
            [status] => 1
            [timezone] => -21600
            [language] => 
            [picture] => 
            [init] => xxxxx
            [data] => a:1:{s:13:"form_build_id";s:37:"form-xxxxxxxxxxxxxxxxxxxxx";}
            [form_build_id] => form-xxxxxxxxxxxxxxxxx
            [roles] => Array
                (
                    [2] => authenticated user
                    [3] => xxxxxxx
                    [5] => xxxxxxx
                )

            [profile_firstname] => John
            [profile_lastname] => Doe
        )

If you have a better solution please let me know, and as always feel free to comment.
Comments
(Thanks to everyone who comments)

Thank you a lot for

Thank you a lot for providing individuals with a very spectacular possibility to read critical reviews from this site. botox seminars for physicians


This is a smart blog. I mean

This is a smart blog. I mean it. You have so much knowledge about this issue, and so much passion. You also know how to make people rally behind it, obviously from the responses. Youve got a design here thats not too flashy, but makes a statement as big as what youre saying. Great job, indeed.

detoxifying pads for the feet


Thanks for ones marvelous

Thanks for ones marvelous posting! I genuinely enjoyed reading it, you are a great author. I will be sure to bookmark your blog and may come back very soon. I want to encourage you to definitely continue your great job, have a nice weekend.
http://motuandpatlugames.com


Payday Loans Carson

You'll find it a classic super research inside issue. We applied the objective and Efficient looking examining brand-new subject matter. In recent times follow through Payday Loans Carson Bless you designed for attractive tips.


2048 game

Thanks for sharing your info. I really appreciate your efforts and I will be waiting for your further write.
Thanks for sharing !
tanki online 2 | 2048 game online


This is exactly what I was

This is exactly what I was looking for. Thanks for sharing this great article! That is very interesting Smile I love reading and I am always searching for informative information like this!
Fashion Styling Tips


It's a great blog post and

It's a great blog post and read it twice for better knowledge. This is an impressing article which gives us good thoughts and interests to read more article like the same 192.168.1.1 192.168.1.1 192.168.1.1 192.168.1.1


blthuong

I can’t help but fall in love with the place. Thank you so much for a wonderful post.
zoxy 2 game pro
friv 5 that
yoob three
agario.org
friv 10
slitherio kizi
Just come across here and I fell in love with the main actor. What should I do now?
kizi agario
slitherio online
friv4school best game
bloxorz
motox3m Interesting
kizi 100
Go to link and play free 5005 game best: action game - frozen game - io game- racing game-Girl game - shoot game - Girl game - puzzle game ....


Book your Apartment in M city

Well, I am so fascinate to come over blog regularly it seems very up-to-date your blog keep me update it is really hard to keep updates own self in this busy life. Your blog is a way who hence and as well as aware me about new things so keep writing cheers.
http://mcitycondosvips.ca


Available Condos for Sale in Panda Condominiums

I was searching a site like this, because I found all information here. Your all articles are amazing and i enjoy to read your all blogs and I would really like to read your new articles.
Read More:- http://pandavip.ca/


Click here The web site is

Click here
The web site is lovingly serviced and saved as much as date. So it should be, thanks for sharing this with us.


VIDMATE

Vidmate is an app that lets you download videos and songs from the Youtube, Metacafe, Vimeo, Soundcloud and from other popular multimedia websites. This app lets you download any number of videos or songs of various qualities. Yes, you can download any quality of video or song that you wish to. You can download full HD (High Definition) videos here too. All official and working download links are provided over here and you don’t need to bother about that at all. It lets you watch the videos or songs directly without having them to be downloaded.
Download :- vidmate movies


Click Here

DL Printing provides fast online printing for both homes and businesses. We provide high quality DL cards, for all occasions, with a super quick turnaround.

Click Here


langkawi and kuala Lumpu packages

Showbox for iPhone

Showbox for iPhone Download here


Whey Proteins

Individual who made this post, this was an extremely useful for me. Acknowledges for posting this, I saw a unite else alike notify however yours was the prime so deep, always has really good posts and topics please keep it up!
www.wellwisdom.com/ultimate-grass-fed-whey-protein


kingswayrivercondos

I feel so fortunate to read your blog it looks really very attractive and very passionate kind of reading stuff. I feel it’s a perfect platform i spend a lot time over your website looking forward.
Condos on Kingsway River


Chocolate Day 2017

Visit us now to view & share beautiful collection of Best Images of
chocolate day images


AR 10 .308 This is exactly

AR 10 .308
This is exactly what I was looking for. Thanks for sharing this great article! That is very interesting Smile I love reading and I am always searching for informative information like this!


latest designer sarees

Well, this is my first visit to your blog! I am very impressed by the way you share awesome and precious content. Thanks for sharing this great blog and keep writing.
latest designer sarees


johnscornor

Excellent blog whenever i feel bored i come here and i get so many new things over your blog thanks for sharing your knowledge able stuff I appreciate your blog. I would love to read more.
http://downsviewparkcondos.ca/


Convenient Apartment - 1 Yonge Condos

This is an extraordinary article it looks so fresh and genuine it does really boost me genius writing i would love to read your new post i am damn sure it would be more interesting.
Development In Harbour street


Best Georgia Car Accident Lawyer

Very interesting article, actually i was looking a website like this; It was nice to land on this website post and to see such superb stuff. I have bookmarked it and I will be back for read new blog articles. Thanks for taking the time to share this.
Attorneykennugent.com


Top Web development company in NJ

I am an experienced web developer in Splendor Design Group and also an intense web reader. Whenever I am getting bored, I would like to reading stuff on web. Thanks for sharing this excellent information and keep posting more.
http://splendorgraphics.com/


Superb job

I would like to thank you for the efforts you have made in writing this article. I am hoping the same best work from you in the future as well. In fact your creative writing abilities has inspired me.
acworth plumber near you


Interesting post

Hope you continue to share more of your ideas.I will definitely love to read. I really be grateful for you work and Thanks for providing us a great Platform for share information’s with others.
http://callmarylandplumber.com/


animal jam

The blog or and best that is extremely useful to keep I can share the ideas of the future as this is really what I was looking for, I am very comfortable and pleased to come here. Thank you very much.
animal jam | five nights at freddy's | hotmail login


NFL Divisional Playoffs 2017

http://cowboysvspackers.co/

http://nflplayoffs2017.org/
http://steelersfootball.net/
http://cowboysgametoday.com/

http://greenbaypackersgame.net/
http://greenbaypackersgame.net/packers-vs-cowboys-divisional-playoffs-2017/

http://nflplayoffs2017.org/
http://nflplayoffs2017.org/nfl-divisional-round-playoffs-2017/


Carpet Cleaning Services

Excellent and very exciting stuff. All the contents you mentioned in post are too good and can be very useful. Surely preferred to the other peoples for reading. Thanks & keep writing.
http://ottawaecopro.com


Where to Live Near the Water - Virginia Beach Housing

It is a fantastic blog its really inspiring and full fill with knowledge i am very active person on your website pleasure to come here regularly looking forward with your next article. It might be enhance my wisdom.
www.757home.com


Tijuana Dentist:The Importance of Dentistry and Dental Care

I really enjoyed reading your stuff. I would gladly suggest this website to someone regard for these type of stuff. This is the best site. Thanks for sharing this valuable information and keep posting more.
goodsamdental.org


Cheap Smog Check Near Me

I wanted to comment & say that I enjoyed reading your posts & they are all very well written out and like to keep on visiting this site once again. I found it very interesting things in this post. I must say a very well written and informative post and really appreciate your effort. Thanks a lot
Smoghub.com


midu

Interesting! I love your post. Thank you very much!
On the Friv 5 Games Moto X3M: Hop on your dirt bike and ride over jumps, do tricks, and try not to fly off! Memorize the course and execute your stunts perfectly.. Play Moto x3m - Show off your speed and clever use of physics on friv4school 2017
Run , jump and ignore certain laws of physics through even more courses in Run 3 on kizi 100.


cool guru..

Gastric Sleeve Mexico | A Better Option for Bariatric Patients

You have a genuine capacity for composing extraordinary Stuff. It is really good. I hope you will continue for sharing such a excellent post. This was truly extremely enlightening site for me. Great Thanks!
samdentaltijuana


Board Exam Result 2017

This is my first time i visit here. I found so many entertaining stuff in your blog Jharkhand 12th Result 2017. Keep up the good work. Can you inform me when the Jharkhand 12th Result 2017 will be released.


Best Dental Clinic in Mexico

I feel it necessary to write a review of this website which I have been a regular visitor of your website for many years. I would gladly recommend this website to someone looking for these kinds of stuff. Thank You!!
http://www.dentalimplantsmex.org


Best Nursing Certifications to Have

I think this blog is one of the best platforms to get updated with the latest information. This is such an extraordinary asset; I have thoroughly enjoyed reading your points and have come to the conclusion that you are right about many of them. You are great.
Nursehub.org


Kate Young

Thanks gentalman yours post is realy informative and helpful too.
Welcome to zoxy 2 brands · about · showroom · news · contact · wholesale ·
FRIV GAMES - Play free friv 2 online games flash at Slitherio online. This is the best solution for having fun. Now play and feel!


Incredible posting this is

Incredible posting this is from you. I am really and truly thrilled to read this marvelous post. You've really impressed me today. I hope you'll continue to do so!
religious story in hindi


great work

I read your whole article, it's really interesting and helpful thanks for sharing. I want to sport your blog by just share little view. Really great work.
pnr status


Understanding

Understanding Nutrition
That's what makes you the best. You should keep publishing more articles and you will Such Become One of the best writers ever


wow, great, I was wondering

wow, great, I was wondering how to cure acne naturally. and found your site by google, learned a lot, now i’m a bit clear. I’ve bookmark your site and also add rss. keep us updated.
voyance gratuite telephone


Data Analysis Consulting

I am first time here; this is very nice and gives in depth information. I will definitely come back to more times in this year for submit more post. Keep trying to make best. Thanks for approving.
http://www.infoplicity.com


Reallt

am first time here; this is very nice and gives in depth information. I will definitely come back to more times in this year for submit more post
[url=http://encgmail.com/]www.gmail.com, gmail.com[/url]


ggg

am first time here; this is very nice and gives in depth information. I will definitely come back to more times in this year for submit more post

www.gmail.com


Post new comment

More information about formatting options