Created
May 25, 2011 20:11
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'); |
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);
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.
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
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();
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
You could use this method for calling sprintf() as well.