You are here

function boost_db_multi_insert in Boost 6

Insert many records into the database.

NOTE Be aware of the MySQL's max_packet_size variable.

Parameters

$table: The name of the table.

$fields array: key: field name value: db_query placeholders; like %d or '%s'

$values: array of values you wish to be inserted. If you have 3 fields then the array should be structured like array($field_1_value_A, $field_2_value_A, $field_3_value_A, $field_1_value_B, $field_2_value_B, $field_3_value_B);

$suppress: bool. TRUE to suppress db_query errors

Return value

returns db_query() result.

1 call to boost_db_multi_insert()
boost_crawler_add_alias_to_table in ./boost.module
Get URLs from url alias table

File

./boost.module, line 5491
Provides static file caching for Drupal text output. Pages, Feeds, ect...

Code

function boost_db_multi_insert($table, $fields, $data, $suppress = FALSE) {
  if (BOOST_VERBOSE >= 1 && count($data) % count($fields) != 0) {
    watchdog('boost_db_multi_insert', 'Number of fields in the fields array do not match the number of fields in the data array', array(), WATCHDOG_ERROR);
    return FALSE;
  }

  // Build the fields part of this query
  $field_names = implode(', ', array_keys($fields));

  // Get the number of rows that will be inserted
  $rows = count($data) / count($fields);

  // Build the values placeholders string.
  $values = '(' . implode(', ', $fields) . ')';
  $placeholders = $values;

  // Add the rest of the placeholders
  for ($i = 1; $i < $rows; $i++) {
    $placeholders .= ', ' . $values;
  }

  // Glue query together
  $query = "INSERT INTO {" . $table . "} ({$field_names}) VALUES {$placeholders}";

  // Run the query
  if ($suppress) {
    return @db_query($query, $data);
  }
  else {
    return db_query($query, $data);
  }
}