You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'and files.file_path = 'pages/bg_img'' at line 5 Query is: SELECT * FROM pages LEFT JOIN pages_files pf ON pf.page_id = pages.page_id LEFT JOIN files on files.file_id = pf.file_id WHERE pages.page_id = and files.file_path = 'pages/bg_img'