views:

267

answers:

2

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)"
+2  A: 

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.

zneak
+2  A: 

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

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