I want a way to show SQL query how it will look with anonymous parameters (?) substituted by actual parameters.
This is only for readability purposes and debugging, and wouldn't be used as an actual query.
I have found this function which works for the most part:
return array_reduce($this->bindValues, function ($sql, $binding) {
return preg_replace('/\?/', is_numeric($binding) ? $binding : '"' . $binding . '"', $sql, 1);
}, $this->query);
replacing ? with actual values:
$data = array(
'item' => '1,
'type' => 'Are you ok.'
);
UPDATE `list` set `item`=?,`type`=? WHERE (`id` = ?) ;
UPDATE `list` set `item`="1",`type`="Are you ok." WHERE (`id` = 1) ;
but if the value contains ? I'm ending up with:
$data = array(
'item' => '1,
'type' => 'Are you ok?'
);
UPDATE `list` set `item`="1",`type`="Are you ok2" WHERE (`id` = ?) ;
How can I make this work, so only binding ? are replaced.
You can always use plain string operations:
3v4l code