How do I query a MySQL view in Drupal 7? 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?
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:

CREATE VIEW test AS SELECT * FROM variable;


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' =&gt; $name));
$value = $result-&gt;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().