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)

digitinc

I think this is an informative post and it is very useful and knowledgeable. therefore, I would like to thank you for the efforts you have made in writing this article. Read more


Many thanks for the exciting

Many thanks for the exciting blog posting! Simply put your blog post to my favorite blog list and will look forward for additional updates. Simply wanted to write down a word in order to say thanks to you for those wonderful tips.
instagram followers


thepagal

I enjoyed reading your articles. This is truly a great read for me. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work! thepagal


My Small Business Branding

Awesome blog. I enjoyed reading your articles. This is truly a great read for me. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work! My Small Business Branding


mp3skull

I really enjoy simply reading all of your weblogs. Simply wanted to inform you that you have people like me who appreciate your work. Definitely a great post I would like to read this mp3skull


ThemeScore I was very

ThemeScore
I was very impressed by this post, this site has always been pleasant news Thank you very much for such an interesting post, and I meet them more often then I visited this site.


Check Cashing

Really is fantastic details in which a lot more folks must notice maintain the nice perform become rear shortly. Thus, today it's also possible to follow-up our own search term Check Cashing Keep writing excellent perform.


digtinc

I agree with you. This post is truly inspiring. I like your post and everything you share with us is current and very informative, I want to bookmark the page so I can return here from you that you have done a fantastic job Dofollow Blog Comments


Dofollow Blog Comments

I agree with you. This post is truly inspiring. I like your post and everything you share with us is current and very informative, I want to bookmark the page so I can return here from you that you have done a fantastic job
http://www.fiverr.com/eunuskhan7/do-blog-comments-backlinks-pr3-to-pr8
Dofollow Blog Comments


Best Video Song

Many thanks for the exciting

Many thanks for the exciting blog posting! Simply put your blog post to my favorite blog list and will look forward for additional updates. Simply wanted to write down a word in order to say thanks to you for those wonderful tips.
mobile massage nj


That's what makes you the

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


Once I thought about things

Once I thought about things like: why such information is for free here? Because when you write a book then at least on selling a book you get a percentage. Thank you and good luck on informing people more about it סדנאות גיבוש בתל אביב


A debt of gratitude is in

A debt of gratitude is in order for the post and extraordinary tips..even I additionally imagine that diligent work is the most vital part of getting achievement.. Sell Your Annuities


I was very impressed by this

I was very impressed by this post, this site has always been pleasant news Thank you very much for such an interesting post, and I meet them more often then I visited this site. יום הולדת לבן 10


Your good knowledge and

Your good knowledge and kindness in playing with all the pieces was very useful. I don’t know what I would have done if I had not encountered such a step like this תכנון גינות


I did enjoyed reading it, I

I did enjoyed reading it, I will be sure to bookmark your blog and definitely will come back from again.. כיסאות מתקפלים


Vagina Senter

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. vagina senter


I must say that overall I am

I must say that overall I am really impressed with this blog. It is easy to see that you are passionate about your writing. If only I had your writing ability I look forward to more updates and will be returning. https://www.youtube.com/watch?v=YQOv7cDE2Vw


You're in point of fact a

You're in point of fact a just right webmaster. The website loading speed is amazing. It kind of feels that you're doing any distinctive trick. Moreover, The contents are masterpiece. you have done a fantastic activity on this subject! http://www.digitaljournal.com/pr/3261204


A very nice and good post

A very nice and good post this. I really like it very much. Keep this quality of your work on articles going on and please do not let the quality of your articles fall to bad. Cheers! https://www.streetinsider.com/SI+Newswire/Regrow+Hair+Protocol+Review%3A+Latest+Report+On+David+Mckennas+Program+Released/12642352.html


I wish to show thanks to you

I wish to show thanks to you just for bailing me out of this particular trouble.As a result of checking through the the net and meeting techniques that were not productive, I was thinking my life was done. employment agency


Convenient Apartment - 1 Yonge Condos

Each of theses locations supply their own unique advantages to living in that community yet general one location protrudes amongst all the rest. North York condos supply a large range of advantages. This short article will review a few of these in detail.Toronto is a city filled with condos. Areas like the downtown monetary and also enjoyment areas, the west end's liberty town and Yonge as well as Eglinton are loaded with new and old condo buildings as well as it seems there are more being built everyday. Condos on 1Yonge Street


digtinc

I agree with you. This post is truly inspiring. I like your post and everything you share with us is current and very informative, I want to bookmark the page so I can return here from you that you have done a fantastic job Dofollow Blog Comments


After reading this I thought

After reading this I thought it was very informative. Thank you for taking the time and effort to put this article together. I find myself once again spending way to much time to both read and comment. But so what it was still worth it! Lockpicking site


WrestleMania

Applaud your work and keep sharing your information.

WrestleMania 33
WrestleMania 2017


red ball

The share your really gives us excitement. Thanks for your sharing. If you feel tired at work or study try to participate in our games to bring the most exciting feeling. Thank you!
hotmail sign in | red ball game | 192.168.1.1


Cool

A good informative post that you have shared and thankful your work for sharing the information. I appreciate your efforts and all the best. Got some appealing information and would like to give it a try. Applaud your work and keep sharing your information.

WrestleMania 33
WrestleMania 2017
WrestleMania 33 Updates
WrestleMania 2017 Updates
WrestleMania 2017 Live
WrestleMania 33 Live


Good Post

Applaud your work and keep sharing your information.

WrestleMania 33
WrestleMania 2017


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. closet doors culver city


I don’t have the time at

I don’t have the time at the moment to fully read your site but I have bookmarked it and also add your RSS feeds. I will be back in a day or two. thanks for a great site. office partitions


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

Post new comment

More information about formatting options