Drupal: Populating CCK Fields via SQL

<<RAW>>

Querying a Drupal database requires knowledge of SQL and Drupal’s Database Abstraction Layer.  Once you have the result from the query, how you manipulate this returned object depends on how you want to utilize that data.  For example:

  1. Adding a default value to a text field in a CCK content type
  2. Adding values to a select field (a.k.a “drop down menu”) in a CCK content type

Steps

  1. Define the SQL Query and store it in a variable
  2. Call db_query with the SQL Query variable
  3. Manipulating the Returned Object
    1. If you only need one field, then call db_result()
    2. Else, call db_fetch_object() or db_fetch_array()

Articles

  1. Database Abstraction Layer
  2. Making a database enquiry using db_query

Drupal API

  1. db_query
  2. db_fetch_object
  3. node_get_types
  4. db_result()

Leave a Reply