1 dbtng_example.module dbtng_example_entry_load($entry = array())

Read from the database using a filter array.

db_query() used in an SQL query with placeholders and arguments as parameters.

 // Old way
 $query = "SELECT * FROM {dbtng_example} n WHERE n.uid = %d AND name = '%s'";
 $result = db_query($query, $uid, $name);

DBTNG provides an abstracted interface that will work with a wide variety of database engines.

db_query() is deprecated except when doing a static query. The following is perfectly acceptable in Backdrop.

  // SELECT * FROM {dbtng_example} WHERE uid = 0 AND name = 'John'
    "SELECT * FROM {dbtng_example} WHERE uid = :uid and name = :name",
    array(':uid' => 0, ':name' => 'John')

But for more dynamic queries, Backdrop provides the db_select() API method, so there are several ways to perform the same SQL query. See the handbook page on dynamic queries.

  // SELECT * FROM {dbtng_example} WHERE uid = 0 AND name = 'John'
    ->condition('uid', 0)
    ->condition('name', 'John')

Here is db_select with named placeholders:

  // SELECT * FROM {dbtng_example} WHERE uid = 0 AND name = 'John'
  $arguments = array(':name' => 'John', ':uid' => 0);
    ->where('uid = :uid AND name = :name', $arguments)

Conditions are stacked and evaluated as AND and OR depending on the type of query. For more information, read the conditional queries handbook page at: http://DOCUMENTATION_PENDING/node/310086

The condition argument is an 'equal' evaluation by default, but this can be altered:

  // SELECT * FROM {dbtng_example} WHERE age > 18
    ->condition('age', 18, '>')


array $entry: An array containing all the fields used to search the entries in the table.

Return value

object: An object containing the loaded entries if found.

See also





Related topics


modules/examples/dbtng_example/dbtng_example.module, line 240
This is an example outlining how a module can make use of the DBTNG database API in Backdrop.


function dbtng_example_entry_load($entry = array()) {
  // Read all fields from the dbtng_example table.
  $select = db_select('dbtng_example', 'example');

  // Add each field and value as a condition to this query.
  foreach ($entry as $field => $value) {
    $select->condition($field, $value);
  // Return the result in object format.
  return $select->execute()->fetchAll();