0

I'm writing a custom module and trying to use the db_query functions instead of just doing straight MySQL calls.

Here's my query.

$nid = $node->nid;
$result = db_query('SELECT * FROM {field_data_field_room_rates} WHERE field_room_rates_target_id = :nid', array(':nid' => $nid));
drupal_set_message('<pre>'. print_r($result, TRUE) .'</pre>');

...and here's what I get back.

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

            [driverClasses:protected] => Array
                (
                    [SelectQuery] => SelectQuery
                    [DatabaseTransaction] => DatabaseTransaction
                    [InsertQuery] => InsertQuery_mysql
                    [UpdateQuery] => UpdateQuery
                )

            [statementClass:protected] => DatabaseStatementBase
            [transactionSupport:protected] => 1
            [transactionalDDLSupport:protected] => 
            [temporaryNameIndex:protected] => 0
            [connectionOptions:protected] => Array
                (
                    [database] => my_db
                    [username] => user
                    [password] => pw
                    [host] => localhost
                    [port] => 
                    [driver] => mysql
                    [prefix] => Array
                        (
                            [default] => 
                        )

                )

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

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

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

        )

    [queryString] => SELECT * FROM field_data_field_room_rates WHERE field_room_rates_target_id = :nid
)

if I were querying the SQL manually the query would be:

SELECT * FROM field_data_field_room_rates WHERE field_room_rates_target_id =2164

and it would yield back the proper row of items...which this doesn't seem to be giving. All it seems to be giving back is Drupal database info.

What am I doing wrong? :)

1

You need to fecth the results from the $result object. See https://www.drupal.org/node/1251174 for reference.

For instance, you might do something like:

$row = $result->fetch();

Hope it helps,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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