function db_placeholders

You are here

6 database.inc db_placeholders($arguments, $type = 'int')

Generate placeholders for an array of query arguments of a single type.

Given a Schema API field type, return correct %-placeholders to embed in a query

Parameters

$arguments: An array with at least one element.

$type: The Schema API type of a field (e.g. 'int', 'text', or 'varchar').

Related topics

9 calls to db_placeholders()
block_list in modules/block/block.module
Return all blocks in the specified region for the current user.
block_user in modules/block/block.module
Implementation of hook_user().
blogapi_mt_validate_terms in modules/blogapi/blogapi.module
Blogging API helper - find allowed taxonomy terms for a node type.
locale_batch_by_language in includes/locale.inc
Prepare a batch to import translations for all enabled modules in a given language.
system_theme_data in modules/system/system.module
Collect data about all currently available themes.

... See full list

File

includes/database.inc, line 251
Wrapper for database interface code.

Code

function db_placeholders($arguments, $type = 'int') {
  $placeholder = db_type_placeholder($type);
  return implode(',', array_fill(0, count($arguments), $placeholder));
}

Comments

Example usage:

<?php
  $data
= array(1, 2, 3);

 

db_query('SELECT client_id
     FROM {pifr_client}
     WHERE client_id IN ('
. db_placeholders($data, 'int') . ')
     ORDER BY client_id'
, $data
 
);
?>

<?php
  $workflow_published_states
= array('4','5');
 
$workflow_internal_review_states = array('6','7','8');

 

$result = db_query('SELECT * FROM {workflow_transitions} wt WHERE
    wt.sid IN ('
. db_placeholders($workflow_internal_review_states, 'int') .') AND
    wt.target_sid IN ('
. db_placeholders($workflow_published_states, 'int') .') ',
   
array_merge($workflow_published_states, $workflow_internal_review_states)
  );
?>

Just thought to point out that the above technique works also when mixing arrays and non-array parameters in a single query.
Non-array parameters need typecasting, though.

<?php
db_query
("UPDATE {users} AS u, {users_roles} AS r SET u.status = 0 " .
        
"WHERE u.uid = r.uid AND r.rid = %d AND name NOT IN (" . db_placeholders($names, 'varchar') . ")",
        
array_merge( (array) variable_get('my_rid', FALSE), $names)));
?>

I needed placeholders for e multiple LIKE-statement, so I wrote a modified version of the db_placeholders function:

<?php
 
/**
   * Generate placeholders for an array of query arguments, to be used in multiple LIKE command
   *
   * @version: 1.0
   * @date: 2013-08-29
   * @author: Radon8472
   *
   * @param:  string    $arguments          An array with at least one element.
   * @param:  string    $col                The database column where the LIKE search should performed
   * @param: [string    $like_pattern]      optional search pattern for LIKE,  with a drupal like %-placeholder
   * @param: [string    $like_pattern]      optional operator between the multiple LIKE statements (default is "OR")
   *
   * @return string:    correct %-placeholders to embeded in a query
   */
 
function db_like_placeholders($arguments, $col, $like_pattern = "%s", $operator = "OR")
  {
    return
implode(" ".$operator." ",array_fill(0, count($arguments),$col." LIKE '".$like_pattern."'"));
  }
?>

Usage:

<?php
<?php
  $titles
= array("New Entry", "%Example%Title");
 
db_query("SELECT *"
        
. "  FROM {node}"
        
. "   WHERE ".db_like_placeholders($titles,"title","%s")
      ,
$names);
?>

?>

This code would select all nodes with the title "New Entry" or where the title includes the words "Example" followed by "Title".

I hope this will be usefull for someone.