The real question is, is it safe to let the user decides of part of the query.
Even in a simple case, providing user-data to the database is unsafe.
Allowing users input to be directly provided to the database may be dangerous, but as far as your filter and use your database escaping strategy (ie: mysql_real_escape_string()
(well prepared statements or any ORM will do that for you) should be safe enough.
However, it may lead to performance problems, a simple EXPLAIN
on your query should warn you on how much rows are scanned by your RDBMS engine.
The best way to implement such feature is to use Search Engine. (Isn't it what you're trying to do?)
There are many choices outta there to help you implementing this.
You may take a look to Sphinx, Solr, Xapian or even Lucene. There are all excellent choice.
They basically allow you to "index" your content to make some fulltext search while increasing performance.
They can also give some incredible functionalities like OR
, AND
, LIKE
, MINUS
, etc. comparators/operators.
You may then be interested in this question: Choosing a stand-alone full-text search server: Sphinx or SOLR?