1

From this question I learned that I have to look in field_data_body to retrieve the body of a node from a database.

field_data_body does not have an nid field though.

What query can I use to retrieve the body from an nid?

To clarify: I simply wanted to quickly fetch a text from a database dump without installing the dump on an actual site. So not looking for PHP here.

7

field_data_body does not have an nid field though.

Because a field can be attached to any entity, which may have a different primary key than nid.

If you really must use direct SQL, you can query the field like this

SELECT body_value FROM field_data_body WHERE entity_type = 'node' AND entity_id = 1

However, this bypasses several of Drupal's layers, so it's normally strongly discouraged to use direct queries.

Instead, it's better to

$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
  ->propertyCondition('status', NODE_PUBLISHED)
  ->propertyCondition('nid', 1)
;
$result = $query->execute();

$nodes = node_load_multiple(array_keys($result['node']));

foreach ($nodes as $node) {
  $bodies = field_get_items('node', $node, 'body');
  foreach ($bodies as $body) {
    var_dump($body['safe_value']);
  }
}

The EFQ version have several advantages

  1. It's safe. You can't (easily) accidentally open injection attacks.
  2. It outputs a safely escaped value, avoiding XSS attacks.
  3. It works if a node has multiple values for the body field.
  4. It works on sites with multiple languages.
|improve this answer|||||
0

I believe it's because field_data_body can be associated with entities that are not nodes, you need to look at the entity_id field as equivalent to the nid field.

If it's a node's body content you're trying to query, then I would run db_query('SELECT body_value FROM `field_data_body` where entity_id = :nid, array(":nid" => $nid));

|improve this answer|||||
-1

In general, all entities are fieldable. Thus, the table field_data_body holds data for all body fields, regardless of being attached to a node or a comment. Hence, the column you are looking for is entity_id (with entity_type = 'node').

On a side node, you shouldn't tamper with the database in such a way, just use

$node = node_load($nid);
$body = node->body['und'][0]['value'];

to get the body (exact code of course depends on your site setup in terms of language, etc.).

|improve this answer|||||
  • please don't recommend ->body['und'][0]['value'] but use field_get_items instead. – Letharion Oct 16 '14 at 15:11

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.