5

Right now i am using this query :

$query_info = db_query("SELECT nid FROM {node} WHERE type=:type  AND language = :language AND status=1",
                array (':language'=> $language->language, ":type" => 'info_pages' , ))->fetchCol();
$info_pages = node_load_multiple($query_info);

As this site is going to have lots and lots of content I am wondering if this is the right way ? If not what would be a better approach ?

2 Answers 2

13
<?php
$query = db_select('node', 'n')
  ->fields('n', array('nid', 'title', 'status'))
  ->condition('type', 'info_page')
  ->addTag('node_access')
  ->execute();
while ($result = $query->fetch()) {
  print_r($result);
}

Query against node table must be a dynamic query with "node_access" tagging to prevent node access system by-passing.

5
  • See drupal.org/node/310075 and drupal.org/node/310077
    – Sivaji
    Commented Oct 9, 2011 at 8:11
  • Thanks for the input Sivaji, i checked both pages but i don't see anything about what you mentioned: preventing node access system by-passing. Can you elaborate please ?
    – silkAdmin
    Commented Oct 9, 2011 at 9:20
  • 1
    The above links meant to help you with writing dynamic queries. For node access features in d7 see api.drupal.org/api/drupal/modules--node--node.module/group/… Ref: "In node listings, the process above is followed except that hook_node_access() is not called on each node for performance reasons and for proper functioning of the pager system. When adding a node listing to your module, be sure to use a dynamic query created by db_select() and add a tag of "node_access". This will allow modules dealing with node access to ensure only nodes to which the user has access.."
    – Sivaji
    Commented Oct 9, 2011 at 12:22
  • If, however, you then need to load fields information, other then the nid, title or status, you will be performing another roundtrip to the database. E.g. a node_load for each found item, will cause at least 21 queries for an uncached set, if your query returns 20 nodes. Possibly a lot more, if you have modules that add their data to nodes on node_load.
    – berkes
    Commented Oct 12, 2011 at 9:02
  • @berkes I would use node_load_multiple() api.drupal.org/api/drupal/modules--node--node.module/function/… for such a case.
    – Sivaji
    Commented Oct 12, 2011 at 18:48
0

If you wish to retrieve nodes of particular content type and want to display somewhere, obviously you can use the powerful module "VIEWS" by using which you can do all the magic.

1
  • 1
    Yes thanks i am aware of what view is, though it s definitely not the most efficient way to perform queries, just a more convenient one
    – silkAdmin
    Commented Oct 9, 2011 at 20:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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