CodeIgniter フレームワークから適応:
function compile_binds($sql, $binds)
{
if (strpos($sql, '?') === FALSE)
{
return $sql;
}
if ( ! is_array($binds))
{
$binds = array($binds);
}
// Get the sql segments around the bind markers
$segments = explode('?', $sql);
// The count of bind should be 1 less then the count of segments
// If there are more bind arguments trim it down
if (count($binds) >= count($segments)) {
$binds = array_slice($binds, 0, count($segments)-1);
}
// Construct the binded query
$result = $segments[0];
$i = 0;
foreach ($binds as $bind)
{
$result .= mysql_real_escape_string($bind);
$result .= $segments[++$i];
}
return $result;
}
次に、関数を持つことができます:
function query($sql, $binds)
{
return $db->Execute(compile_binds($sql, $binds));
}
$query = query('select * from table where val=?', array('10'));