1 database_example.module database_example_entry_load($entry = array())

Reads from the database using a filter array.

Backdrop 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 code is perfectly acceptable in Backdrop.

  // SELECT * FROM {database_example} WHERE uid = 0 AND name = 'John'
  db_query("SELECT * FROM {database_example} WHERE uid = :uid and name = :name",
    array(':uid' => 0, ':name' => 'John')
  )->execute();

For more dynamic queries, Backdrop provides db_select().

  // SELECT * FROM {database_example} WHERE uid = 0 AND name = 'John'
  db_select('database_example')
    ->fields('database_example')
    ->condition('uid', 0)
    ->condition('name', 'John')
    ->execute();

With named placeholders, the code becomes the following

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

Conditions are stacked and evaluated as AND or OR depending on the type of query. The condition argument is an 'equal' evaluation by default, but this can be altered, like in the following snippet.

  // SELECT * FROM {database_example} WHERE age > 18
  db_select('database_example')
    ->fields('database_example')
    ->condition('age', 18, '>')
    ->execute();

Parameters

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

Return value

array: An array containing the loaded entries, if found.

See also

db_select()

db_query()

Related topics

File

modules/examples/database_example/database_example.module, line 212
Hook implementations for the Database Example module.

Code

function database_example_entry_load($entry = array()) {
  // Read all fields from the database_example table.
  $select = db_select('database_example', 'example');
  $select->fields('example');

  // Add each field and value as a condition to this query.
  foreach ($entry as $field => $value) {
    $select->condition($field, $value);
  }

  return $select->execute()->fetchAll();
}