mysql_real_escape_string with Zend

前端 未结 4 873
遇见更好的自我
遇见更好的自我 2021-01-19 00:38

I am developing a web application using zend framework. For select statements I have used following way.

Ex:

public function getData($name)
{
  $sql          


        
相关标签:
4条回答
  • 2021-01-19 01:12

    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'";
    
    0 讨论(0)
  • 2021-01-19 01:16

    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

    0 讨论(0)
  • 2021-01-19 01:28

    You can use the quote() function provided by Zend_Db:

    http://framework.zend.com/manual/en/zend.db.adapter.html#zend.db.adapter.quoting.quote

    0 讨论(0)
  • 2021-01-19 01:32

    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

    0 讨论(0)
提交回复
热议问题