Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql query against wildcard

I have a mysql query in php, using match / against to filter results. Im trying to get a wildcard after $string so the next character can be anything. Help?

"SELECT * FROM $table WHERE MATCH (message) AGAINST('$string%' IN BOOLEAN MODE)"
like image 358
David Avatar asked Feb 23 '10 00:02

David


2 Answers

Use * instead of % as a wildcard when using MATCH (...) AGAINST (...):

"SELECT * FROM $table WHERE MATCH (message) AGAINST('$string*' IN BOOLEAN MODE)"
like image 127
Mark Byers Avatar answered Sep 19 '22 06:09

Mark Byers


Don't embed variables into double-quoted strings and you're set:

"SELECT * FROM " . $table . " WHERE MATCH (message) AGAINST ('" . $string . "%' IN BOOLEAN MODE)"

That being said, I can't confirm this is valid SQL. But at least it'll ensure your variable is expanded properly and if it doesn't work then it won't be because of PHP.

And just in case this reminder will be useful, nevar forget to escape data that goes into a query.

like image 34
zneak Avatar answered Sep 18 '22 06:09

zneak