Search code examples
phpmysqlarraysescaping

How to escape a dot in mysql query?


The values of the array $rf contain a dot:

  $rf = array(img34563.jpg , img34536.jpg);

  $query = "SELECT * FROM $appin_table WHERE img IN ( ".implode( ',' , $rf )."";
  $result = mysql_query($query)
  or die(mysql_error());

How could I escape the dot, is that possible?

Thanks in advance.


Solution

  • Escaping the dot alone won't help you; you'll end up with a query like that:

    SELECT * FROM table WHERE img IN(img34563.jpg , img34536.jpg)
    

    You'll have to apply quotes before:

    function quote($k)
       {
          return '"' . mysql_real_escape_string($k) . '"';
       }
    
    $values = array_map('quote', $rf);
    $query = "SELECT * FROM $appin_table WHERE img IN ( ".implode( ',' , $values )."";