SQL Using LIKE on userinput value

Viewed 66

I'm trying to select all values in a table based off the userinput. My issue is when I try to write the query and include the '%' character, I always run into errors. I've tried at least 10 different placements, but I always receive an error. The database being used is MySQL.

SELECT * FROM clients WHERE UserName = %:UserName;

enter image description here

1 Answers

If you want to use the operator LIKE with the wildcard '%' you must concatenate it to the user's input:

SELECT * 
FROM clients 
WHERE UserName LIKE CONCAT('%', :UserName);

If you want all rows with UserName that contain :UserName:

WHERE UserName LIKE CONCAT('%', :UserName, '%');
Related