0

So, I am new to drupal, and I am using a small pdo library to directly fetch from database list of articles, to display them on a page that is not running drupal. But, the articles are posted using drupal cms.

My problem is that, also I am able to fetch the article content, node id ... but I can't seem to find the title of article itself.

I looked in my table, and these are all the data I have.

mysql> describe field_data_body;
+--------------+------------------+------+-----+---------+-------+
| Field        | Type             | Null | Key | Default | Extra |
+--------------+------------------+------+-----+---------+-------+
| entity_type  | varchar(128)     | NO   | PRI |         |       |
| bundle       | varchar(128)     | NO   | MUL |         |       |
| deleted      | tinyint(4)       | NO   | PRI | 0       |       |
| entity_id    | int(10) unsigned | NO   | PRI | NULL    |       |
| revision_id  | int(10) unsigned | YES  | MUL | NULL    |       |
| language     | varchar(32)      | NO   | PRI |         |       |
| delta        | int(10) unsigned | NO   | PRI | NULL    |       |
| body_value   | longtext         | YES  |     | NULL    |       |
| body_summary | longtext         | YES  |     | NULL    |       |
| body_format  | varchar(255)     | YES  | MUL | NULL    |       |
+--------------+------------------+------+-----+---------+-------+
10 rows in set (0.00 sec)

Just as example, I am doing

$db = new DB(...);
$stmt = $db->query("SELECT *, entity_id as id, body_value as content from field_data_body where entity_id < 10  LIMIT 8"); 
$result = $stmt->fetchAll(); 

foreach($result as $row){
  echo $row['title']; //error. 
}
1

As with everything there are a few ways to do this. Here are a couple, if you know the node id.

Do a query on the node table:
select title from node where nid = KNOWN_NODE_ID

Use a Drupal function:
$node = node_load(KNOWN_NODE_ID);
$nodeTitle = $node->title;

| improve this answer | |
  • thanks, but I believe all articles in drupal are stored in the field_data_body table, and I don't see a row called title or node or nid. If you mean to tell me how a select statement is done in mysql then, that is not what I meant. My question was specific to drupal and fetching article from a table. – samayo Apr 9 '15 at 23:26
  • 1
    Drupal uses more than one table for its content objects, or nodes. field_data_body (& field_revision_body for prior revisions) contains data for node body. It's the node table that contains the node title and other metadata related to nodes. – Shawn Conn Apr 9 '15 at 23:56
  • @ShawnConn so that means, if I posted one article on drupal (which i have done) i must look for the title, image, tags, contents on different tables? – samayo Apr 10 '15 at 2:48
  • Yes, if you want to get all the data you need in one set of results look at doing at doing an INNER JOIN query on the node/field_data_body tables. – Shawn Conn Apr 10 '15 at 3:00
  • Is there any example or beginner-friendly way to do this? – samayo Apr 10 '15 at 3:28

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.