[英]What is the ZF2 equivalent of the quoteInto() method of ZF1?
在Zend Framework 1中,有一个数据库适配器的quoteinto方法,可用于引用sql语句。
我想知道它在Zend Framework 2中的等价物吗?
不幸的是,在ZF 2.0中引入了新的Zend\\Db
,删除了quoteInto()
方法。 并且没有相同的行为具有完全相同的行为。
在ZF2中有quoteValue()
方法。 此方法将一个值作为参数,然后引用该值,以便您可以安全地将其作为值放入SQL查询中。
但是,您可以使用quoteValue()
来复制ZF1 quoteInto()
方法的行为。 你可以简单地从ZF1中获取quoteInto()
方法的代码,并将quoteValue()
方法从ZF2中的平台对象应用到它:
// modified quoteInto() function for ZF2
function quoteInto($text, $value, $platform, $count = null)
{
if ($count === null) {
return str_replace('?', $platform->quoteValue($value), $text);
} else {
while ($count > 0) {
if (strpos($text, '?') !== false) {
$text = substr_replace($text, $platform->quoteValue($value), strpos($text, '?'), 1);
}
--$count;
}
return $text;
}
}
有一些差异。 ZF1有一个$type
参数,但由于ZF2使用这些东西的方式,type参数没有多大意义。 并且有一个$platform
参数,因为此方法依赖于quoteValue()
方法的平台。
引用SQL语句是一种旧的,可能不安全的方法。 您应该使用预防性语句,这些语句可以更好地防止SQL注入。 我将使用此处列出的PDO驱动程序之一(取决于您的数据库)并按照下面的一些使用准备好的查询的示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.