I have created a database view and I would like to show the results in a Drupal Page. The view has some pretty complex SQL behind it, so trying to use the Views module isn't possible.

I just want to do SELECT * FROM VIEW_X and display the results on a page in PHP.

I know how to do this in Drupal 6, but how is it done in Drupal 7?

1 Answer 1


I don't think it's possible to query views using the DBTNG query builder: if you try to use db_select() with a view, it raises a PDOException complaining that the table doesn't exist.

Instead, you'll want to use db_query(). Let's assume you created a view named test:


To get the value of cron_key, you'd do:

$name = 'cron_key';
$result = db_query('SELECT * FROM test t WHERE t.name = :name', array(':name' => $name));
$value = $result->fetchColumn(1);

The comment by drclaw on the db_query() API entry gives a number of examples which should help you out with figuring out how to manipulate a SELECT query using db_query().

  • db_select() works fine with views
    – ya.teck
    Mar 24, 2015 at 13:22

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.