db_placeholders

Versions
6
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

▾ 8 functions call 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.
taxonomy_select_nodes in modules/taxonomy/taxonomy.module
Finds all nodes that match selected taxonomy conditions.
taxonomy_term_page in modules/taxonomy/taxonomy.pages.inc
Menu callback; displays all nodes associated with a term.
user_access in modules/user/user.module
Determine whether the user has a given privilege.
_menu_navigation_links_rebuild in includes/menu.inc
Helper function to build menu links for the items in the menu router.

Code

includes/database.inc, line 250

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

Example

boombatower - Mon, 2009-08-24 22:15

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

[Edited by KiamLaLuno to better format the code]

Another example (multiple calls)

daniel.a.lopez - Mon, 2009-11-09 04:00

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

[Edited by KiamLaLuno to remove the tag <code> used to wrap the PHP code]

Login or register to post comments
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.