Computed code db query issue -- working in PHPmyadmin but not field On my site, I have venues, and reviews of the venues, related with an entity reference field. I'm creating a computed field to store the UID of the first person to create a review for each venue. To do so I've written the below query. Now, it works fine when I enter the simplified sql directly into PHPmyadmin, and substitute $entity->nid with an actual nid, but for some reason it just will not work when I add it into the computed code field, with the added db_query and fetch portions. Any ideas?

if (!$entity-&gt;nid) node_save($entity);

$entity_field[0]['value'] = 
db_query (
    SELECT entity_id
    FROM (
        db_query (
            SELECT * 
            FROM field_data_field_review_venue_reference AS ReviewRow
            WHERE field_review_venue_reference_target_id = $entity-&gt;nid
        )-&gt;fetchrow()
    ) AS uid
    LIMIT 1
)-&gt;fetchfield();
What you wrote is not PHP code, which is what a computed field is expecting.
I would rather use the following code.

$entity_field[0]['value'] = db_query("SELECT entity_id FROM {field_data_field_review_venue_reference} vr WHERE vr.field_review_venue_reference_target_id = :id", array(':id' =&gt; $entity-&gt;nid))
  -&gt;fetchField();