function sql_escape($s)
{
return get_magic_quotes_gpc() ? $s : mysql_escape_string($s);
}
function mk_mysql_query($q,$a)
{
$i=0;
return preg_replace(
"~\?([is])~e",
" '\\1'=='i'
? (int)\$a[\$i++]
: '\'' . sql_escape(\$a[\$i++]) . '\''
",
$q
);
}
// test
$integer_value = "1.123";
$string_value = "foo'bar";
$limit = "123'";
$offset = "10 ?asd";
echo
mk_mysql_query(
"select foo from bar where integer_value=?i and string_value=?s limit ?i offset ?i",
array($integer_value, $string_value, $limit, $offset)
);