mysql_real_escape_string 与 Zend

发布于 2024-12-23 13:50:31 字数 764 浏览 1 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

无人接听 2024-12-30 13:50:31

您也可以使用参数绑定,那么该方法将如下所示:

public function getData($name)
{
  $sql = "SELECT * from customer where Customer_Name = :name";
  return $this->objDB->getAdapter()->fetchAll ($sql, ['name' => $name]);
}

然后您的数据将被自动转义

You could use parameter binding as well, then the method will look like:

public function getData($name)
{
  $sql = "SELECT * from customer where Customer_Name = :name";
  return $this->objDB->getAdapter()->fetchAll ($sql, ['name' => $name]);
}

Then your data will be escaped automatically

血之狂魔 2024-12-30 13:50:31

我遇到了这个问题,我使用了这种方式并且工作正常:

您可以使用 quote()

quote() 方法接受单个参数,即标量字符串值。它返回带有特殊字符的值,这些特殊字符以适合您正在使用的 RDBMS 的方式转义,并由字符串值分隔符包围。标准 SQL 字符串值分隔符是单引号 (')。

但 quote 返回一个带有 'string' 的字符串(在引号内返回),例如我从输入文本框(或通过 GET 方法中的 URL)从用户那里获取一个字符串

$string = $this->parameters['string']; // This is like $_POST or $_GET
$string = $this->db->quote($string);
$string = substr($string, 1, strlen($string)-2);   
//The above line will remove quotes from start and end of string, you can skip it

现在我们可以使用这个$string,它就像mysql_real_escape_string返回的一样

I had this problem, I used this way and is working correctly:

You can use quote():

The quote() method accepts a single argument, a scalar string value. It returns the value with special characters escaped in a manner appropriate for the RDBMS you are using, and surrounded by string value delimiters. The standard SQL string value delimiter is the single-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)

$string = $this->parameters['string']; // This is like $_POST or $_GET
$string = $this->db->quote($string);
$string = substr($string, 1, strlen($string)-2);   
//The above line will remove quotes from start and end of string, you can skip it

Now we can use this $string, and it is like what mysql_real_escape_string returns

倾城°AllureLove 2024-12-30 13:50:31

我遇到了同样的问题,这个解决方案对我来说效果很好。我希望这会有所帮助。
你可以这样做:

$quote_removed_name = str_replace("'","''",$name);

然后这样写你的查询:

$sql = "SELECT * from customer where Customer_Name = '$quote_removed_name'";

I had the same problem and this solution works fine for me. I hope this will help.
you can do something like this:

$quote_removed_name = str_replace("'","''",$name);

then write your query this way:

$sql = "SELECT * from customer where Customer_Name = '$quote_removed_name'";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文