mysql_real_escape_string 与 Zend
我正在使用 zend 框架开发一个 Web 应用程序。对于选择语句我使用了以下方式。
例如:
public function getData($name)
{
$sql = "SELECT * from customer where Customer_Name = '$name'";
return $this->objDB->getAdapter()->fetchAll ($sql);
}
这很好用。但是如果我将客户姓名发送为:colvin's place
, 查询失败。我知道这是因为单引号。
之前我使用过addslashes PHP 函数。但我发现这不是一个好方法。这次我使用了 mysql_real_escape_string
PHP 函数。
问题是它在警告之后说。
警告:mysql_real_escape_string() [function.mysql-real-escape-string]:用户访问被拒绝'ODBC'@'localhost'(使用密码:NO)
这是因为mysql_real_escape_string
函数需要连接到由mysql_connect
。我的问题是如何将它与 *Zend_DB* 类一起使用。我需要始终使用自定义选择查询。感谢您的其他建议(如果有)。
谢谢
I am developing a web application using zend framework. For select statements I have used following way.
Ex:
public function getData($name)
{
$sql = "SELECT * from customer where Customer_Name = '$name'";
return $this->objDB->getAdapter()->fetchAll ($sql);
}
This works fine. But If I send customer name as : colvin's place
,
The query fail. And I know it's because of the single quote.
Earlier I used addslashes PHP function. But I saw it is not a good way to do this. This time I used mysql_real_escape_string
PHP function.
The issue is it says following warning.
Warning</b>: mysql_real_escape_string() [<a href='function.mysql-real-escape-string'>function.mysql-real-escape-string</a>]: Access denied for user 'ODBC'@'localhost' (using password: NO)
This is because of the mysql_real_escape_string
function needs a connection to the database opened by mysql_connect
. My question is how can I use this with *Zend_DB* classes. I need to use custom select queries always. Appreciate your other suggestions if available.
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用
Zend_Db
提供的quote()
函数:http://framework.zend.com/manual/en/zend.db.adapter.html#zend.db.adapter.quoting.quote
You can use the
quote()
function provided byZend_Db
:http://framework.zend.com/manual/en/zend.db.adapter.html#zend.db.adapter.quoting.quote
您也可以使用参数绑定,那么该方法将如下所示:
然后您的数据将被自动转义
You could use parameter binding as well, then the method will look like:
Then your data will be escaped automatically
我遇到了这个问题,我使用了这种方式并且工作正常:
您可以使用
quote()
:但 quote 返回一个带有 'string' 的字符串(在引号内返回),例如我从输入文本框(或通过 GET 方法中的 URL)从用户那里获取一个字符串
现在我们可以使用这个
$string
,它就像mysql_real_escape_string
返回的一样I had this problem, I used this way and is working correctly:
You can use
quote()
:But quote returns a string with 'string' (return it inside quotation), for example I get an string from user from a input-text box (or by URL in GET method)
Now we can use this
$string
, and it is like whatmysql_real_escape_string
returns我遇到了同样的问题,这个解决方案对我来说效果很好。我希望这会有所帮助。
你可以这样做:
然后这样写你的查询:
I had the same problem and this solution works fine for me. I hope this will help.
you can do something like this:
then write your query this way: