If the goal is to see the corresponding values 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 the second argument within another($consulta
). array_fill() creates an array with the replace regex '/\?/'
the number of items shall be defined by $count
and finally the substitution occurs with preg_replace, works as follows it searches for the first regex and replaces it with the first value found in $valor
and so on until the end of the array. The last argument 1
means the maximum number of replacements performed.
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++;
}
exit:
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
I don’t think there is a 'native' way to do this. Especially one that takes into account the proper escape of values. As far as I know (although I never checked the code), Mysqli never produces the full query in full and even sends the data separately.
– luiscubal
if there is no native way maybe there is a homemade way to do this, because in zend there is a function native to it, but I don’t know how to develop it in pure php to be able to evaluate the query
– RodrigoBorth
This suggests that Zend uses pdo_mysql and not mysqli. PDO has the
queryString
which can be used as a starting point.– luiscubal
http://stackoverflow.com/questions/962986/how-to-echo-a-mysqli-prepared-statement
– RodrigoBorth
Take a look at this link, the guy’s using mysqli
– RodrigoBorth
The code in the question is for debugging but be careful not to use it as code for another query because it does not escape the parameters.
– luiscubal