I have a search form that searches for users names. The problem is, in my database, I have a separate firstname and lastname collumn. This is how my query currently looks like (this is inside a PHP script, and $term is the search input):
SELECT * FROM profiles WHERE firstname LIKE '%$term%' OR lastname LIKE '%$term%'
This works fine when the user only enter either a firstname or a lastname, but for full names such as “John Smith”, this won’t work.
How can I modify my query and/or php script to efficiently fix this problem? For example, should I split the string first, or etc?
You can do something like
That comparares firstname plus a space plus last name against
%$term%.However, you shouldn’t be building SQL queries with variables. You should use parametrized queries through PDO. See http://bobby-tables.com/php.html for examples.