I have a problem getting a query to work. I use PHP 8 and MySQL 5.7.36
What i need is let the user type a place (city,country or region) and show all corresponding places while typing.
The table name is : places and i have 4 fields : id, name, name_fr, alternate_name
So i want that if a user wants to find 'Belgium' so if he wrote it in French(belgique) of in English(Belgium), i want that Belgium is displayed... so i make the queries on 'alternate_field' field.. here what i did :
At first i made this query :
$dbcon->query("select name from places where (alternate_name like '%$q%') LIMIT 25");
It works perfectly but it was too slow..
So i found that i can use MATCH and AGAINST.. So i tried this :
1- at first, i alter the table with :
ALTER TABLE places ADD FULLTEXT(alternate_name);
2- then i tried this :
$dbcon->query("select name from places where MATCH(alternate_name) AGAINST('".$q."' IN BOOLEAN MODE) LIMIT 25");
and it was very fast but the suggestion appears only when i type the full name... Ex.. if i'm looking for 'belgium'.. if i start typing "belg" nothing is shown... but belgium is found when i fully wrote the place name... So that is not what i want...
3- Then i tried this :
$dbcon->query("select namefrom places where MATCH(alternate_name) AGAINST('+*".$q."*' IN BOOLEAN MODE) LIMIT 25");
and I thought that I had finally found because it was working..... until i add 'space' or hyphen (-) in the place name... like : 'Los angeles' or 'New York' or 'Winston-Salem'
When the place name has a hyphen, i receive this error : syntax error, unexpected $end, expecting FTS_TERM or FTS_NUMB or '*'
And when the place name has a space in the name... like 'Los Angeles' its shows all places with in the name 'los' and all places with in the name 'angeles'.. of course 'Los Angeles' is displayed but with a lot other places... that not corresponding to 'Los Angeles'
So what i'm doing wrong ?
Thank you so much ! Have a great day !