여기에 내가 원하는 결과를 시뮬레이션하기 위해 채찍질 일부 코드는 다음과 같습니다
<?php
function preprepare(&$query, &$data) {
preg_match_all('/\?/', $query, $matches, PREG_OFFSET_CAPTURE);
$num = count($matches[0]);
for ($i = $num; $i;) {
--$i;
if (array_key_exists($i, $data) && is_array($data[$i])) {
$query = substr_replace($query, implode(',', array_fill(0, count($data[$i]), '?')), $matches[0][$i][1], strlen($matches[0][$i][0]));
array_splice($data, $i, 1, $data[$i]);
}
}
}
$query = 'SELECT * FROM `table` WHERE `col1` = ? AND `col2` IN(?) AND `col3` = ? AND `col4` IN(?)';
$data = array('foo', array(1, 2, 3), 'bar', array(4, 2));
preprepare($query, $data);
var_dump($query, $data);
?>
그것은 출력 :
array (size=2)
0 => string 'SELECT * FROM `table` WHERE `col1` = ? AND `col2` IN(?,?,?) AND `col3` = ? AND `col4` IN(?,?)' (length=93)
1 =>
array (size=7)
0 => string 'foo' (length=3)
1 => int 1
2 => int 2
3 => int 3
4 => string 'bar' (length=3)
5 => int 4
6 => int 2
쿼리 및 데이터는 문을 준비 정상적인 PDO 사용할 수 있습니다. 나는 그것이 모든 PDO 마술 niceness를 설명하는지 모르지만 그것은 지금까지 꽤 잘 작동합니다.
자주 묻습니다. [여기에 하나] (http://stackoverflow.com/questions/920353/php-pdo-can-i-bind-an-array-to-an-in-condition) [그리고 여기에 또 다른] (http : // stackoverflow .com/questions/14767530/php-using-pause-with-clause-array) –
여러 값에서 문자열을 만들어 바인딩합니다. –
일반적으로'?,?,?,?,?,? '당신은 배열 요소를 가지고 있습니다. –