I'm getting uid by $account->uid on rules when I wrote a query by using this id it returns 0 rows. Below is the code that i'm writing on rules

$sql="select * from profile where uid=:uid";
$args = array(':uid' => $uid);
$row = db_query($sql, $args)->rowCount();
echo $row;

the output is 0. uid is there on both tables please help me why this is happening.

  • Which version of Drupal are you using, and are you sure that there is table named 'profile'? – Елин Й. Jul 12 '14 at 8:18

The documentation for DatabaseStatementInterface::rowCount states:

Returns the number of rows affected by the last SQL statement.

Return value

The number of rows affected by the last DELETE, INSERT, or UPDATE statement executed.

Notably, "SELECT" isn't included in there, so may not be reliable to use with selections.

Assuming $account is a valid user object, you may find you have a query result if you try something like:

// Get query result using db_select
$result = db_select('profile', 'p')
            ->condition('uid', $account->uid)

It might just be the missing curly braces around the table name (profile versus {profile}).

Lastly, you might also be interested in profile2_load_by_user($account, 'main').

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.