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
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));
}
?>Login or register to post comments 
Example
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)
<?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]