I'm using this query to select data:
mysql_query("SELECT * FROM products WHERE product_name LIKE '%".$search."%'");
The only problem is, that it sometimes selects more, than I would like.
For example, I would like to select product "BLA", but my query select product "BLABLA" as well. To be clear, if i wanted to select "Product 1", I don't want the query to select "Product 11".
Does anybody know how to manage that?
Thanks.
Then don't use LIKE, but search for equality.
ie.
BTW I hope you sanitize/escape $search before using it in a query.
you can use select query like this ,i also use in cakePHP and it's helpful.
Try using regular expressions:
will select exact
BLA
will select
BLADDER
andBLACKBERRY
but notREBLAND
To select
BLA
as the first word of the string, use:The second condition may improve your query performance if you have an index on
product_name
.Do you just want to search on word boundaries? If so a crude version might be:
Or you could be a bit cleverer and look for word boundaries with the following
REGEXP
Remove
LIKE
keyword and use=
for exact matchEDIT
do not forgot to escape user input using mysql_real_escape_string otherwise your query will fail if some one enter quotes inside the input box.