Search code examples
phpmysqlsqlmysql-error-1064

Where clause containing dots extension in mysql


I have a question which is to select values from my DB. I have a where clause like this :

$query = 'SELECT * FROM liquid 
          WHERE Id = '.$id.' 
          AND (
                    picone = tiger-1477927129.jpg OR 
                    pictwo = tiger-1477927129.jpg 
                    OR picthree = tiger-1477927129.jpg 
                    ' . 'picfour = tiger-1477927129.jpg 
                    OR picfive = tiger-1477927129.jpg
                    OR picsix = tiger-1477927129.jpg
          )';

The problem is I have dots before the file extension, so sql does not execute this. Do you know how could I solve this?

Thanks Lee


Solution

  • Use quotes around the values i.e. image='image.jpg' and you will need to escape the quotes for the string in PHP

     $query = 'SELECT * FROM liquid WHERE
        Id = '.$id.'
        AND (
            picone = \'tiger-1477927129.jpg\'
            OR pictwo = \'tiger-1477927129.jpg\'
            OR picthree = \'tiger-1477927129.jpg\'
            OR picfour = \'tiger-1477927129.jpg\'
            OR picfive = \'tiger-1477927129.jpg\'
            OR picsix = \'tiger-1477927129.jpg\'
        )
    ';