If you want to remove all spaces present in the data then you can Replace() with eith empty character, eg
$var = $folders['name'];
$query = "SELECT * FROM tablename where REPLACE(fieldname, ' ','') = '$var'";
but remember that it can kill the index, so the query will perform full table scan causing slow on large databases.
As a sidenote, the query is vulnerable with SQL Injection if the value(s)  of the variables came from the outside. Please take a look at the article below to learn how to prevent from it. By using PreparedStatements you can get rid of using single quotes around values.