db_update

7 database.inc db_update($table, array $options = array())
8 database.inc db_update($table, array $options = array())

Returns a new UpdateQuery object for the active database.

Parameters

$table: The table to update.

$options: An array of options to control how the query operates.

Return value

UpdateQuery A new UpdateQuery object for this connection.

Related topics

174 calls to db_update()

File

includes/database/database.inc, line 2427
Core systems for the database layer.

Code

function db_update($table, array $options = array()) {
  if (empty($options['target']) || $options['target'] == 'slave') {
    $options['target'] = 'default';
  }
  return Database::getConnection($options['target'])->update($table, $options);
}

Comments

Example #1

A simple example of this function borrowed from http://drupal.org/node/310080 Go there for more information!

<?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);

?>

Increment column

here is an example of how to increment a column:

<?php
db_update
('system')
    ->
expression('weight', 'weight + :weight', array(':weight' => 1))
    ->
condition('name', 'mymodule')
    ->
condition('type', 'module')
    ->
execute();
?>

You can't use ::fields() as PDO takes the value as literal.

Login or register to post comments