1.select
<?php
function db_select($table, $alias = NULL, array $options = array()) {
if (empty($options['target'])) {
$options['target'] = 'default';
}
return Database::getConnection($options['target'])->select($table, $alias, $options);
}
?>
2.insert
<?php
// For the following query:
// INSERT INTO {node} (title, uid, created) VALUES ('Example', 1, 1221717405)
$nid = db_insert('node') // Table name no longer needs {}
->fields(array(
'title' => 'Example',
'uid' => 1,
'created' => REQUEST_TIME,
))
->execute();
// Above Example is Equivalent to the Following in D6
$result = db_query("INSERT INTO {node} (title, uid, created) VALUES (%s, %d, %d)", 'Example', 1, time());
// OR using drupal_write_record...
$data = array(
'title' => 'Example',
'uid' => 1,
'created' => REQUEST_TIME,
);
drupal_write_record('node', $data);
?>
3.update
<?php
// For the following query:
// UPDATE {node} SET uid=5, status=1 WHERE created >= 1221717405
$num_updated = db_update('node') // Table name no longer needs {}
->fields(array(
'uid' => 5,
'status' => 1,
))
->condition('created', REQUEST_TIME - 3600, '>=')
->execute();
// Above Example is Equivalent to the Following in D6
$result = db_query("UPDATE {node} SET uid = %d, status = %d WHERE created >= %d", 5, 1, time() - 3600);
?>
4.delete
<?php
// Drupal 7
$nid = 5;
$num_deleted = db_delete('node')
->condition('nid', $nid)
->execute();
// Above example is equivalent to the following in Drupal 6
$nid = 5;
db_query("DELETE FROM {node} WHERE nid = %d", $nid);
?>