drupal 7 db_query example
Example of db_query in Drupal 7
Here is an example of using the db_query
function in Drupal 7:
$query = db_query("SELECT nid, title FROM {node} WHERE type = :type", array(':type' => 'article'));
foreach ($query as $row) {
// Do something with the results
$nid = $row->nid;
$title = $row->title;
}
In this example, we are using the db_query
function to execute a SELECT query on the node
table. We are retrieving the nid
and title
fields from nodes of type "article". The results are then looped through using a foreach loop, and you can perform further actions with each row of data retrieved.
Please note that the table name is enclosed in curly braces {}
and the query uses placeholders (:type
) to prevent SQL injection. The placeholders are then replaced with the actual values using the array passed as the second argument to db_query
.
I hope this example helps! Let me know if you have any further questions.