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 '' at line 5 SELECT DISTINCT foto.id, foto.nome_foto FROM Gufetto_foto_articoli AS foto JOIN Gufetto_articolo AS art ON foto.id_articolo = art.id WHERE foto.id_articolo=