Skip to content

Instantly share code, notes, and snippets.

@gavinblair
Created May 25, 2011 20:11
Show Gist options
  • Save gavinblair/991817 to your computer and use it in GitHub Desktop.
Save gavinblair/991817 to your computer and use it in GitHub Desktop.
Drupal 7 allows you to pass an array as the second parameter in db_query(). However, in Drupal 6 you need to pass each value in as a separate parameter. Here's how to use call_user_func_array() to call db_query with an unknown number of fields and values
<?php
//$values is an associative array where the keys are field names and values are the values we want in the database
//create a string with enough %s's for fields and values. Wrap the values in single quotes.
$percents_fields = "";
$percents_values = "";
foreach($values as $field => $value) {
$percents_fields = "%s, ";
$percents_values = "'%s', ";
}
//trim off the last comma-space
$percents_fields = rtrim($percents_fields, ", ");
$percents_values = rtrim($percents_values, ", ");
//here's our SQL
$sql = "INSERT INTO table ($percents_fields) VALUES ($percents_values);";
//create an array of parameters we'll send to db_query
$params = array();
//add the SQL
$params[] = $sql;
//add the fields and values
$params = array_merge($params, array_keys($values), $values);
//magic!
call_user_func_array('db_query', $params);
//get the ID of the new row
$new_id = db_last_insert_id('table', 'id');
@gavinblair
Copy link
Author

You could use this method for calling sprintf() as well.

@SeanJA
Copy link

SeanJA commented May 26, 2011

There isn't really a need to call it using the call_user_func_array function is there?

$values = array_merge(array_keys($values), $values);
db_query($sql, $values);

would work just fine according to the code:

<?php
    function db_query($query) {
      $args = func_get_args();
      //take the query out of the args retrieved
      array_shift($args);
      //do the db_prefix magix
      $query = db_prefix_tables($query);
      //if the arg that remains is an array, use it as the args
      if (isset($args[0]) and is_array($args[0])) { // 'All arguments in one array' syntax
          $args = $args[0];
      }
      //... do the rest of the magix
    }

Alternatively you could just use drupal_write_record:

drupal_write_record('table', $values);

@gavinblair
Copy link
Author

Doh. Someone said they tried it with an array, and it didn't work, so I didn't even try it. Thanks for the drupal_write_record() tip.

@SeanJA
Copy link

SeanJA commented May 26, 2011

They probably did it as an array of key value pairs and wondered why it didn't work. Your way is quite innovative though :P

@SeanJA
Copy link

SeanJA commented May 26, 2011

This is why drupal needs a query builder... that way you don't need to worry about building dynamic queries:

if($case_1){
  $query->where('something', $case_1);
}
foreach($input as $k=>$v){
  $query->field($k, $v);
}
$result = $query->insert();

@gavinblair
Copy link
Author

Usually I'm against query builders, but in this case it would have been extremely handy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment