0

I have written a query which gets the node title according to 'term ID' for a 'portfolio' content type.Now I want to get the 'node title' according to the character that means I want to use 'LIKE' clause on the following query.If user enter "a" or "A" then all the node tile from these characters should be fetch.

<?php
$nodes = array();
$select = db_query("SELECT node.title AS node_title, node.nid AS nid, node.created AS node_created
                    FROM {node} node
                    WHERE node.status = '1'
                         AND node.type = 'portfolio'
                         AND node.nid IN  (SELECT tn.nid AS nid
                                           FROM {taxonomy_index} tn
                                           WHERE tn.tid = '1')
                    ORDER BY node_created");
    foreach ($select as $nodes) {  
       print $nodes->node_title;
   }
?>
apaderno
  • 28,547
  • 16
  • 75
  • 90
fanatic
  • 117
  • 5

1 Answers1

0

The sql should be something like

SELECT node.title AS node_title, node.nid AS nid, node.created AS node_created
    FROM {node} node
        WHERE node.status = '1'
            AND node.type = 'portfolio'
            AND node.nid IN  
            (
                SELECT tn.nid AS nid
                FROM {taxonomy_index} tn
                WHERE tn.tid = '1'
            )
            AND upper(node.title) like '%<whatever>%'
            ORDER BY node_created

You might find more information with the following links
https://drupal.stackexchange.com/questions/112530/how-to-use-like-condition-in-db-select-query
Deals with using the API provided by drupal for writing queries instead of the raw sql like this case

php mysqli prepared statement LIKE
How to use prepared statements with LIKE (if you don't use the functions provided by drupal to do the work)

Community
  • 1
  • 1
geco17
  • 5,152
  • 3
  • 21
  • 38