Code: Select all
function compile_binds($string, $binds)
{
if (!preg_match_all('#{(\d+)}#', $string, $map))
return $string;
if ( ! is_array($binds))
$binds = array($binds);
$map = $map[1];
$segments = preg_split('#{(\d+)}#', $string);
$result = '';
for($i=0, $len = count($segments); $i<$len; $i++)
{
$result .= $segments[$i];
if (array_key_exists($i, $map))
{
if (!array_key_exists($map[$i]-1, $binds))
throw new Exception('Bind index not found for {'.$map[$i].'}', 1025);
$result .= $binds[$map[$i]-1] ;
}
}
return $result;
}Code: Select all
$query = "
select
*
from
person
where
(id = {1} or {1} IS NULL)
or
(surname={3} and firstname = {2})
";
echo compile_binds($query, array(
10,
'John',
'Trotzki'
));- go to "system/database" directory;
- open DB_driver.php;
- rename compile_binds method to compile_binds_ci;
- insert this code:
Code: Select all
function compile_binds($sql, $binds)
{
if (!preg_match_all('#{(\d+)}#', $sql, $map))
return $this->compile_binds_ci($sql, $binds);
if ( ! is_array($binds))
$binds = array($binds);
$map = $map[1];
$segments = preg_split('#{(\d+)}#', $sql);
// Construct the binded query
$result = '';
for($i=0, $len = count($segments); $i<$len; $i++)
{
$result .= $segments[$i];
if (array_key_exists($i, $map))
{
if (!array_key_exists($map[$i]-1, $binds))
throw new Exception('Bind index not found for {'.$map[$i].'}', 1025);
$result .= $this->escape($binds[$map[$i]-1]) ;
}
}
return $result;
}EDIT: Exception is thrown if {N} is not found in the bind list.
EDIT2: NULL values bug fixed