db_insert

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

Returns a new InsertQuery object for the active database.

Parameters

$table: The table into which to insert.

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

Return value

InsertQuery A new InsertQuery object for this connection.

Related topics

137 functions call db_insert()

File

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

Code

<?php
function db_insert($table, array $options = array()) {
  if (empty($options['target']) || $options['target'] == 'slave') {
    $options['target'] = 'default';
  }
  return Database::getConnection($options['target'])->insert($table, $options);
}
?>

Comments

Example #1

Simple Example taken from http://drupal.org/node/310079 Check it out for more information!

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

documentation of db_insert()

Login or register to post comments