I want to check if user exists based on some profile2 field. I have tried the following. Works fine. I want to know if it is proper or not. If it is not proper, is there any other way around?  function checkUserExistsByProfile($company_id){          $query = 'SELECT field_somefield_value         FROM {field_data_field_somefield} AS fn         WHERE fn.field_somefield_value  =:rids';         $result = db_query($query, array(':rids' =&gt; array($company_id)));          $field_value = $result-&gt;fetchCol();         return count($field_value);      }   Please suggest.  UPDATE:  I have tried solutions suggested by  Mołot, Bulat as follows.  Profile Field name is field_ngouniqueid  $query = new EntityFieldQuery(); $query-&gt;entityCondition('entity_type', 'user') -&gt;fieldCondition('field_ngouniqueid', 'value', (int)$company_id, '='); $results = $query-&gt;execute();  $users = user_load_multiple(array_keys($results['user']));   But users is an empty array always. But it has a record with value $company_id  Solution: it should be $query->entityCondition('entity_type', 'profile2')
121478
I have a field within the user's profile set with their payroll. What I'm trying to achieve within my module is find the uid, based on the value of a payroll field.  I've tried a few things, but I can't get it to work. What I'm currently trying is this, but it returns unknown field of user  $query = new EntityFieldQuery();  $query-&gt;entityCondition('entity_type', 'user')   -&gt;fieldCondition('user', 'field_user_payroll', $payroll, '=');  $result = $query-&gt;execute(); $account = user_load($result);  $wrapper = entity_metadata_wrapper('user', $account); $mimemail = $wrapper-&gt;mimemail-&gt;raw();  watchdog('Photo Import', "Mimemail: %mimemail",    array('%mimemail' =&gt; $mimemail), WATCHDOG_INFO);   Any help would be really appreciated. It's driving me crazy!
33318
I'm performing in Drupal 8 an entity query that searches for nodes that have not been changed since x days, where x is a numeric node field (node.field_days). So I would like to perform a query like:  $query = \Drupal::entityQuery("node")   -&gt;condition('changed', strtotime('-node.field_days days'), '&lt;=');   I know that condition takes as value a string and not a db column, but is it possible to use entity query with a dynamic value?
42749
If i run below query it returns all records of that object:  Select ID, name from Contact where Name like '%%%'   This should return only those records which have % in the name. For example if i run below query it will return only those records which have test in the name.  Select ID, name from Contact where Name like '%test%'   Any idea, What is going on here?
26567
How can I execute a query that connects to different databases?  I have the following query, and I need to rewrite it for Drupal.  SELECT a.empid, b.empname FROM db1..empidmaster a, db2..emplist b WHERE a.empid = b.empid   How can I rewrite the query using db_query(), or db_select()?
12526
Would you recommend using a  or a  field, and why (using MySQL)?   I'm working with PHP on the server side.
45100
I have a simple mysql table which I use as a server booking system. A user can log in to a server, if he has a valid booking for that server. A sample data might look like this:  +----------+----------------+---------------------+---------------------+ | server   | user           | start               | end                 | +----------+----------------+---------------------+---------------------+ | server01 | Alice,Bob      | 2013-10-28 00:00:00 | 2013-12-31 00:00:00 | | server02 | Carlos         | 2013-10-28 00:00:00 | 2013-12-31 00:00:00 | | server03 | Dan,Erin,Frank | 2013-10-28 00:00:00 | 2013-12-31 00:00:00 | +----------+----------------+---------------------+---------------------+   To check whether user Carlos has a booking for server02, I use the following SQL query:  SELECT 0, id FROM booking WHERE user = 'Carlos' AND (( start &lt; now() and end &gt; now() ) AND server = 'server02') ;   But this query does not work if there are multiple (comma-separated) users. How can I check if Alice has booking for server01 ?
74508
Is it possible to select all of the entities who's xyz field is empty?  I tried soemthing like this:  -&gt;fieldCondition('field_name', 'value', NULL, 'IS NOT NULL');   However, this doesn't seem to work.  Any ideas?
96093
We are submitting our application for Security review so we have to check FLS/CURD so we have a query like below.  Account ac  = [SELECT id, Name From Account limit 1];   so do we need to check isAccessible() for Id field here? I know we have to check for Name field but not sure do we really need to check same for Id field?
24654
Is there any performance difference between these two queries?  select name from Account where id = :myId  select name from Account where id = :myId limit 1   Or, I guess more general: does salesforce stop searching after first hit when there's a unique value as a condition?
18957
I have read this post  about how to use field_attach_update to save nodes without the overhead of node_save().  I would like to use this for users (which it should work for, as per ) . However, I'm not sure how to apply the examples to users.  Currently, this is my code:  if (isset($uid)) {     $user = user_load($uid);     $user_og_groups = $user-&gt;og_user_node[LANGUAGE_NONE];     $user_committee_positions = $user-&gt;field_committee_position_serno[LANGUAGE_NONE];     $user_data = my_module_import_user($cust_info, $cust_attributes, $drupal_countries, $employers);     $user = user_save($user, $user_data);   }   The $user_data variable is an array of data, which is what user_save expects (). user_save() doesn't work exactly the same as node_save() and doesn't accept a user object.  How then can I use field_attach_update to programmatically update users?
42692
I created an entity via Entity Construction Kit, Initially I wanted to use rules to create the new entity but the entity is fielded and the fields don't get exposed in rules (as of now, someone is trying to work on a patch for that), but in the interm I wanted to write a simple function that creates a entity, which I assumed would be somewhat like creating a node programmatically, but I've been at this for like 4 hours and I haven't gained an inch, I have entity_create and entity_save but they don't do much and I can't find any documentation on it.  What I'd like to see is how to create an example entity from a bundle made with eck, or through code, that is fielded.  Thanks so much  Edit: The last thing I need to figure out is how to specify the name of the fields that are attached to the entity so I can enter values for those as well.  $values = array('uid' =&gt; 1, 'field_receipt_prod_ref' =&gt; 1, 'type' =&gt; 'receipt');         $new_ent = entity_create('receipt', $values)-&gt;save();
65216
I am dissecting some code and came across this,   $sql = 'SELECT page.*, author.name AS author, updator.name AS updator '      . 'FROM '.TABLE_PREFIX.'page AS page '      . 'LEFT JOIN '.TABLE_PREFIX.'user AS author ON author.id = page.created_by_id '      . 'LEFT JOIN '.TABLE_PREFIX.'user AS updator ON updator.id = page.updated_by_id '      . 'WHERE slug = ? AND parent_id = ? AND (status_id='.Page::STATUS_REVIEWED.' OR status_id='.Page::STATUS_PUBLISHED.' OR status_id='.Page::STATUS_HIDDEN.')';   I am wondering what the "?" does in the WHERE statement.  Is it some sort of parameter holder?
91310
I'm currently working on a plugin, and I essentially want to check if a certain page has been created.  Essentially, I want to do something like this:      if (file_exists($file)) {     $exists = true; } else {     $exists = false; }   But I just want to do it so that it checks, instead, for a post_name value. (If I was further able to ensure that the post was a page and not just a post with that title, that would be great too.)  How do I do this?  I had been getting the script to grab the headers from the url of the post and if it returned a 404, to return $exists = false, but I've pinpointed a memory leak problem with that and I don't think it's very efficient.
34791
This is the announcement post of the Stack Exchange Data Explorer. Its answers were meant to iron out the initial glitches 6 to 8 years ago.  If you have found a bug or need support  today with any of SEDEs features, please ask a new question and tag it with   What does it do? Allows you to run arbitrary SQL queries on the data dumps, and share those queries with your friends. Technical details It is hosted on the Stack Exchange infrastructure , the database is hosted on the same SQL Server version all other sites are hosted on. You can use most of the TSQL commands, even create temporary objects (but not User Defined Functions). The controls used are jQuery based, the grid is  . All query results are cached, so sharing a link to a query will not result in the server going in to a tailspin. The query edit control is a slightly modified  control, I also am considering bespin cause the inter iframe communication is nasty complicated. It gives up nice syntax highlighting for queries. Parameters There is support for parameterized queries, to get it to work name your params like ##this##. For example have a . Query name and description A query can have a name and description, to name it lead with a comment, to describe it continue commenting, for example: -- This is my query name  -- This is my description -- I can span multiple lines  SELECT 1  Anonymous users can initially name a query, but can not change the names of any existing queries. Tutorial There's a  which helps you getting started in writing your own SQL queries. Logging in You can use your openid provider to log in, at the moment once logged in we will track the list of queries you execute (giving you basic history support) Also if there are any params named UserId they will be populated automatically if your EmailHash matches the one on the site being queried. Magic columns If you alias an id column with as [Post Link] it will automatically create a link in the result set to the parent site. Similarly if you alias an id column with as [User Link] it will display a link to the user page. select top 10 * from Posts will show you how tags are done. (Magic columns for images is planned) example (most controversial posts:  ) So what are you waiting for, check it out at  Any feedback would be greatly appreciated. I hope to have the login section working fairly soon and allow voting on the best queries and commenting. Featured Queries At the moment, admins have permission to feature interesting queries on this page , I am looking for better ways to manage the huge query list that is building up.
82370
I need to select entities with specific dates.   How should the following be formatted. Do I need to parse 15-May-2010  Also how I get the error page.   function events2() {  $query = new EntityFieldQuery(); $query   -&gt;entityCondition('entity_type', 'node', '=')   -&gt;propertyCondition('status', 1, '=')   -&gt;propertyCondition('type', 'event')     -&gt;propertyCondition('field_event_date', '15-May-2010', '=');  $result = $query-&gt;execute();   return $result;  }
15927
I used this code to get a pager.     if (isset($_GET['tid'])) {      $id=($_GET['tid']);      $query = new EntityFieldQuery();      $query-&gt;entityCondition('entity_type', 'node');      $query-&gt;entityCondition('bundle', 'escorts_product');      $query-&gt;pager(2,1);      $query-&gt;propertyCondition('status', 1);      $query-&gt;fieldCondition('field_product_sub_cat', 'tid', $id,'=');      $result = $query-&gt;execute();     //print "&lt;pre&gt;";print_r($result);     }   When I used print "&lt;pre&gt;";print_r($result), it gave me 2 results on y page, whereas I added more than 2; this is right.   How should I get links for next page and also previous, next? Is there any other way to get a pager?
9564
I am looking to return a list of users from a database. There are hundreds of users in the db. Each user has it's own unique user_id. All of the values that I am trying to return reside in the same column (meta_value).   The database structure is as follows:  id | user_id | meta_key | meta_value   sample data is as follows:   1 | 3434 | first_name | Brandon 2 | 3434 | last_name  | Johnson 3 | 3434 | street_add | 123 main 4 | 3434 | city       | ocean beach 5 | 3434 | state      | Texas   I am trying to return the first name, last name, street_add, city, and state in one sql statement.  I'd like the output to look like:  Brandon, Johnson, 123 Main, Ocean Beach, Texas