0

I am in the process of updating a website that was created a previous admin. Most of it is outdated php code however there is too much to change so I am working with what I have.

I am trying to run a search query as follows;

$term = trim($_REQUEST['term']);
$searchterm = mysql_real_escape_string($term, $link); //link to db

$sql = "SELECT DISTINCT party_id 
FROM vw_ft_search WHERE 
MATCH (party_name) 
AGAINST ('+$searchterm' IN BOOLEAN MODE)";

This always throws the error below;

MySQL Error 1210 : Incorrect arguments to AGAINST

I have read the instructions here which advises using a literal string (which I think I am?). When I echo out the $searchterm it shows the correct search term ('Charles' in this case.)

I have also tried to run the query with a manual string entered as follows, however same result;

$sql = "SELECT DISTINCT party_id 
FROM vw_ft_search WHERE 
MATCH (party_name) 
AGAINST ('%Charles%' IN BOOLEAN MODE)";

Is it something to do with the query, view, variable?

Any direction is appreciated. Quite new to php/mysql.

Community
  • 1
  • 1
jonboy
  • 2,729
  • 6
  • 37
  • 77
  • What happen if you remove the `%` sign? – frz3993 May 27 '15 at 16:33
  • Wildcard to use with `LIKE`, for full text search it should be asterisk. I didn't even use wildcards to do a full text search with a single word. – frz3993 May 27 '15 at 17:19
  • And for mysql fulltext search you can not put wildcard before the left most character. – frz3993 May 27 '15 at 17:30
  • No, I mean the '%'. Something like '+Charles' or '+Charles*' not '+*Charles'. – frz3993 May 27 '15 at 19:04
  • Example of query with `LIKE` . `SELECT * FROM vw_ft_search WHERE party_name LIKE %Charles%` – frz3993 May 27 '15 at 19:07
  • Are you misunderstanding something. I was giving an example not an answer. What I meant was, to use `MATCH` and `AGAINST` the wildcard should be `*` not `%`. And there are some set of rules for `FULLTEXT` search in MYSQL. – frz3993 May 27 '15 at 19:59

1 Answers1

2
$term = trim($_REQUEST['term']);
$searchterm = mysql_real_escape_string($term, $link); //link to db

Add *, if you also want to match against Charlesk, Charlesd, etc.

$searchterm = $searchterm.'*';
$sql = "SELECT DISTINCT party_id 
FROM vw_ft_search WHERE 
MATCH (party_name) 
AGAINST ('$searchterm' IN BOOLEAN MODE)";

And also beware of the stopwords which can be found here. Matching against stopwords will return nothing.

frz3993
  • 1,595
  • 11
  • 13
  • That is not setting variable again. That was concatenating the wild card. I tested this, the only difference with yours is that I didnt use `mysql_real_escape_string` because I used prepared statement. I guess somewhere else in your code then. – frz3993 May 28 '15 at 11:16
  • Thanks anyway for the help! I have simply changed my query now to a much simpler one. Similar to your `SELECT * FROM vw_ft_search WHERE party_name LIKE %Charles%` suggestion. Answer accepted. – jonboy May 28 '15 at 13:26