1

I'm trying to get a list of nodes whose titles contain a given string, in this case the term name of a term attached to another node.

Based on other answers I've found online, I've thought this could work:

$sql = 'SELECT title FROM {node} WHERE title LIKE :title';
$result = db_query($sql, array(':title' => '%' . db_like($term->name)));

That's giving me a response but it's all empty, which makes no sense.

Here's what the response looks like:

DatabaseStatementBase Object
(
[dbh] => DatabaseConnection_mysql Object
    (
        [needsCleanup:protected] => 
        [target:protected] => default
        [key:protected] => default
        [logger:protected] => 
        [transactionLayers:protected] => Array
            (
            )

        [driverClasses:protected] => Array
            (
                [SelectQuery] => SelectQuery
                [MergeQuery] => MergeQuery
                [UpdateQuery] => UpdateQuery
                [DatabaseSchema] => DatabaseSchema_mysql
            )

        [statementClass:protected] => DatabaseStatementBase
        [transactionSupport:protected] => 1
        [transactionalDDLSupport:protected] => 
        [temporaryNameIndex:protected] => 0
        [connectionOptions:protected] => Array
            (
                STUFF
                    (
                        [default] => 
                    )

            )

        [schema:protected] => DatabaseSchema_mysql Object
            (
                [connection:protected] => DatabaseConnection_mysql Object 
*RECURSION*
                [placeholder:protected] => 0
                [defaultSchema:protected] => public
                [uniqueIdentifier:protected] => 5670ef6385b8e5.94304345
            )

        [prefixes:protected] => Array
            (
                [default] => 
            )

        [prefixSearch:protected] => Array
            (
                [0] => {
                [1] => }
            )

        [prefixReplace:protected] => Array
            (
                [0] => 
                [1] => 
            )

    )

[queryString] => SELECT * FROM node WHERE title LIKE :title  
) 

Do I need to do something like this using CONTAINS instead?

$sql = 'SELECT title FROM {node} WHERE CONTAINS(title, :title)';
$result = db_query($sql, array(':title' => '%' . db_like($term->name)));

1 Answer 1

1

You need iterate over the node collection, you can use this code to do so:

$sql = 'SELECT title FROM {node} WHERE title LIKE :title';
$result = db_query($sql, array(':title' => '%' . $term->name . '%'));

foreach($result as $node) {
    //dpm($node);
    //$node is your object
    echo $node->title . '<br>';
}

Edit: If you want only specifics content types use this:

$sql = "SELECT title FROM {node} WHERE title LIKE :title AND type = 'your_content_type'"; 
6
  • Hey, this works! So I was encoding the term incorrectly by leaving off the trailing %? Ugh. That seems pretty obvious now.
    – rexfaraday
    Commented Dec 16, 2015 at 5:55
  • @rexfaraday a pleasure help you, good luck with your project ;-) Commented Dec 16, 2015 at 5:57
  • If I wanted to filter my results to just a specific content type, would I add a condition to the first line of the snippet?
    – rexfaraday
    Commented Dec 16, 2015 at 5:57
  • @rexfaraday I edit my answer, see it Commented Dec 16, 2015 at 6:00
  • Are the ' 's in the right place in the edit?
    – rexfaraday
    Commented Dec 16, 2015 at 6:08

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.