Skip to main content
edited tags; edited title
Link
Adrian Cid Almaguer
  • 18.5k
  • 14
  • 79
  • 133

Get How to get Node IDs of nodes whose titles contain a certain string?

Source Link

Get Node IDs of nodes whose titles contain a certain string

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)));