我,我通過向查詢函數添加參數解決了這個問題。 我發現codeigniter做得非常好,所以我適應了自己的口味。
實施例:
$result = Database::query('INSERT INTO table (column1,column2,column3) VALUES(?,?,?)',array($value1,$value2,$value3));
public static $bind_marker = '?';
public static function query($query, $binds = FALSE)
{
if($binds !== FALSE)
{
$query = self::compile_binds($query,$binds);
}
// $query now should be safe to execute
}
private static function compile_binds($query, $binds)
{
if(strpos($query, self::$bind_marker) === FALSE)
{
return $query;
}
if(!is_array($binds))
{
$binds = array($binds);
}
$segments = explode(self::$bind_marker, $query);
if(count($binds) >= count($segments))
{
$binds = array_slice($binds, 0, count($segments)-1);
}
$result = $segments[0];
$i = 0;
foreach($binds as $bind)
{
if(is_array($bind))
{
$bind = self::sanitize($bind);
$result .= implode(',',$bind);
}
else
{
$result .= self::sanitize($bind);
}
$result .= $segments[++$i];
}
return $result;
}
public static function sanitize($variable)
{
if(is_array($variable))
{
foreach($variable as &$value)
{
$value = self::sanitize($value);
}
}
elseif(is_string($variable))
{
mysql_real_escape_string($variable);
}
return $variable;
}
主要此外,我從笨的版本添加的我可以使用的陣列作爲一個參數,其是用於使用「IN」有用:
$parameters = array
(
'admin',
array(1,2,3,4,5)
);
$result = Database::query("SELECT * FROM table WHERE account_type = ? AND account_id IN (?)",$parameters);
我知道我在你之前的帖子上說過,但讓我重複一遍:使用PDO。如果您使用PDO的準備好的語句,您甚至不必擔心逃脫它們。 – ryeguy 2010-03-29 19:44:23
使用參數化的quires,使用adodb或pdo。 – rook 2010-03-29 21:28:43