If the goal is to see the values corresponding to the queries that substitution function.
function setValor($consulta, $valores){
$count = substr_count($consulta, '?');
$interrogacoes = array_fill('0', $count, '/\?/');
$consulta_original = preg_replace($interrogacoes, $valores, $consulta, 1);
return $consulta_original;
}
substr_count () returns the number of occurrences found (in the case ?
) of a string passed as second argument within another ( $consulta
). array_fill () creates an array with the% regex of repository with% being the number of elements will be defined by '/\?/'
and lastly the substitution occurs with preg_replace , it works so it looks for the first regex and replaces it with the first value found in $count
and so on until the end of the array arrives. The last argument $valor
means the maximum number of overrides made.
example:
$consultas = array(
'INSERT INTO tabela(nome, email, idade, endereco) VALUES (?,?,?,?)',
'SELECT * FROM tabela WHERE id IN(?,?)',
'DELETE FROM tabela where id = ?'
) ;
$valores = array(
array('joao', '[email protected].', '20', 'rua xx'),
array(99, 70),
array(48)
);
$i =0;
foreach($consultas as $item){
echo setValor($item, $valores[$i]) .'<br>';
$i++;
}
output:
INSERT INTO tabela(nome, email, idade, endereco) VALUES (joao,[email protected].,20,rua xx)
SELECT * FROM tabela WHERE id IN(99,70)
DELETE FROM tabela where id = 48