I'm using a global PHP query in a View to find out the count of membership users. The first query, that extracts the total number of users from the database works fine, but when I use the WHERE clause in a query for a comparison with a string, I received a website error like in the image attached below.

This is the query:

$count_users_premium = db_query('SELECT COUNT(license_id) FROM {commerce_license} WHERE state="active"')->fetchField();
print $count_users_premium;

So, if I want to use WHERE some_number=3, it will work, but comparison with a string didn't work. (Drupal 8 version)

enter image description here

1 Answer 1


You cannot directly insert values into queries in Drupal. They need to be escaped. You would want something like this:

$count_users_premium = db_query('SELECT COUNT(license_id) FROM {commerce_license} WHERE state=:active', [':active' => 'active'])->fetchField();
  • You're a hero! Thank you!!! Mar 28, 2020 at 23:55

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.