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)

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/


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.


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


freeapkdownloader.info I’m

freeapkdownloader.info
I’m happy I located this blog! From time to time, students want to cognitive the keys of productive literary essays composing. Your first-class knowledge about this good post can become a proper basis for such people.


Agency Employment || Myticas Consulting

As a beginner it is a perfect platform your blogs really helps to understand me in a much easier way i regular participate on your website.
Agency Employment


voyance par telephone Great

voyance par telephone
Great info! I recently came across your blog and have been reading along. I thought I would leave my first comment. I don’t know what to say except that I have.


Movers Chicago Illinois

Awesome blog, I really enjoy your article, it’s really worth it. I am Regular user of your website Thanks for taking my post.
Reebieallied


<a href="http://www.reebieallied.com">Local Movers Chicago</a>

Awesome blog, I really enjoy your article, it’s really worth it. I am Regular user of your website Thanks for taking my post.
http://www.reebieallied.com


binaryoptionautotradingreview

binaryoptionautotradingreview.com review
We keep up with the latest techniques of building and have qualified tradesmen to ensure that your job/project is carried out safely. We also make sure that we keep to the highest standards on projects and everyone who works for us is health & safety trained.


Attractions of Sedona || Family Fun in Sedona

Big thumbs for making amazing blog. This is the best site. Actually this was an awesome blog post. You have a very inspiring way of exploring and sharing your thoughts. I enjoyed reading your stuff. Thank you !!
Visit Our Website


tips and tricks We keep up

tips and tricks

We keep up with the latest techniques of building and have qualified tradesmen to ensure that your job/project is carried out safely. We also make sure that we keep to the highest standards on projects and everyone who works for us is health & safety trained.


Utah vs Indiana

http://utahvsindiana.org/
Utah vs Indiana


80g

1. If any, copy of the registration allowed U/s 12a or replicate of cautioning provided U/s 10(23) or 12 (23c).
2. A note on the exercises of the establishment throughout the previous three years.
3. Copy of the examined records for the Past 3 years.
4. Memorandum and Rules & Regulations.
5. Copy of the enrollment Certification from the general public. After tameness of your application.

It is frequently polish that after lodging of the above structure with the Commissioner of Income Tax, the structure is sent by him to your Assessing Officer. After tameness of such evaluation report, a request is accepted by the Commissioner providing the enlistment U/s 80g. In the occasion that, the Income Tax Authority chooses not to permit registration of your Society a notice will certainly be sent out showing the purpose of the division and on receipt of which you can send your remarks or protests.

This registration is routinely thought about a time of 2 to 3 years. It has actually been additional considered that the chief will certainly choose his selections in 6 months of the tameness of use.80g


oil paintings for

oil paintings for sale

Classic Oil Painting Gallery is a well reputed painting art company. We can supply famous oil painting on canvas in museum quality by the best price. Fine art is not far from you. We have professional artists of different painting subjects and art styles to do famous painting reproduction.


Awesome post

Thanks for Nice and Informative Post. game hacker This article is really contains lot more information about This Topic.. game killer Really informative post it is thanks for share such nice information. game hacker apk


fatihin

memerlukan kanstin? Kemi membuat kanstin atau kansteen dengan berbagai konstruksi baja wf jasa konstruksi bangunan


festivals

santa claus images 2016 new year best wishes best new year  images x-mas quotes Merry Christmas is coming very soon and if you want to celebrate this festival with a lot of gifts and surprises then buy the latest merry christmas wishes messages, tree and Santa’s costume from the markets and celebrate it now with your families and friends


January 2017 Calendar new

January 2017 Calendar new year calendar


On that website page, you'll

On that website page, you'll see your description, why not read through this. รายได้พิเศษ


Such A Nice Article

It was really a wonderful article and I was really impressed by reading this blog.. Nice piece of code, I really like that comment field, I think I'd like to implement it on some of my troubleshooting guides also.
sb game hacker
game killer apk
game hacker
game killer
nba 2k16 apk
file extension
true skate mod apk
freedom apk
geometry dash lite apk
gunship battle apk
clash royale mod apk
clash of clans mod apk


nice information

This is wonderful blog about the Drupal. You have written on a very good topic. I really liked this. I am book marking your page for the new interesting blog. Keep share you more blog. Good Work. Keep it up. Packagingboxessupplier.com


nice information

This is excellent information for the searching profile . I just would like to say you have shared very quality stuff in your content. This was quite new information for me. Thanks for sharing this with me. quality die cut corrugated boxes


such an interesting article

such an interesting article here.I was searching for something like that for quite a long time and at last I have found it here.
inz residences ec location


Luxury Escape

Such a very useful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article.

Luxury Escape


Imported Natural Marble

This is a wonderful article, Given so much info in it, These type of articles keeps the users interest in the website, and keeponsharing more...good luck.
Imported Natural Marble


free nba 2k17 locker vc

free nba 2k17 locker vc codes nba2k17

We keep up with the latest techniques of building and have qualified tradesmen to ensure that your job/project is carried out safely. We also make sure that we keep to the highest standards on projects and everyone who works for us is health & safety trained.


moving company in san diego

I am very happy to teach the owner of this day's guestbook, your own reaction and then put my opinion good way to save should I say it is definitely every couple of great additions to your needs.
moving company in san diego


very nice article

startups seeking investors

This is a wonderful product, taking into account all the information about it, this type of product that prevents user interest in the site, and you will share more ... good luck. startups seeking investors


Feel Great

We will always respect my elders.
Numerologist reviewshut Website


orang terkaya Great info! I

orang terkaya
Great info! I recently came across your blog and have been reading along. I thought I would leave my first comment. I don’t know what to say except that I have.


Carpet Cleaning Welcome to

Carpet Cleaning

Welcome to FreshStart Cleaning Service! We are the most reputable carpet cleaning firm in the Daphne, Baldwin, Mobile and Escambia area. We pride ourselves in providing the most thorough carpet cleaning, upholstery cleaning, tile and grout cleaning and water extraction services.


Post new comment

More information about formatting options